Forum > General

[SOLVED] Getting the local time offset

(1/1)

CM630:
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.

ccrause:

--- Quote from: CM630 on July 29, 2021, 12:01:17 pm ---  offset:= GetLocalTimeOffset; results in "No symbol offset in current context".

--- End quote ---
Did you:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses sysutils;?

trev:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program time_offset; Uses sysutils; beginwriteln('Time offset: ', IntToStr(GetLocalTimeOffset));end.
trev@macmini8 [/Users/trev] $ ./time_offset
Time offset: -600

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

CM630:

--- Quote from: ccrause on July 29, 2021, 12:27:25 pm ---
--- Quote from: CM630 on July 29, 2021, 12:01:17 pm ---  offset:= GetLocalTimeOffset; results in "No symbol offset in current context".

--- End quote ---
Did you:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses sysutils;?

--- End quote ---
Yes.


--- Quote from: trev on July 29, 2021, 12:31:40 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program time_offset; Uses sysutils; beginwriteln('Time offset: ', IntToStr(GetLocalTimeOffset));end.
trev@macmini8 [/Users/trev] $ ./time_offset
Time offset: -600

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


--- End quote ---
This is not working for me. That is my code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;  {$mode objfpc}{$H+}  interface  uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, sntpsend,ShellApi,dos,DateUtils;  type    { TForm1 }    TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private    public    end;  var  Form1: TForm1;  implementation  {$R *.lfm}  { TForm1 }  procedure TForm1.Button1Click(Sender: TObject);var  WebTime: TDateTime;  offset: integer;begin  offset:= GetLocalTimeOffset;  ShowMessage (IntToStr(GetLocalTimeOffset));  with TSNTPSend.Create do try    TargetHost := 'ntp.amnic.net';    if GetNtp then    begin      WebTime:=UniversalTimeToLocal(NTPTime);            label1.caption := {FormatDateTime('hh:nn:ss',NTPTime) + '    :' + }FormatDateTime('hh:nn:ss',webtime) ;      SetTime(HourOf(NTPTime) + GetLocalTimeOffset,MinuteOf(NTPTime),SecondOf(NTPTime),0);    end    else      label1.caption := 'Failed to obtain NTP time';  finally    free;  end; //tryend;  end. 
I created a brand-new app. This works in it:



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);begin    ShowMessage (IntToStr(GetLocalTimeOffset));end;

CM630:
I do not know what was wrong. I have created a brand new project and now everything works just fine.


Thanks for the assistance!

Navigation

[0] Message Index

Go to full version