lua-users home
lua-l archive

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


It was thus said that the Great steve donovan once stated:
> On Fri, Apr 13, 2012 at 10:16 AM, Rena <hyperhacker@gmail.com> wrote:
> > Ouch.) Does Lua provide a way to turn a table into a Unix timestamp
> > under the assumption that the table specifies GMT, not the local
> > timezone?

  Even operating systems have trouble with timezones:

http://www.chronos-st.org/Discovering%20the%20Local%20Time%20Zone--Why%20It%27s%20a%20Hard%20Problem.html

> I use this kind of logic to find the timezone offset in pl.Date:
> 
>         local t = os.time()
>         local ut = os.date('!*t',t)
>         local lt = os.date('*t',t)
>         thour = lt.hour - ut.hour
>         tmin = lt.min - ut.min
> 
> If this is misguided I would certainly like to know!

  It is.  Try this:

gmt = {  0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 , 10 , 11 ,
        12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 }
loc = { 19 , 20 , 21 , 22 , 23 ,  0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,
         7 ,  8 ,  9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 }

for i = 1 , 24 do
  print(i,loc[i] - gmt[i])
end

And even when it does work, it still doesn't take into account DST.  I found
this works:

now   = os.time()
lmt   = os.date("*t",now)
gmt   = os.date("!*t",now)
timel = os.time(lmt)
timeg = os.time(gmt)
zone  = os.difftime(timel,timeg)

if lmt.isdst then
  if zone < 0 then
    zone = zone + 3600
  else
    zone = zone - 3600
  end
end

  -spc (Time is hard!  I wonder how The Doctor does it?)