This topic contains 2 replies, has 2 voices, and was last updated by  Casey Coleman 10 months 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 os.ts.date changes the date-time value that’s passed into it. The call to os.date 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 os.date.

    --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 "ts.date" adjusts the output for us.
    os.ts.date('%c', dtestNbrA);
    --the "user data" based Interfaceware function preserves the input value.
    os.date('%c', dtestNbrB)
       
    --I would be happy to use the "os.ts.date" function if I could perform some artithmetic 
    --on the userdata value returned and consumed by "os.time" and "os.date".
    

    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);
    end
    

    code snippit in action

    Hope this helps.

    Wade

    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.

    Casey.

Tagged: 

You must be logged in to reply to this topic.