Recent

Author Topic: How to get local time and GMT time on linux  (Read 7190 times)

michastro

  • Newbie
  • Posts: 5
How to get local time and GMT time on linux
« on: April 10, 2017, 05:32:40 pm »
Hello,
I need to get local time and GMT time on Linux to get the difference between them and be able to make astronomical calculation.
With Windows it's with GetTimeZoneInformation.
Thanks
Michel

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: How to get local time and GMT time on linux
« Reply #1 on: April 10, 2017, 06:46:26 pm »
Cross platform in Dateutils:
LocalTimeToUniversal
UniversalTimeToLocal

See http://www.freepascal.org/docs-html/rtl/dateutils/localtimetouniversal.html
Code: Pascal  [Select][+][-]
  1. program timedemo;
  2. {$mode objfpc}
  3. uses sysutils,dateutils;
  4. var
  5.   Time:TDateTime; // store to get exact match-up
  6. begin
  7.   Time := Now;
  8.   writeln('Local:',formatdatetime('dd-mm-yyyy hh:nn:ss:zzz',Time));
  9.   writeln('UTC  :',formatdatetime('dd-mm-yyyy hh:nn:ss:zzz',LocalTimeToUniversal(Time)));
  10.   readln;
  11. end.
« Last Edit: April 10, 2017, 07:04:48 pm by Thaddy »
Specialize a type, not a var.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: How to get local time and GMT time on linux
« Reply #2 on: April 10, 2017, 07:04:36 pm »
[…]I need to get local time and GMT time on Linux to get the difference between them and be able to make astronomical calculation.[…]
Since you wrote you wanna perform some astronomical calculations: What exactly do you mean by “local time”?
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: How to get local time and GMT time on linux
« Reply #3 on: April 10, 2017, 07:06:48 pm »
[…]I need to get local time and GMT time on Linux to get the difference between them and be able to make astronomical calculation.[…]
Since you wrote you wanna perform some astronomical calculations: What exactly do you mean by “local time”?

Astronomical calculations are always UTC (formerly, and colloquially, known as GMT) based. E.g.f you want to know the sun/moon up/down times or visibility of a constellation etc. for your local time you will have to convert.
That goes even for the local time in Greenwich, London. To correct for daylight savings time.
Local time in computing is time offset to UTC corrected for daylight savings time if applicable in defined zones measured in steps of 1 hour to the center of a zone, although there are many politically defined exceptions to that.
From UTC time and your geo-location it is of course possible to get an even more exact "local" time, but that is a different matter.
One hour of exact time difference corresponds to 15° of longitude. I live in the Netherlands (local GMT+2) , but my real time zone is GMT+ only 36 minutes  O:-) :o
I live one degree north of Greenwich so now you know where I live  :D :)
« Last Edit: April 10, 2017, 09:08:00 pm by Thaddy »
Specialize a type, not a var.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: How to get local time and GMT time on linux
« Reply #4 on: April 11, 2017, 08:13:58 pm »
[…]Astronomical calculations are always UTC (formerly, and colloquially, known as GMT) based.
Yeah, however he asked for “local time”, too. As you noted, usually “local time” is not used for astronomical calculations. That confused me.
[…] my real time zone is GMT+ only 36 minutes  O:-) :o
I live one degree north of Greenwich so now you know where I live  […]
Miss! I hit water. … Sorry, I took a future map of the Netherlands. :o

PS: And I thought astronomers use “stardate”. :D
PPS: OK, now I really did the math. You're about 9°E? That's Germany. Am I missing some aspect? Or what do you mean by “real time zone”?
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018