I'm creating an administrating tool and I need to convert string type like that: '1y2m3d4h5mi6s' to unixtime (seconds) in Lua. How can I make this?
I expect the output of StrToTime("1d") to be 86400.
function StrToTime(time_as_string)
local dt = {year = 2000, month = 1, day = 1, hour = 0, min = 0, sec = 0}
local time0 = os.time(dt)
local units = {y="year", m="month", d="day", h="hour", mi="min", s="sec", w="7day"}
for num, unit in time_as_string:gmatch"(%d+)(%a+)" do
local factor, field = units[unit]:match"^(%d*)(%a+)$"
dt[field] = dt[field] + tonumber(num) * (tonumber(factor) or 1)
end
return os.time(dt) - time0
end
print(StrToTime("1d")) -- 86400
print(StrToTime("1d1s")) -- 86401
print(StrToTime("1w1d1s")) -- 691201
print(StrToTime("1w1d")) -- 691200
Code snippet converting your date string to seconds
local testDate = '2019y2m8d15h0mi42s'
local seconds = string.gsub(
testDate,
'(%d+)y(%d+)m(%d+)d(%d+)h(%d+)mi(%d+)s',
function(y, mon, d, h, min, s)
return os.time{
year = tonumber(y),
month = tonumber(mon),
day = tonumber(d),
hour = tonumber(h),
min = tonumber(min),
sec = tonumber(s)
}
end
)
print(seconds)
You can also write a local function, I think it's a bit better to read.
local function printTime(y, mon, d, h, min, s)
local res = os.time{
year = tonumber(y),
month = tonumber(mon),
day = tonumber(d),
hour = tonumber(h),
min = tonumber(min),
sec = tonumber(s)
}
return res
end
local testDate = '2019y2m8d15h0mi42s'
local seconds = string.gsub(
testDate,
'(%d+)y(%d+)m(%d+)d(%d+)h(%d+)mi(%d+)s',
printTime
)
print(seconds)
Related
Summary of issue
Facing field 'day' missing in date table issue while adding days to timestamp extracted as an object.
my try
I have converted object to string and then adding the days to the timestamp, no use.
local my_obj = syslib.getobject(myPath)
local my_ts = tostring(my_obj)
local y, m, d, Hr, Min, Sec, Milli = my_ts:match '(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+).(%d+)'
local new_ts = os.time({ year = y, month = m, day = d, hour = Hr, min = Min, sec = Sec }) + 1*24*60*60
local next_time = string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", new_ts), Milli)
syslib.setvalue(my_obj, next_time)
Please help
Summary
I would like to add 1 day to given timestamp in lua script.
My try
local creation_date = "2016-01-01 00:00:00.000"
local y, m, d, Hr, Min, Sec, Milli = creation_date:match '(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+).(%d+)'
local dt = y .. '-' .. m .. '-'..d.. ' '..Hr..':'..Min..'-'..Sec..'.'..Milli
print(dt)
local new_ts = os.time
{ year = y, month = m, day = d,
hour = Hr, min = Min, sec = Sec, mill=Milli } + 1*24*60*60
print(new_ts)
I am getting new_ts = 1452470400
Expected result
2016-01-01 00:01:00.000 (in this format only)
The closest you may be able to get is something like this:
local creation_date = "2016-01-01 00:00:00.000"
local y, m, d, Hr, Min, Sec, Milli = creation_date:match '(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+).(%d+)'
local dt = y .. '-' .. m .. '-'..d.. ' '..Hr..':'..Min..'-'..Sec..'.'..Milli
print(dt)
local new_ts = os.time { year = y, month = m, day = d, hour = Hr, min = Min, sec = Sec } + 1*24*60*60
print(string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", new_ts), Milli))
The Mill entry in the table that you pass on to os.time() is being ignored, and the resulting value doesn't provide millisecond precision, so there's no point in including it. To get the format you desire, you need os.date() with an appropriate format, and then you can append the milliseconds to that again, manually.
With that, it looks like this:
$ lua script.lua
2016-01-01 00:00-00.000
2016-01-02 00:00:00.000
With the suggestion from #Nifim it can be simplified further, confirmed by the documentation
When the function is called, the values in these fields do not need to be inside their valid ranges. For instance, if sec is -10, it means 10 seconds before the time specified by the other fields; if hour is 1000, it means 1000 hours after the time specified by the other fields. - Lua 5.4 Reference Manual: os.time
local creation_date = "2016-01-31 00:00:00.000"
local y, m, d, Hr, Min, Sec, Milli = creation_date:match '(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+).(%d+)'
local dt = y .. '-' .. m .. '-'..d.. ' '..Hr..':'..Min..'-'..Sec..'.'..Milli
print(dt)
local new_ts = os.time { year = y, month = m, day = d + 1, hour = Hr, min = Min, sec = Sec }
print(string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", new_ts), Milli))
Output:
$ lua script.lua
2016-01-31 00:00-00.000
2016-02-01 00:00:00.000
I'm having this problem for a while and I can't find a good way to resolve it. So I have a timestamp like this for example : local t = "00:00:0.031" and I'm trying to convert it into an array like this :
local ft = {
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 31
}
This is pretty much the same no matter the language so if you have an idea on how to solve this but don't know Lua you can submit your answer anyway in any language. I tried solving it myself using regex and I'm quite sure it's possible this way...
Thank you for the interest to my question, have a good day (:
You could use string.match to extract the substrings in a first place. In a second time, you could use the function tonumber to convert it into numbers.
function ParseTimestampString (TimestampString)
local Hours, Minutes, Seconds, Milliseconds = string.match(TimestampString, "(%d+)%:(%d+)%:(%d+)%.(%d+)")
local Result
if Hours and Minutes and Seconds and Milliseconds then
Result = {
hours = tonumber(Hours),
minutes = tonumber(Minutes),
seconds = tonumber(Seconds),
milliseconds = tonumber(Milliseconds)
}
end
return Result
end
With the following code, you could get the results you want:
Result = ParseTimestampString("00:00:0.031")
print(Result.hours)
print(Result.minutes)
print(Result.seconds)
print(Result.milliseconds)
This should returns:
> Result = ParseTimestampString("00:00:0.031")
>
> print(Result.hours)
0
> print(Result.minutes)
0
> print(Result.seconds)
0
> print(Result.milliseconds)
31
Here is a not bad way using string.gmatch which is splitting by regex in Lua. Here the value is being split by either ":" or ".". Then there is a counter in place to match the index for the resulting table.
local t = "00:00:0.031"
local ft = {
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0
}
local count = 1
for str in string.gmatch(t, "([^:|.]+)") do
ft[count] = tonumber(str)
count = count + 1
end
You can do a printing loop afterwards to check the results
for i = 1, 4 do
print(ft[i])
end
Output:
0
0
0
31
The main problem I have found with my solution is that it does not save the values under the keys listed but instead the numbers 1 2 3 4.
My Simplest Answer Will Be Just Split the given string by your regex, in This Case For HOUR:MIN:SEC.MS
first Split By (:) To Get HOUR MIN & SEC+MS, Then Split SEC+MS by (.) To separate Both seconds And milliseconds
Below is my answer in java
import java.util.*;
class timeX {
long hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 31;
//Convert Given Time String To Vars
timeX(String input) {
//Split Input By (:) For Hour, Minutes & Seconds+Miliseconds
String[] splitted=input.split(":");
this.hours=Long.parseLong(splitted[0]);
this.minutes=Long.parseLong(splitted[1]);
//Split Again For Seconds And Miliseconds By (.)
String[] splittedMandS=splitted[2].split("\\.");
this.seconds=Long.parseLong(splittedMandS[0]);
this.milliseconds=Long.parseLong(splittedMandS[1]);
}
}
public class Main
{
public static void main(String[] args)
{
timeX tmp = new timeX("30:20:2.031");
System.out.println("H: "+tmp.hours+" M: "+tmp.minutes+" S: "+tmp.seconds+" MS: "+tmp.milliseconds);
}
}
With Lua you can do...
The os.date() can be a format tool for seconds.
...but depends on Operating System.
This works on Linux but not (as i know so far) on MS-Windows.
print(os.date('%H:%M:%S',0-3600)) -- puts out: 00:00:00
print(os.date('%H:%M:%S',300-3600)) -- puts out: 00:05:00
Also it can output the date/time as a table.
> tshow=function(tab) for k,v in pairs(tab) do print(k,'=',v) end end
> tshow(os.date('*t'))
day = 4
year = 2021
month = 11
hour = 11
yday = 308
isdst = false
min = 23
wday = 5
sec = 51
...and unfortunally it has no milliseconds.
If the table output of os.date() is saved as a table...
> ttable=os.date('*t')
> os.time(ttable)
1636021672
> os.date(_,os.time(ttable))
Thu Nov 4 11:27:52 2021
> os.date('%H:%M:%S',os.time(ttable))
11:27:52
...then its key/value pairs can be used for: os.time()
Further code do nearly what you expect when in ttable key 1 is your time with milliseconds as a string...
local tshow=function(tab) for k,v in pairs(tab) do print(k,'=',v) end end
local ttable=os.date('*t') -- Create a time table
ttable[1]='0:0:0.31' -- Numbered keys in sequence are ignored by os.tim()
ttable[2]=ttable[1]:gsub('(%d+):(%d+):(%d+)[.](%d+)','return {year=ttable.year,month=ttable.month,day=ttable.day,hour=%1,min=%2,sec=%3,milliseconds=%4}')
-- That creates ttable[2] with the value:
--- return {year=ttable.year,month=ttable.month,day=ttable.day,hour=0,min=0,sec=0,milliseconds=31}
-- Lets convert it now to a table with...
ttable[2]=load(ttable[2])()
-- Using tshow() to look inside
tshow(ttable[2])
That will output...
milliseconds = 31
day = 4
year = 2021
hour = 0
month = 11
min = 0
sec = 0
And this will put it out formated with os.date()
print(os.date('%H:%M:%S.'..ttable[2].milliseconds,os.time(ttable[2])))
-- Output: 00:00:00.31
I have a .log file that looks like this :
--hh:mm:ss
10:15:46
10:09:33
10:27:26
10:09:49
09:54:21
10:10:25
And what I need to do is to calculate the average on those timestamps, I wrote a function to calculate the average :
function calculate_average_working_hours(working_day_diff)
local current_hour, current_minutes, current_secondes = 0, 0, 0
local total_hour, total_minutes, total_secondes = 0, 0, 0
local working_days_counter = #working_day_diff
for key, value in pairs(working_day_diff) do
current_hour, current_minutes, current_secondes = extract_timestamp_value(value) --covert the values to numbers
total_hour = total_hour + current_hour
total_minutes = total_minutes + current_minutes
total_secondes = total_secondes + current_secondes
end
total_hour = math.ceil(total_hour / working_days_counter)
total_minutes = math.ceil(total_minutes / working_days_counter)
total_secondes = math.ceil(total_secondes / working_days_counter)
return ("%02d:%02d"):format(total_hour, total_minutes)
end
So basically what I'm doing is to sum the seconds, minutes, and hours and divide the results by the number of logs,
for example if i have 10:15:46 and 10:09:33 i will add the seconds, minutes and hours and divide it by 2
is this the correct way to calculate the average of timestamps values?
I would be something like this solved the problem:
--- example 1
local log = [[--hh:mm:ss
10:15:46
10:09:33
10:27:26
10:09:49
09:54:21
10:10:25
]]
local function seconds(v)
local h,m,s = v:match("(%d%d):(%d%d):(%d%d)")
if h and m and s then return h*3600+m*60+s else return nil end
end
local i,sum = 0, 0
for line in log:gmatch('(.-)[\r\n]') do
local r = seconds(line)
if r then i=i+1; sum = sum+r end
end
print(sum,i, os.date("!%X",sum/i) )
I'm working on a problem where I want to do calculations on NSDates where a single NSDate gives different dd/mm/yyyy values in different time zones.
To do that I'm currently using New York City (EST) and Aukland, NZ, since they are frequently on different dates.
I'd like to be able to use the time zones on either side of the international date line, UTC+12, and UTC-12. There appears to be a standard abbreviation for UTC+12, ANAT, for Anadyr, Russia. However, the iOS implementation of TimeZone/NSTimeZone doesn't seem to recognize it. There also does not seem to be an abbreviation for UTC-12 (which would be in Alaska).
Does anybody know if there are such abbreviations for UTC+12 and UTC-12 that iOS (or Mac OS, for that matter) recognizes?
It looks like the answer is no.
I wrote some code to fetch all the system time zones, sort them by offset, and print them:
typealias timeZoneTuple = (abbreviation: String, name: String, offset: Int)
let timeZones = TimeZone.abbreviationDictionary
let mappedTimeZones: [timeZoneTuple] = timeZones
.map {key, value in
var offset = 0
if let timeZone = TimeZone(abbreviation: key) {
offset = timeZone.secondsFromGMT() / 3600
}
return (abbreviation: key, name: value, offset:offset)}
.sorted {$0.offset < $1.offset}
mappedTimeZones.forEach {
let abbreviation = $0.abbreviation.padding(toLength: 4, withPad: " ", startingAt: 0)
let name = $0.name.padding(toLength: 20, withPad: " ", startingAt: 0)
print("abbreviation = \(abbreviation), offset = \(name), val = \($0.offset)")}
The output of the above code is:
abbreviation = HST , offset = Pacific/Honolulu , val = -10
abbreviation = AKDT, offset = America/Juneau , val = -9
abbreviation = AKST, offset = America/Juneau , val = -9
abbreviation = PST , offset = America/Los_Angeles , val = -8
abbreviation = PDT , offset = America/Los_Angeles , val = -8
abbreviation = MDT , offset = America/Denver , val = -7
abbreviation = MST , offset = America/Denver , val = -7
abbreviation = CDT , offset = America/Chicago , val = -6
abbreviation = CST , offset = America/Chicago , val = -6
abbreviation = EDT , offset = America/New_York , val = -5
abbreviation = PET , offset = America/Lima , val = -5
abbreviation = EST , offset = America/New_York , val = -5
abbreviation = COT , offset = America/Bogota , val = -5
abbreviation = ADT , offset = America/Halifax , val = -4
abbreviation = AST , offset = America/Halifax , val = -4
abbreviation = CLT , offset = America/Santiago , val = -3
abbreviation = CLST, offset = America/Santiago , val = -3
abbreviation = ART , offset = America/Argentina/Bu, val = -3
abbreviation = BRST, offset = America/Sao_Paulo , val = -2
abbreviation = BRT , offset = America/Sao_Paulo , val = -2
abbreviation = GMT , offset = GMT , val = 0
abbreviation = WET , offset = Europe/Lisbon , val = 0
abbreviation = BST , offset = Europe/London , val = 0
abbreviation = WEST, offset = Europe/Lisbon , val = 0
abbreviation = UTC , offset = UTC , val = 0
abbreviation = CEST, offset = Europe/Paris , val = 1
abbreviation = WAT , offset = Africa/Lagos , val = 1
abbreviation = CET , offset = Europe/Paris , val = 1
abbreviation = CAT , offset = Africa/Harare , val = 2
abbreviation = MSD , offset = Europe/Moscow , val = 3
abbreviation = EAT , offset = Africa/Addis_Ababa , val = 3
abbreviation = IRST, offset = Asia/Tehran , val = 3
abbreviation = MSK , offset = Europe/Moscow , val = 3
abbreviation = EET , offset = Europe/Istanbul , val = 3
abbreviation = EEST, offset = Europe/Istanbul , val = 3
abbreviation = GST , offset = Asia/Dubai , val = 4
abbreviation = IST , offset = Asia/Calcutta , val = 5
abbreviation = PKT , offset = Asia/Karachi , val = 5
abbreviation = BDT , offset = Asia/Dhaka , val = 6
abbreviation = WIT , offset = Asia/Jakarta , val = 7
abbreviation = ICT , offset = Asia/Bangkok , val = 7
abbreviation = SGT , offset = Asia/Singapore , val = 8
abbreviation = HKT , offset = Asia/Hong_Kong , val = 8
abbreviation = PHT , offset = Asia/Manila , val = 8
abbreviation = KST , offset = Asia/Seoul , val = 9
abbreviation = JST , offset = Asia/Tokyo , val = 9
abbreviation = NZDT, offset = Pacific/Auckland , val = 13
abbreviation = NZST, offset = Pacific/Auckland , val = 13
So it looks like UTC-12, UTC-11, UTC-1, UTC+10, UTC+11, and UTC+12 are all missing from the "named" timezones that are available in Cocoa.
EDIT:
Based on a comment from #MattJohnson, it seems that the identifiers is a better way to get the list of available time zones. Modifying my code to use identifiers instead:
struct timeZoneStruct: CustomStringConvertible {
let identifier: String
var offset: Int
var description: String {
let displayOffset = String(format: "%3d", offset)
let displayIdentifier = (identifier + ",").padding(toLength: 30, withPad: " ", startingAt: 0)
return "identifier = \(displayIdentifier) offset = \(displayOffset)"
}
}
let timeZoneIDs = TimeZone.knownTimeZoneIdentifiers
let mappedTimeZones: [timeZoneStruct] = timeZoneIDs
.map {identifier in
var offset = 0
if let timeZone = TimeZone(identifier: identifier) {
offset = timeZone.secondsFromGMT() / 3600
}
return timeZoneStruct(identifier: identifier, offset: offset)}
.sorted {$0.offset < $1.offset}
mappedTimeZones.forEach {
print($0.description)
}
That yields a list of time zones ranging from UTC-11 (Pacific/Pago_pago) to UTC+14 (Pacific/Apia)
(There are quite a few duplicates for most time zones, so the list is too long to include here.)
So it seems there are defined time zones for offsets from UTC-11 to UTC+14. There is not a time zone for UTC-12 however, even though Baker Island, at Lat/Long: 0°12'N / 176°29'W, is in UTC-12. Curious.
This is the simplest way to get all timezones with their respective abbreviation.
P.S Not all timezones have their proper 3-Letter Abbreviations.
let timezoneList = NSTimeZone.knownTimeZoneNames
for i in 0...timezoneList.count - 1 {
let locale = NSTimeZone.init(name: timezoneList[i])
print("Region: \((locale?.name)!) Abbr: \((locale?.abbreviation)!)")
}
Also total 51, 3-lettered Abbreviations are present:
print(TimeZone.abbreviationDictionary.count)
You can also explore https://developer.apple.com/documentation/foundation/timezone for more.