Arduino Forum

Forum 2005-2010 (read only) => Software => Interfacing => Topic started by: jm_wsb on Dec 06, 2010, 01:19 pm

Title: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 06, 2010, 01:19 pm
Hello,

I've a datalogger (mega2560 and ethernet/sd shield).
I've been able to sync the arduino time with a NTP server.

But an NTP server always gives UTC time. And I need local time.

Is there a module/code to convert the UTC to local, based on a timezone, keep in track with summertime ?
To write is self it nasty (summertime start at the last saturday of march and ends at the last saterday of oktober).

Thanks in advance,
Jeroen
Title: Re: getting local time over internet (NTP?)
Post by: webmeister on Dec 06, 2010, 02:22 pm
I am using worldtimeengine to get local time:
http://worldtimeengine.com
Title: Re: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 06, 2010, 05:07 pm
Hello Webmeister,

You have to request a key : is it free ?
Can you share your receiving/interpreting code for the .xml message ?

Thanks,
Jeroen
Title: Re: getting local time over internet (NTP?)
Post by: keeper63 on Dec 06, 2010, 06:24 pm
I may be misunderstanding your question/needs - but if you have a server that is synced to NTP, presumably it also has on it (or can be configured) to output "local time". Assuming the server is under your control, of course. So - can't you set a machine on your local network that gives you this information (I would think a simple linux box could easily do it - and for free)...?

:-?
Title: Re: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 06, 2010, 08:36 pm
I have myself no server, and I want to sync the arduino time with a timeserver on internet (fe NTP), but I need for arduino local with summertime.
Jeroen.
Title: Re: getting local time over internet (NTP?)
Post by: robtillaart on Dec 06, 2010, 09:50 pm
Hi Jeroen,

Quote
But an NTP server always gives UTC time. And I need local time.


Wrote this some time ago http://www.arduino.cc/playground/Main/DS1307OfTheLogshieldByMeansOfNTP and even if you don't have a RTC the library contains a DateTime class that does what you need.

Hopes this helps,
Title: Re: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 07, 2010, 06:11 pm
Hello Rob,

I see in your code:
Adjust timezone and DST... in my case add two hours

You just add 2 hours. What about if the DST changes. I would like to see this automatic change in code. But it is calender related (DST changes time in Europe the last sunday of the month oktober and back the last sunday of march...)

I need the code that does this checking for DST.

Any thoughts,
Jeroen
Title: Re: getting local time over internet (NTP?)
Post by: robtillaart on Dec 07, 2010, 07:09 pm
As far as I have searched I found no algorithm for DST. It differs per country / continent and if you read - http://en.wikipedia.org/wiki/Daylight_saving_time - you see it is not easy.

[edit]stumbled upon this one http://home-4.tiscali.nl/~t876506/TZworld.html[/edit]

In an application I made the logging had two timestamps, UTC for computers and a local time for humans. UTC will never shift (OK lapseconds not counted) No strange bumps/lines/jumps in Excel etc.

However if you find a good solution keep us informed!
Title: Re: getting local time over internet (NTP?)
Post by: westfw on Dec 08, 2010, 08:36 am
Quote
What about if the DST changes

You are doomed.  The (relatively) recent DST changes were all implemented by manually changing code.  Either you find a server you trust that gives you "local" time, or you subtract/add a manually entered offset.
Title: Re: getting local time over internet (NTP?)
Post by: robtillaart on Dec 08, 2010, 02:19 pm
Quote
What about if the DST changes ?
You are doomed

We need a sketch that can read the fingers(?) of an analog clock :) FUN!
Title: Re: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 08, 2010, 07:04 pm
I've found my solution.

for Europe fe. the DST starts on the last sunday 1:00 of march and ends on the last sunday of october.

You can calculate:
see this link http://www.webexhibits.org/daylightsaving/i.html
European Economic Community:
Begin DST: Sunday March (31 - (5*y/4 + 4) mod 7) at 1h U.T.
End DST: Sunday October (31 - (5*y/4 + 1) mod 7) at 1h U.T.
Since 1996, valid through 2099

so I put this in a function that gives the adjusting seconds towards UTC for the European TZ, depending on the current date:

int adjustDstEurope()
{
 // last sunday of march
 int beginDSTDate=  (31 - (5* year() /4 + 4) % 7);
 Serial.println(beginDSTDate);
 int beginDSTMonth=3;
 //last sunday of october
 int endDSTDate= (31 - (5 * year() /4 + 1) % 7);
 Serial.println(endDSTDate);
 int endDSTMonth=10;
 // DST is valid as:
 if (((month() > beginDSTMonth) && (month() < endDSTMonth))
     || ((month() == beginDSTMonth) && (day() >= beginDSTDate))
     || ((month() == endDSTMonth) && (day() <= endDSTDate)))
 return 7200;  // DST europe = utc +2 hour
 else return 3600; // nonDST europe = utc +1 hour
}

Isn't that nice ?

Best regards,
Jeroen
Title: Re: getting local time over internet (NTP?)
Post by: robtillaart on Dec 08, 2010, 11:14 pm
Very good searching Jeroen, Should be on the playground,

As info for the US is also on the site you might make your code more generic (so it can be added to Time lib or so)

instead  int adjustDstEurope() ..

I think something like

Code: [Select]
enum TZONE = { US, EUR } ;

int adjustDST(TZONE TZ)
{
...
}


BTW can endDSTDate expressed as a function of beginDSTDate? formulas look quite alike?

Furthermore You don't take into account the hour the clock is set back. In Netherlands it is always between 2 and 3 I don't know for other countries.

Still well done!
Title: Re: getting local time over internet (NTP?)
Post by: pantonvich on Dec 09, 2010, 04:09 am
your router might have a page with the time on it - mine does
Title: Re: getting local time over internet (NTP?)
Post by: jm_wsb on Dec 09, 2010, 05:28 pm
Rob,

I set the time sync as an alarm every dag at 2:00, so  normally it is synced with NTP (and automatically adjusted by the function above) at 2:00.

And indeed you can make this more generic, but I leave this for the expert.

Pantovich, my router doesn't, but still it can be a good solution.

Greetings,
Jeroen
Title: Re: getting local time over internet (NTP?)
Post by: MarkT on Dec 11, 2010, 12:14 am
Google must have a webservice that does this.