time zone handling
Page last updated
14 Dec 2011, by
Hendrik Lipka.
11
replies
time
When using NTP to set the current time, it is always set to UTC. The mbed libraries have no notion of timezone handling. This library can read time zone definitions, and can calculate the proper local time. It also handles the daylight savings.
Library
/users/hlipka/libraries/TimeZone
- first, set up the time zone file as described below
- make sure you have mounted the time zone file under /local
- in your code, set the current time (preferably via an NTP call)
- create a /Time/ object
- whenever you need the current local time, create a TimeStamp object via the Time object
- use the provided methods to get the time
A demo program can be found here (Note: is uses an NTP server suited for Germany, so you might want to change this).
Timezone files
This library uses time zones files in CSV format, as provided by this project.
- go to the project page
- download the most current data file (right now this is tzdata_2010o.zip)
- extract it
- go to the tzdata/csv folder
- look for the csv file matching your location the best (use one of the global time zones, or by city divided by continents)
- copy this file as 'timezone.csv' to the mbed USB disk
After that, use the library as described.
11 comments on time zone handling:
Thanks! This saves me some time.
I've had a clock working OK until several days ago when suddenly the UTC time has been Jan 3, 1970. I've tried several sites such as "0.de.pool.ntp.org", and North American, Japanese, etc. All give the same result. Is it the server, and if it is, are there some others I could use?
Then, out of desparation, I tried this project with no changes. The UTC time problem remained while the screen prints "set time OK" and the .csv file does not load printing "error while reading timezone file". I used several, i.e. New York, Toronto,etc. with the same result. I use a HyperTerminal to print.
Any help?
Regards
George
The link tzdata_2010o.zip is dead. Somebody have for me an alternative location? I try to find something similar on the internet but nothing there.
Thanks in advance!
Hmm, this project seems to be dead. Maybe as reaction of this stupid timezone lawsuit. Fortunately, there is a clone to be found here. There is no ZIP available, you need to browse around for the file you need.
Just what I need!
Thanks for your quick reaction!
BTW a lawsuit about a timezone integration? How stupid is that.
Hmm, this project seems to be dead. Maybe as reaction of this stupid timezone lawsuit. Fortunately, there is a clone to be found here. There is no ZIP available, you need to browse around for the file you need.
I imported a timezone.csv file like this (header removed):
-9999-01-01T00:00:00Z,1893-03-31T23:06:32Z,3208,LMT,false
1893-03-31T23:06:32Z,1916-04-30T22:00:00Z,3600,CET,false
1916-04-30T22:00:00Z,1916-09-30T23:00:00Z,7200,CEST,true
1916-09-30T23:00:00Z,1917-04-16T01:00:00Z,3600,CET,false
1917-04-16T01:00:00Z,1917-09-17T01:00:00Z,7200,CEST,true
1917-09-17T01:00:00Z,1918-04-15T01:00:00Z,3600,CET,false
1918-04-15T01:00:00Z,1918-09-16T01:00:00Z,7200,CEST,true
and got the following error:
error while reading timezone file [timezone.csv]
time=13/14 15:20:49
This error happens when the file cannot be found. Did you set up a local file system as "/local"?
The code in question look like this:
FILE *fp = fopen("/local/timezone.csv", "r");
if (fp==NULL) {
printf("error while reading timezone file [timezone.csv]\n");
return;
}
As an additional note: try to comment out all the lines you don't need (e.g. everything before 2010 and after 2015), because otherwise you easily run out of memory.
the cvs file no longer exist. Can someone upload it to somewhere please??
As already noted, a copy can be found here.
Please log in to post comments.
Thanks! This saves me some time.