Search code examples
timestamptclutc

Adding/subtracting a second to a UTC timestamp


I have some timestamp data in the form of "2013-07-31T13:31:29" that I need to add a second to. I was having some issue with "clock add" so my plan was to convert the time into epoch time and then increment it. When trying to do this I noticed that the times don't seem to match each-other.

Timestamp: 2013-07-31T13:31:29
Epoch time: 1375252289
GMT: Wed, 31 Jul 2013 06:31:29 GMT

This timestamp was generated via the TCL code below:

# timeMinusMilli == 2013-07-31T13:31:29
set epoch [ clock scan $timeMinusMilli -gmt 0 ]

Now, maybe I'm just confused, but I would think that 2013-07-31T13:31:29 would be Wed, 31 Jul 2013 1:31:29, not 6:31:29.


Solution

  • The way ISO times are scanned by Tcl is documented: http://www.tcl.tk/man/tcl8.5/TclCmd/clock.htm#M83

    An ISO 8601 point-in-time specification, such as “CCyymmddThhmmss,” where T is the literal “T”, “CCyymmdd hhmmss”, or “CCyymmddThh:mm:ss”. Note that only these three formats are accepted. The command does not accept the full range of point-in-time specifications specified in ISO8601. Other formats can be recognized by giving an explicit -format option to the clock scan command.

    So, you either have to remove the punctuation from the date part, or fully specify the expected input

    % set timestr 2013-07-31T13:31:29
    2013-07-31T13:31:29
    % set t [clock scan [string map {- ""} $timestr]]
    1375291889
    % set t [clock scan $timestr -format {%Y-%m-%dT%T}]
    1375291889
    % clock format $t
    Wed Jul 31 13:31:29 EDT 2013
    

    Then, manipulate away:

    % clock format [clock add $t +1 second]
    Wed Jul 31 13:31:30 EDT 2013
    

    Notice I didn't have to do anything special to interpret the time in my timezone (EDT)