2

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.

Joshua
  • 4,270
  • 10
  • 42
  • 62
  • Its because of the time zones. GMT (also called UTC) is roughly the time in England ignoring summer time. You must be in eastern europe or the middle east. – Lee Meador Aug 07 '13 at 17:39
  • so I should be doing GMT-5 since I am in EST? – Joshua Aug 07 '13 at 17:42

1 Answers1

3

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)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I'm using TCL 8.4 (out of my control to upgrade, sadly) and I do not believe you can do a scan -format in 8.4... so it looks like I'm doing it the map way. Thanks! – Joshua Aug 07 '13 at 18:29
  • @Joshua You could try calling an 8.5 tclkit via `exec` to run a script to do the timestamp parsing… which would be _ridiculously_ convoluted but would in fact work. (Except if you're in a very odd timezone.) – Donal Fellows Aug 08 '13 at 14:58