Is there a simple, 2-3 line way to do this?
Look at the clock man page, especially where it mentions the -gmt option
to "clock format".
That will *format* the number of seconds I have as a GMT time. But the
number of seconds I have isn't GMT, it's local. Given the local time, I
want to know the GMT time.
Why in the world would you think the seconds returned by [clock seconds] is
local?
BTW, did you even try what Bryan suggested?
The only difference in time between here (pick your own definition for
"here) and GMT is the formatting, so I'm not sure what you're asking.
How is what I suggested not what you want? Does the following code print
something that is unexpected for you?
% set now [clock seconds]
1135889846
% clock format $now
Thu Dec 29 14:57:26 -0600 2005
% clock format $now -gmt 1
Thu Dec 29 20:57:26 +0000 2005
You do know that Microsoft Windows has been making that mistake for
years? Tcl tries very hard to paper over the fact FWIW (there are some
ugly edge cases though.)
Donal.
You didn't try it did you? It works as expected:
% clock format [clock seconds]
Fri Dec 30 8:31:45 AM Malay Peninsula Standard Time 2005
% clock format [clock seconds] -gmt 1
Fri Dec 30 12:31:48 AM GMT 2005
>From the results I got, since Malaysian time is GMT+8, it looks like it
works (8:31 AM here means it's 31 minutes past midnight GMT).
Sorry, I was incredibly unclear, mostly because I didn't even know what
I wanted.
[clock seconds] returns a number of seconds. I format those seconds to
my local timezone. Then I remove the TZ designation and pretend that it
is a GMT time. I turn that GMT time into a number of seconds.
Obviously I can do this with string manipulation, I was just wondering
if there was a to do it with [clock], like if it could return how many
seconds from GMT my localtime is. But now that I lay it all out like
that, I realize just how ugly this is (it was a workaround to another
problem). I've since just fixed the issue I was trying to work around
in the first place.
Sorry, everyone!
I use following algoritm to determine the GMT time to calculate planet
positions
## let t be the local time
set t [clock seconds]
1136026469
clock format $t -format "%Y-%m-%d %H:%M:%S"
2005-12-31 11:54:29
## Belgium is GMT+1
clock format $t -format "%Y-%m-%d %H:%M:%S" -gmt 1
2005-12-31 10:54:29
## Following command will convert the local time t to GMT time
clock scan [clock format $t -format "%Y-%m-%d %H:%M:%S" -gmt 1]
1136022869
That is just plain wrong.
Whatever you might believe, [clock seconds] returns the number of
nominal seconds since 00:00 UTC, 1 January 1970. There is no such
thing as "local seconds" in Tcl. All time zone conversion happens
inside [clock scan] and [clock format]. If [clock seconds] reports
time in the local time zone and not UTC, that is a bug. (Nobody
has reported such a bug - and since I'm the mainainer of [clock],
I'd surely know if anyone had.)
--
73 de ke9tv/2, Kevin
OK, I shouldn't have set the starting value with [clock seconds]. But if
the starting value represents a local time, not obtained from TCL
build-in commands, then in my opinion the algoritm works.