This topic contains 2 replies, has 2 voices, and was last updated by  Casey Coleman 4 years, 1 month ago.

Trying to parse and serialize UTC date-time values

  • I’m trying to take UTC date-time values as incoming strings and
    1) parse them into usable tables and/or Epoch integers
    2) perform some arithmetic on them
    3) and then re-serialize them back to a string

    Since these values are always in UTC, I do not need the help of automatic conversions to/from DST.
    And, so far, I can’t find a way to avoid them.

    For instance, in the following code snippet, you will see that the call to changes the date-time value that’s passed into it. The call to does not change the value.
    The problem for me is that I want to manipulate the original value and then re-serialize it, and I don’t see any way to manipulate the value of the userdata value used by

    --this is Interfaceware's standard DateParse module 
    local dateParser = require("dateparse2");
    --I picked this date because it illustrates the problem. 
    --Daylight savings time switched at 2am on Mar 12, 2017. 
    --the following behavior happens for any time in the hour between 2-3am on this day.
    local dtest1 = '2017-03-12T02:08Z'
    local _,dtestTbl = dateParser.parse(dtest1);
    local dtestNbrA = os.ts.time(dtestTbl)
    local dtestNbrB = os.time(dtestTbl)
    --Note that the built-in Lua function "" adjusts the output for us.'%c', dtestNbrA);
    --the "user data" based Interfaceware function preserves the input value.'%c', dtestNbrB)
    --I would be happy to use the "" function if I could perform some artithmetic 
    --on the userdata value returned and consumed by "os.time" and "".

    Thanks for any light you can shed.

    Hi Casey,

    The problem here is that your timestamp is being altered to accommodate the timezone of the machine Iguana is running on. You can fix that by using os.ts.gmtime and os.ts.gmdate instead.

    function main(Data)
       local dtest1 = '2017-03-12T02:08Z'
       local _,dtestTbl = dateParser.parse(dtest1);
       -- Create a unix timestamp in GM time otherwise we're using local time
       local dtestNbrA = os.ts.gmtime(dtestTbl)
       -- Arithmetic here (remove 1 day)
       local secondsInDay = 24*60*60
       dtestNbrA = dtestNbrA - secondsInDay
       -- Parse it back into a GMT string
       os.ts.gmdate('%c', dtestNbrA);

    code snippit in action

    Hope this helps.


    That did the trick, Wade.
    I was aware of the gm versions of these functions and I’m pretty sure I even tried os.ts.gmdate (though I was trying a lot of things and probably lost it in the rush).

    I think perhaps that after seeing this line from the os.ts.gmtime reference (“Note: os.ts.gmtime is identical to os.ts.time.”) I didn’t look too closely at the gmtime function.

    My rule of thumb will be to use the gm functions from beginning to end when working with UTC time values.
    In any case, thanks so much for your help.



You must be logged in to reply to this topic.