Pages: [1]   Go Down
Author Topic: getting local time over internet (NTP?)  (Read 6564 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

CH
Offline Offline
God Member
*****
Karma: 19
Posts: 703
Book Writer "Arduino Praxiseinstieg"
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using worldtimeengine to get local time:
http://worldtimeengine.com
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Webmeister,

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

Thanks,
Jeroen
« Last Edit: December 06, 2010, 11:07:56 am by jm_wsb » Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5551
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 :-?
Logged

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Logged

Rob Tillaart

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: December 07, 2010, 01:13:33 pm by robtillaart » Logged

Rob Tillaart

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What about if the DST changes ?
You are doomed
We need a sketch that can read the fingers(?) of an analog clock smiley FUN!
« Last Edit: December 08, 2010, 08:20:14 am by robtillaart » Logged

Rob Tillaart

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: December 08, 2010, 01:06:38 pm by jm_wsb » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!
Logged

Rob Tillaart

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

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

your router might have a page with the time on it - mine does
« Last Edit: December 08, 2010, 10:10:24 pm by pantonvich » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google must have a webservice that does this.
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: