Time Zones Question

Hi All, 

Im very new to corona so please be kind

Is there an easy way to detyermine the time zone a device is in (relative to UTC)?

I’ve done some searching on Time Zone and Locale but havent found what Im after

In my app its important that the time used is accurate so I want to get the time off of the GPS, not the users set time.  I need to let the user enter a time that something will happen and test against GPS time for the event.

This means the user will be entering time in their local time, and it will be tested against UTC.

So If I’m in the time zone UTC+10 I’ll be 10 hours out.   

I dont mind if its the locale the user has set in the devices settings or if there is a way of determining time zone based on location.

Thanks

Hi, I’m not aware of an API to get the timezone off the device. However, you can use the GPS API to get a location fix for where the user is standing and then translate this into a timezone using a lookup of some sort. Would this work for you? Just an idea. 

Here’s a couple routines that might get you started, best of luck.

[lua]

– from: http://forums.coronalabs.com/topic/33652-day-of-week/?hl=os.date%28%29

function get_day_of_week( dd, mm, yy )
    local timestamp = os.time( { year = 1900 + yy, month = mm, day = dd } )
    return os.date( “%w”, timestamp ) + 1, os.date( “%a”, timestamp )
end

local function get_timezone()
  local now = os.time()
  return os.difftime(now, os.time(os.date("!*t", now)))
end

[/lua]

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Thanks Guys,

mpappas, that’s a neat function. I think it still leaves me with a problem with daylight savings, but I’ll probably go down this path.

The other option I have thought of relies on an internet connection, JSON and Google timezone API.  but I dont want to rely on an internet connection and know there is restrictions on the use of the API

print(os.date("%Z"))

That will return the timezone information if the device has timezone information set.  Now when I ran that  using my local Lua install it printed EDT, which is what I expected.  You can then look to see if the timezone matches known DST timezones and adjust accordlingly.

Thanks Rob,

http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

os.date("%Z") and the table in the link above should do the job.

Cheers

Hi, I’m not aware of an API to get the timezone off the device. However, you can use the GPS API to get a location fix for where the user is standing and then translate this into a timezone using a lookup of some sort. Would this work for you? Just an idea. 

Here’s a couple routines that might get you started, best of luck.

[lua]

– from: http://forums.coronalabs.com/topic/33652-day-of-week/?hl=os.date%28%29

function get_day_of_week( dd, mm, yy )
    local timestamp = os.time( { year = 1900 + yy, month = mm, day = dd } )
    return os.date( “%w”, timestamp ) + 1, os.date( “%a”, timestamp )
end

local function get_timezone()
  local now = os.time()
  return os.difftime(now, os.time(os.date("!*t", now)))
end

[/lua]

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Thanks Guys,

mpappas, that’s a neat function. I think it still leaves me with a problem with daylight savings, but I’ll probably go down this path.

The other option I have thought of relies on an internet connection, JSON and Google timezone API.  but I dont want to rely on an internet connection and know there is restrictions on the use of the API

print(os.date("%Z"))

That will return the timezone information if the device has timezone information set.  Now when I ran that  using my local Lua install it printed EDT, which is what I expected.  You can then look to see if the timezone matches known DST timezones and adjust accordlingly.

Thanks Rob,

http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

os.date("%Z") and the table in the link above should do the job.

Cheers

I needed this, so I made this into a CSV text file timezone table. I added a column with the offset as a number with two decimals. It’s in UTF-8, no BOM, Windows line breaks. 

I needed this, so I made this into a CSV text file timezone table. I added a column with the offset as a number with two decimals. It’s in UTF-8, no BOM, Windows line breaks. 

@henrik5:

please, could you share with us the final code for the timestamp?

Would appreciate it a lot!

@henrik5:

please, could you share with us the final code for the timestamp?

Would appreciate it a lot!

Well, I just load the CSV to a table and loop through it, if (tz[i][1]==os.date("%Z")) and (tz[i][2]==os.date("%z")) then mytz=timezones[i][3]. Actually, I’d like a link to the official nice name list for os.date("%z") in lua. I’d like to verify that it identifies the nicenames for all timezones, since I can’t list them in the Simulator or on a phone. You need to match on the nicename to differentiate between EST (USA) and EST (Australia), for example.

Edit: No, I don’t do it this way.

It seems as if os.date("%z") gives a 5 character string starting with “+” or “-” followed by hours and minutes, f.ex. “+0100”.

Is this reliable on all Android and Iphone phones? If so, then parsing this string is all you need. Optional question, why it mismatches the lua documentation.

I also need to know if it includes DST, of course.

Well, I just load the CSV to a table and loop through it, if (tz[i][1]==os.date("%Z")) and (tz[i][2]==os.date("%z")) then mytz=timezones[i][3]. Actually, I’d like a link to the official nice name list for os.date("%z") in lua. I’d like to verify that it identifies the nicenames for all timezones, since I can’t list them in the Simulator or on a phone. You need to match on the nicename to differentiate between EST (USA) and EST (Australia), for example.

Edit: No, I don’t do it this way.

It seems as if os.date("%z") gives a 5 character string starting with “+” or “-” followed by hours and minutes, f.ex. “+0100”.

Is this reliable on all Android and Iphone phones? If so, then parsing this string is all you need. Optional question, why it mismatches the lua documentation.

I also need to know if it includes DST, of course.