lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Monday, August 5, 2013, Andrew Starks wrote:
I apologize in advance for the "CSCI 99" nature of this post.

I'll start with the most important question that I have, and then the
reason that I'm asking it:

When daylight savings hits, does os.time() shift with it?


The reason that I'm asking this dumb question is that I am confused by
the following:

> =os.time() - os.time(os.date("*t"))
0
> =os.time() - os.time(os.date("!*t"))
-21600

According to every definition that I've read, time() returns "number
of seconds since 00:00 hours, Jan 1, 1970 *UTC*"

That would lead me to believe that  passing in a table with the
current date and time in UTC would result in the same answer as
`os.time(nil)` ... but it doesn't. I need to pass in "*t" to get that.
So, then I wonder how this behavior wouldn't be described as, "The
value returned represents the number of seconds since 00:00 hours, Jan
1, 1970, *shifted to the local time zone.*"

I'd blame Windows, but this is also true on my Mac. So now I think I'm
just not understanding this.

The second reason that I'm confused is that "isdst" is currently set
to true, which means that I'm actually off by -5 hours, not -6 (
-21600 / 60 / 60 ). So the answer not only seems wrong, but
unhelpfully so.

I've read the Lua Wiki, looked through Lua's source (noticed it's
doing next to nothing, in terms of trickery, os it's not to blame) and
have LuaDate. I still wonder whether or not os.time() shifts when dst
flips.

I hate time.

-Andrew

I'm responding to my own post, with what I hope is the answer. 

os.date can receive a number value, which is inturpreted in the the same way as os.time's output is defined. It returns *local time*. os.time follows the standard posix definition, which is usually as described above bit it receives a time structure, then it is interpreted as *local time* and is converted to UTC number of seconds. 

If time is output from os.date as UTC, then read by os.time, it will be converted by os.time and thus be offset by the time zone. 

What's more, if os.time receives the current time in UTC, it shifts it, but with isdt as false. If it then receives the current time in the current time zone, it shifts it as well, arriving at UTC. do the math and your always going to get the non-dst answer. 

Sorry for the noise. 

-Andrew