Go Down

Topic: getting local time over internet (NTP?) (Read 7918 times) previous topic - next topic

jm_wsb

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

Webmeister


jm_wsb

#2
Dec 06, 2010, 05:07 pm Last Edit: Dec 06, 2010, 05:07 pm by jm_wsb Reason: 1
Hello Webmeister,

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

Thanks,
Jeroen

cr0sh

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)...?

:-?
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

jm_wsb

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.

robtillaart

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,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jm_wsb

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

robtillaart

#7
Dec 07, 2010, 07:09 pm Last Edit: Dec 07, 2010, 07:13 pm by robtillaart Reason: 1
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!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

westfw

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.

robtillaart

#9
Dec 08, 2010, 02:19 pm Last Edit: Dec 08, 2010, 02:20 pm by robtillaart Reason: 1
Quote
What about if the DST changes ?
You are doomed

We need a sketch that can read the fingers(?) of an analog clock :) FUN!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jm_wsb

#10
Dec 08, 2010, 07:04 pm Last Edit: Dec 08, 2010, 07:06 pm by jm_wsb Reason: 1
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

robtillaart

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!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

pantonvich

#12
Dec 09, 2010, 04:09 am Last Edit: Dec 09, 2010, 04:10 am by pantonvich Reason: 1
your router might have a page with the time on it - mine does

jm_wsb

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

MarkT

Google must have a webservice that does this.
[ I won't respond to messages, use the forum please ]

Go Up