Recent

Author Topic: [SOLVED] Getting the local time offset  (Read 1776 times)

CM630

  • Hero Member
  • *****
  • Posts: 1090
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] Getting the local time offset
« on: July 29, 2021, 12:01:17 pm »
I get the time from a web server and I want to convert it to local time.


  offset:= GetLocalTimeOffset; results in "No symbol offset in current context".

  ShowMessage (IntToStr(GetLocalTimeOffset)); shows no messagebox at all.

Shall I dig through the registries to get the local time offset? I expect that a non-OS dependent way exists.
« Last Edit: July 29, 2021, 12:48:12 pm by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Getting the local time offset
« Reply #1 on: July 29, 2021, 12:27:25 pm »
  offset:= GetLocalTimeOffset; results in "No symbol offset in current context".
Did you:
Code: Pascal  [Select][+][-]
  1. uses sysutils;
?

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Getting the local time offset
« Reply #2 on: July 29, 2021, 12:31:40 pm »
Code: Pascal  [Select][+][-]
  1. program time_offset;
  2.  
  3. Uses sysutils;
  4.  
  5. begin
  6. writeln('Time offset: ', IntToStr(GetLocalTimeOffset));
  7. end.

trev@macmini8 [/Users/trev] $ ./time_offset
Time offset: -600

Granted I tested this in macOS + FreeBSD, but it should be cross platform.

CM630

  • Hero Member
  • *****
  • Posts: 1090
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Getting the local time offset
« Reply #3 on: July 29, 2021, 12:38:17 pm »
  offset:= GetLocalTimeOffset; results in "No symbol offset in current context".
Did you:
Code: Pascal  [Select][+][-]
  1. uses sysutils;
?
Yes.

Code: Pascal  [Select][+][-]
  1. program time_offset;
  2.  
  3. Uses sysutils;
  4.  
  5. begin
  6. writeln('Time offset: ', IntToStr(GetLocalTimeOffset));
  7. end.

trev@macmini8 [/Users/trev] $ ./time_offset
Time offset: -600

Granted I tested this in macOS + FreeBSD, but it should be cross platform.

This is not working for me. That is my code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6.  
  7. interface
  8.  
  9.  
  10. uses
  11.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, sntpsend,ShellApi,dos,DateUtils;
  12.  
  13.  
  14. type
  15.  
  16.  
  17.   { TForm1 }
  18.  
  19.  
  20.   TForm1 = class(TForm)
  21.     Button1: TButton;
  22.     Label1: TLabel;
  23.     procedure Button1Click(Sender: TObject);
  24.   private
  25.  
  26.  
  27.   public
  28.  
  29.  
  30.   end;
  31.  
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36.  
  37. implementation
  38.  
  39.  
  40. {$R *.lfm}
  41.  
  42.  
  43. { TForm1 }
  44.  
  45.  
  46. procedure TForm1.Button1Click(Sender: TObject);
  47. var
  48.   WebTime: TDateTime;
  49.   offset: integer;
  50. begin
  51.   offset:= GetLocalTimeOffset;
  52.   ShowMessage (IntToStr(GetLocalTimeOffset));
  53.   with TSNTPSend.Create do try
  54.     TargetHost := 'ntp.amnic.net';
  55.     if GetNtp then
  56.     begin
  57.       WebTime:=UniversalTimeToLocal(NTPTime);
  58.             label1.caption := {FormatDateTime('hh:nn:ss',NTPTime) + '    :' + }FormatDateTime('hh:nn:ss',webtime) ;
  59.       SetTime(HourOf(NTPTime) + GetLocalTimeOffset,MinuteOf(NTPTime),SecondOf(NTPTime),0);
  60.     end
  61.     else
  62.       label1.caption := 'Failed to obtain NTP time';
  63.   finally
  64.     free;
  65.   end; //try
  66. end;
  67.  
  68.  
  69. end.
  70.  

I created a brand-new app. This works in it:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.     ShowMessage (IntToStr(GetLocalTimeOffset));
  4. end;
« Last Edit: July 29, 2021, 12:42:01 pm by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

CM630

  • Hero Member
  • *****
  • Posts: 1090
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Getting the local time offset
« Reply #4 on: July 29, 2021, 12:47:40 pm »
I do not know what was wrong. I have created a brand new project and now everything works just fine.


Thanks for the assistance!
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

 

TinyPortal © 2005-2018