• Subject: Re: slickest way to find out local time zone offset in pure lua?
• From: Louis Mamakos <louie@...>
• Date: Wed, 5 Mar 2008 11:20:09 -0500

```On Wed, Mar 05, 2008 at 08:33:08AM -0500, Louis Mamakos wrote:
> On Wed, Mar 05, 2008 at 09:57:42AM +0000, Robert Raschke wrote:
> > Norman Ramsey writes:
> > > I'm sure this question has come up many times, but the way I'm doing
> > > it seems clunky, and I hope someone here has a solution that looks sweet:
> > > using pure Lua, how can I compute the time difference between local time
> > > and UCT (sometimes known as universal time or Greenwich Mean Time)?
> >
> > Using http://luaforge.net/projects/date/ , which is pure Lua:
> >
> > require "date"
> > print(date():getbias())
> >
> >
> > Robby
> >
>
>
> local currenttime = os.time()
> local utc = os.date("!*t", currenttime)
> local lcl = os.date("*t",  currenttime)
> local offset = (utc.hour*60 + utc.min)  - (lcl.hour*60 + lcl.min)
> print (offset)  -- offset in minutes

On further reflection, this code is buggy if the UTC and local time
span days, but you get the idea.

In fact, I think this is a shorter, more correct result of figuring
out the offset between local time and UTC:

now = os.time()
UTC_offset_seconds = os.difftime(os.time(os.date("!*t", now)),
os.time(os.date("*t", now)))

This presumes os.time() on UNIX returns number of seconds since the
epoch, of course.

louie

```

• Follow-Ups:
• References: