-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.pas
More file actions
68 lines (55 loc) · 1.38 KB
/
time.pas
File metadata and controls
68 lines (55 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
unit Time;
{$mode objfpc}{$H+}
interface
uses
Classes,
DateUtils,
{$ifdef MSWINDOWS}
windows,
{$endif}
{$ifdef UNIX}
unixutil,
{$endif}
SysUtils;
function GetBias: Integer;
function UniversalTimeToLocal(UT: TDateTime): TDateTime;
function LocalToUniversalTime(LT: TDateTime): TDateTime;
function LocalTimeToUnix(LocalTime: TDateTime): LongInt;
function UnixToLocalTime(UnixTime: LongInt): TDateTime;
implementation
function GetBias: Integer;
{$ifdef MSWINDOWS}
var
BiasType: Byte;
TZInfo: TTimeZoneInformation;
{$endif}
begin
{$ifdef MSWINDOWS}
BiasType := GetTimeZoneInformation(TZInfo);
case BiasType of
TIME_ZONE_ID_UNKNOWN: Result := TZInfo.Bias;
TIME_ZONE_ID_STANDARD: Result := TZInfo.Bias + TZInfo.StandardBias;
TIME_ZONE_ID_DAYLIGHT: Result := TZInfo.Bias + TZInfo.DaylightBias;
end;
{$endif}
{$ifdef UNIX}
Result := -Tzseconds div 60;
{$endif}
end;
function UniversalTimeToLocal(UT: TDateTime): TDateTime;
begin
Result := IncMinute(UT, -GetBias());
end;
function LocalToUniversalTime(LT: TDateTime): TDateTime;
begin
Result := IncMinute(LT, GetBias());
end;
function LocalTimeToUnix(LocalTime: TDateTime): LongInt;
begin
Result := DateTimeToUnix(LocalToUniversalTime(LocalTime));
end;
function UnixToLocalTime(UnixTime: LongInt): TDateTime;
begin
Result := UniversalTimeToLocal(UnixToDateTime(UnixTime));
end;
end.