forked from hgourvest/superobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supertimezone.pas
1514 lines (1410 loc) · 39 KB
/
supertimezone.pas
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit supertimezone;
{$IFDEF FPC}
{$mode Delphi}
{$ENDIF}
interface
uses
{$IFDEF MSWINDOWS}
Windows,
Registry,
{$ELSE}
System.Classes,
{$ENDIF}
System.SyncObjs,
SysUtils, Math, Generics.Collections,
supertypes;
type
{ TSuperTimeZone }
TSuperSystemTime = record
Year: Word;
Month: Word;
DayOfWeek: Word;
Day: Word;
Hour: Word;
Minute: Word;
Second: Word;
Millisecond: Word;
end;
{$IFDEF LINUX}
TSystemTime = TSuperSystemTime;
{$ENDIF}
TSuperTimeZone = class
private
const
TZ_TZI_KEY = '\SYSTEM\CurrentControlSet\Control\TimeZoneInformation'; { Vista and + }
TZ_KEY = '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\';
TZ_KEYNAME = 'TimeZoneKeyName';
private type
TExpirable<T> = record
Timestamp: TDateTime;
Data: T;
function IsValid: Boolean;
class function Box(Data: T): TExpirable<T>; static;
end;
private
FName: SOString;
FDaylightDisabled: TExpirable<Boolean>;
{$IFDEF MSWINDOWS}
FCacheTZI: TDictionary<Word, TExpirable<TTimeZoneInformation>>;
{$ENDIF}
function GetName: SOString;
{ Windows Internals }
{$IFDEF MSWINDOWS}
function TzSpecificLocalTimeToSystemTime(
const lpTimeZoneInformation: PTimeZoneInformation;
var lpLocalTime, lpUniversalTime: TSystemTime): BOOL;
function SystemTimeToTzSpecificLocalTime(
const lpTimeZoneInformation: PTimeZoneInformation;
var lpUniversalTime, lpLocalTime: TSystemTime): BOOL;
function GetTimezoneBias(const pTZinfo: PTimeZoneInformation;
lpFileTime: PFileTime; islocal: Boolean; pBias: PLongint): Boolean;
function CompTimeZoneID(const pTZinfo: PTimeZoneInformation;
lpFileTime: PFileTime; IsLocal: Boolean): FixedUInt;
function DayLightCompareDate(const date: PSystemTime;
const compareDate: PSystemTime): Integer;
{$ENDIF}
private
class constructor Init;
class destructor Finish;
class var FCacheCS: TCriticalSection;
class var FCache: TObjectDictionary<string, TSuperTimeZone>;
class function GetSuperTimeZoneInstance(const Name: string): TSuperTimeZone; static;
class function GetLocalSuperTimeZoneInstance: TSuperTimeZone; static;
public
constructor Create(const TimeZoneName: SOString = '');
destructor Destroy; override;
{ ISO8601 formatted date Parser }
class function ParseISO8601Date(const ISO8601Date: SOString;
var st: TSystemTime; var dayofyear: Integer; var week: Word; var bias: Integer;
var havetz, havedate: Boolean): Boolean;
{ Conversions }
function LocalToUTC(const DelphiDateTime: TDateTime): TDateTime;
function UTCToLocal(const DelphiDateTime: TDateTime): TDateTime;
function JavaToDelphi(const JavaDateTime: Int64): TDateTime;
function DelphiToJava(const DelphiDateTime: TDateTime): Int64;
function JavaToISO8601(JavaDateTime: Int64): string;
function DelphiToISO8601(DelphiDateTime: TDateTime): string;
function ISO8601ToJava(const ISO8601Date: string; out JavaDateTime: Int64): Boolean;
function ISO8601ToDelphi(const ISO8601Date: string; out DelphiDateTime: TDateTime): Boolean;
{ TZ Info }
class function GetCurrentTimeZone: SOString;
{$IFDEF MSWINDOWS}
function GetTimeZoneInformation(Year: Word; var TZI: TTimeZoneInformation): Boolean;
{$ENDIF}
function GetDaylightDisabled: Boolean;
property Name: SOString read GetName;
{ Builder }
class property Local: TSuperTimeZone read GetLocalSuperTimeZoneInstance;
class property Zone[const TimeZoneName: string]: TSuperTimeZone read GetSuperTimeZoneInstance;
end;
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
(* NOT DST Aware *)
{ Windows 2000+ }
function _SystemTimeToTzSpecificLocalTime(
lpTimeZoneInformation: PTimeZoneInformation;
var lpUniversalTime, lpLocalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'SystemTimeToTzSpecificLocalTime'{$IFNDEF FPC} delayed{$ENDIF};
{ Windows XP+ }
function _TzSpecificLocalTimeToSystemTime(
lpTimeZoneInformation: PTimeZoneInformation;
var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'TzSpecificLocalTimeToSystemTime'{$IFNDEF FPC} delayed{$ENDIF};
(* EXtended version - DST Aware *)
{ Windows 7+ }
function _TzSpecificLocalTimeToSystemTimeEx(
const lpTimeZoneInformation: PDynamicTimeZoneInformation;
const lpLocalTime: PSystemTime; var lpUniversalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'TzSpecificLocalTimeToSystemTimeEx'{$IFNDEF FPC} delayed{$ENDIF};
{ Windows 7+ }
function _SystemTimeToTzSpecificLocalTimeEx(
const lpTimeZoneInformation: PDynamicTimeZoneInformation;
const lpUniversalTime: PSystemTime; var lpLocalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'SystemTimeToTzSpecificLocalTimeEx'{$IFNDEF FPC} delayed{$ENDIF};
{ Convert Local <=> UTC for specific time-zones using the Windows API only. NOT Guaranteed to work }
function _ConvertLocalDateTimeToUTC(const TimeZoneName: SOString;
const Local: TDateTime; var UTC: TDateTime): Boolean;
function _ConvertUTCDateTimeToLocal(const TimeZoneName: SOString;
const UTC: TDateTime; var Local: TDateTime): Boolean;
{$WARN SYMBOL_PLATFORM ON}
{$ENDIF}
implementation
{$IFDEF MSWINDOWS}
{ Convert Local -> UTC for specific time-zones using the Windows API only. NOT Guaranteed to work }
function _ConvertLocalDateTimeToUTC(const TimeZoneName: SOString;
const Local: TDateTime; var UTC: TDateTime): Boolean;
{$IFNDEF FPC}
var
DTZI: TDynamicTimeZoneInformation;
local_st, utc_st: TSystemTime;
{$ENDIF}
begin
if not CheckWin32Version(6, 1) then
begin
Result := False;
Exit;
end;
{$IFNDEF FPC}
{ We work with system times }
DateTimeToSystemTime(Local, local_st);
{ Get current Dynamic TimeZone Information }
FillChar(DTZI, SizeOf(TDynamicTimeZoneInformation), 0);
GetDynamicTimeZoneInformation(DTZI);
{ Replaces the TimeZoneKeyName member with specified TimeZoneName }
Move(TimeZoneName[1], DTZI.TimeZoneKeyName, (Length(TimeZoneName) + 1) * SizeOf(SOChar));
{ Retrieves the TimeZoneInformation structure and convert the local time to utc }
if _TzSpecificLocalTimeToSystemTimeEx(@DTZI, @local_st, utc_st) then
begin
{ We really want Delphi TDateTime }
UTC := SystemTimeToDateTime(utc_st);
Result := True;
end
else
Result := False;
{$ENDIF}
end;
{ Convert UTC -> Local for specific time-zones using the Windows API only. NOT Guaranteed to work }
function _ConvertUTCDateTimeToLocal(const TimeZoneName: SOString;
const UTC: TDateTime; var Local: TDateTime): Boolean;
{$IFNDEF FPC}
var
DTZI: TDynamicTimeZoneInformation;
utc_st, local_st: TSystemTime;
{$ENDIF}
begin
if not CheckWin32Version(6, 1) then
begin
Result := False;
Exit;
end;
{$IFNDEF FPC}
{ We work with system times }
DateTimeToSystemTime(UTC, utc_st);
{ Get current Dynamic TimeZone Information }
FillChar(DTZI, SizeOf(TDynamicTimeZoneInformation), 0);
GetDynamicTimeZoneInformation(DTZI);
{ Replaces the TimeZoneKeyName member with specified TimeZoneName }
Move(TimeZoneName[1], DTZI.TimeZoneKeyName[0], Length(TimeZoneName) * SizeOf(SOChar));
{ Retrieves the TimeZoneInformation structure and convert the local time to utc }
if _SystemTimeToTzSpecificLocalTimeEx(@DTZI, @utc_st, local_st) then
begin
{ We really want Delphi TDateTime }
Local := SystemTimeToDateTime(local_st);
Result := True;
end
else
Result := False;
{$ENDIF}
end;
{$ENDIF}
{ TSuperDate }
class constructor TSuperTimeZone.Init;
begin
FCacheCS := TCriticalSection.Create;
FCache := TObjectDictionary<string, TSuperTimeZone>.Create([doOwnsValues]);
end;
class destructor TSuperTimeZone.Finish;
begin
FCache.Free;
FCacheCS.Free;
end;
class function TSuperTimeZone.GetSuperTimeZoneInstance(
const Name: string): TSuperTimeZone;
begin
FCacheCS.Enter;
try
if not FCache.TryGetValue(Name, Result) then
begin
Result := TSuperTimeZone.Create(Name);
FCache.Add(Name, Result);
end;
finally
FCacheCS.Leave;
end;
end;
class function TSuperTimeZone.GetLocalSuperTimeZoneInstance: TSuperTimeZone;
begin
Result := TSuperTimeZone.GetSuperTimeZoneInstance('');
end;
constructor TSuperTimeZone.Create(const TimeZoneName: SOString);
begin
inherited Create;
FName := TimeZoneName;
FDaylightDisabled := TExpirable<Boolean>.Box(GetDaylightDisabled);
{$IFDEF MSWINDOWS}
FCacheTZI := TDictionary<Word, TExpirable<TTimeZoneInformation>>.Create;
{$ENDIF}
end;
destructor TSuperTimeZone.Destroy;
begin
{$IFDEF MSWINDOWS}
FCacheTZI.Free;
{$ENDIF}
end;
function TSuperTimeZone.LocalToUTC(const DelphiDateTime: TDateTime): TDateTime;
{$IFDEF MSWINDOWS}
var
local, utc: TSystemTime;
tzi: TTimeZoneInformation;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
DateTimeToSystemTime(DelphiDateTime, local);
if GetTimeZoneInformation(local.wYear, tzi) and TzSpecificLocalTimeToSystemTime(@tzi, local, utc) then
Result := SystemTimeToDateTime(utc)
else
{$ENDIF}
Result := DelphiDateTime;
end;
function TSuperTimeZone.UTCToLocal(const DelphiDateTime: TDateTime): TDateTime;
{$IFDEF MSWINDOWS}
var
utc, local: TSystemTime;
tzi: TTimeZoneInformation;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
DateTimeToSystemTime(DelphiDateTime, utc);
if GetTimeZoneInformation(utc.wYear, tzi) and SystemTimeToTzSpecificLocalTime(@tzi, utc, local) then
Result := SystemTimeToDateTime(local)
else
{$ENDIF}
Result := DelphiDateTime;
end;
function TSuperTimeZone.DelphiToJava(const DelphiDateTime: TDateTime): Int64;
{$IFDEF MSWINDOWS}
var
local, utc, st: TSystemTime;
tzi: TTimeZoneInformation;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
DateTimeToSystemTime(DelphiDateTime, local);
if GetTimeZoneInformation(local.wYear, tzi) and TzSpecificLocalTimeToSystemTime(@tzi, local, utc) then
st := utc
else
st := local;
Result := Round((SystemTimeToDateTime(st) - 25569) * 86400000);
{$ENDIF}
end;
function TSuperTimeZone.JavaToDelphi(const JavaDateTime: Int64): TDateTime;
{$IFDEF MSWINDOWS}
var
utc, local: TSystemTime;
tzi: TTimeZoneInformation;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
DateTimeToSystemTime(25569 + (JavaDateTime / 86400000), utc);
if GetTimeZoneInformation(utc.wYear, tzi) and SystemTimeToTzSpecificLocalTime(@tzi, utc, local) then
Result := SystemTimeToDateTime(local)
else
Result := SystemTimeToDateTime(utc);
{$ENDIF}
end;
function TSuperTimeZone.DelphiToISO8601(
DelphiDateTime: TDateTime): string;
const
ISO_Fmt = '%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%d';
TZ_Fmt = '%s%.2d:%.2d';
var
{$IFDEF MSWINDOWS}
local, utc: TSystemTime;
tzi: TTimeZoneInformation;
bias: TDateTime;
h, m, d: Word;
{$ENDIF}
iso: SOString;
begin
{$IFDEF MSWINDOWS}
DateTimeToSystemTime(DelphiDateTime, local);
iso := Format(ISO_Fmt, [
local.wYear, local.wMonth, local.wDay,
local.wHour, local.wMinute, local.wSecond, local.wMilliseconds]);
if GetTimeZoneInformation(local.wYear, tzi) and TzSpecificLocalTimeToSystemTime(@tzi, local, utc) then
begin
bias := SystemTimeToDateTime(local) - SystemTimeToDateTime(utc);
DecodeTime(bias, h, m, d, d);
case Sign(bias) of
-1: Result := iso + Format(TZ_Fmt, [ '-', h, m ]);
0: Result := iso + 'Z';
+1: Result := iso + Format(TZ_Fmt, [ '+', h, m ]);
end;
end
else
{$ENDIF}
Result := iso;
end;
function TSuperTimeZone.JavaToISO8601(JavaDateTime: Int64): string;
begin
Result := DelphiToISO8601(JavaToDelphi(JavaDateTime));
end;
function TSuperTimeZone.ISO8601ToDelphi(const ISO8601Date: string; out DelphiDateTime: TDateTime): Boolean;
var
JavaDateTime: Int64;
begin
Result := ISO8601ToJava(ISO8601Date, JavaDateTime);
if Result then
DelphiDateTime := JavaToDelphi(JavaDateTime);
end;
function TSuperTimeZone.ISO8601ToJava(const ISO8601Date: string;
out JavaDateTime: Int64): Boolean;
{$IFDEF MSWINDOWS}
var
st: TSystemTime;
dayofyear: Integer;
week: Word;
bias: Integer;
havetz, havedate: Boolean;
tzi: TTimeZoneInformation;
utc: TSystemTime;
m: Word;
DayTable: PDayTable;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
if ParseISO8601Date(ISO8601Date, st, dayofyear, week, bias, havetz, havedate) then
begin
if (not havetz) and GetTimeZoneInformation(st.wYear, tzi) and TzSpecificLocalTimeToSystemTime(@tzi, st, utc) then
bias := Round((SystemTimeToDateTime(utc) - SystemTimeToDateTime(st)) * MinsPerDay);
JavaDateTime := st.wMilliseconds + st.wSecond * 1000 + (st.wMinute + bias) * 60000 + st.wHour * 3600000;
if havedate then
begin
DayTable := @MonthDays[IsLeapYear(st.wYear)];
if st.wMonth <> 0 then
begin
if not (st.wMonth in [1..12]) or (DayTable^[st.wMonth] < st.wDay) then
begin
Result := False;
Exit;
end;
for m := 1 to st.wMonth - 1 do
Inc(JavaDateTime, Int64(DayTable^[m]) * 86400000);
end;
Dec(st.wYear);
Inc(JavaDateTime, Int64(
(st.wYear * 365) + (st.wYear div 4) - (st.wYear div 100) +
(st.wYear div 400) + st.wDay + dayofyear - 719163) * 86400000);
end;
Result := True;
end
else
{$ENDIF}
Result := False;
end;
function TSuperTimeZone.GetName: SOString;
begin
if FName <> '' then
Result := FName
else
Result := GetCurrentTimeZone;
end;
class function TSuperTimeZone.GetCurrentTimeZone: SOString;
begin
{$IFDEF MSWINDOWS}
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly(TZ_TZI_KEY) and ValueExists(TZ_KEYNAME) then
{ Windows Vista+ }
Result := Trim(ReadString(TZ_KEYNAME))
else
begin
{ Windows 2000/XP }
CloseKey;
RootKey := HKEY_CURRENT_USER;
if OpenKeyReadOnly(TZ_KEY) and ValueExists(TZ_KEYNAME) then
Result := Trim(ReadString(TZ_KEYNAME))
else
begin
CloseKey;
RootKey := HKEY_USERS;
if OpenKeyReadOnly('.DEFAULT\' + TZ_KEY) and ValueExists(TZ_KEYNAME) then
Result := Trim(ReadString(TZ_KEYNAME))
else
Result := '';
end;
end;
finally
CloseKey;
Free;
end;
{$ELSE}
Result := '';
{$ENDIF}
end;
function TSuperTimeZone.GetDaylightDisabled: Boolean;
var
KeyName: SOString;
begin
if FDaylightDisabled.IsValid then
Exit(FDaylightDisabled.Data);
Result := False;
KeyName := TZ_KEY + Name;
{$IFDEF MSWINDOWS}
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly(KeyName) then
begin
if ValueExists('IsObsolete') then
Result := ReadBool('IsObsolete');
CloseKey;
end;
FDaylightDisabled := TExpirable<Boolean>.Box(Result);
finally
Free;
end;
{$ENDIF}
end;
{$IFDEF MSWINDOWS}
function TSuperTimeZone.GetTimeZoneInformation(Year: Word;
var TZI: TTimeZoneInformation): Boolean;
type
TRegistryTZI = packed record
Bias: LongInt;
StandardBias: LongInt;
DaylightBias: LongInt;
StandardChangeTime: TSystemTime;
DaylightChangeTime: TSystemTime;
end;
var
RegTZI: TRegistryTZI;
KeyName: SOString;
FirstYear, LastYear, ChangeYear: Word;
Retry: Boolean;
CachedData: TExpirable<TTimeZoneInformation>;
begin
if FCacheTZI.TryGetValue(Year, CachedData) and CachedData.IsValid then
begin
TZI := CachedData.Data;
Exit(True);
end;
FillChar(TZI, SizeOf(TZI), 0);
KeyName := TZ_KEY + Name;
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if not KeyExists(KeyName) then
begin
Result := False;
Exit;
end;
ChangeYear := 0;
if OpenKeyReadOnly(KeyName + '\Dynamic DST') then
try
FirstYear := ReadInteger('FirstEntry');
LastYear := ReadInteger('LastEntry');
if (Year >= FirstYear) and (Year <= LastYear) then
ChangeYear := Year
else
ChangeYear := 0;
Retry := False;
repeat
while (ChangeYear > 0) and (not ValueExists(IntToStr(ChangeYear))) do
begin
Dec(ChangeYear);
if ChangeYear < FirstYear then
ChangeYear := 0;
end;
if ChangeYear > 0 then
begin
ReadBinaryData(IntToStr(ChangeYear), RegTZI, SizeOf(TRegistryTZI));
if RegTZI.DaylightChangeTime.wMonth > RegTZI.StandardChangeTime.wMonth then
begin
Dec(ChangeYear);
Retry := not Retry;
end;
end;
until not Retry;
finally
CloseKey;
end;
if (ChangeYear = 0) and OpenKeyReadOnly(KeyName) then
try
ReadBinaryData('TZI', RegTZI, SizeOf(TRegistryTZI));
finally
CloseKey;
end;
TZI.Bias := RegTZI.Bias;
TZI.StandardDate := RegTZI.StandardChangeTime;
TZI.StandardBias := RegTZI.StandardBias;
TZI.DaylightDate := RegTZI.DaylightChangeTime;
TZI.DaylightBias := RegTZI.DaylightBias;
Result := True;
FCacheTZI.AddOrSetValue(Year, TExpirable<TTimeZoneInformation>.Box(TZI));
finally
Free;
end;
end;
function TSuperTimeZone.TzSpecificLocalTimeToSystemTime(
const lpTimeZoneInformation: PTimeZoneInformation;
var lpLocalTime, lpUniversalTime: TSystemTime): BOOL;
var
ft: TFileTime;
lBias: LongInt;
t: Int64;
begin
Assert(lpTimeZoneInformation <> nil);
if (not SystemTimeToFileTime(lpLocalTime, ft)) then
begin
Result := False;
Exit;
end;
t := PInt64(@ft)^;
if (not GetTimezoneBias(lpTimeZoneInformation, @ft, True, @lBias)) then
begin
Result := False;
Exit;
end;
(* convert minutes to 100-nanoseconds-ticks *)
Inc(t, Int64(lBias) * 600000000);
PInt64(@ft)^ := t;
Result := FileTimeToSystemTime(ft, lpUniversalTime);
end;
function TSuperTimeZone.GetTimezoneBias(const pTZinfo: PTimeZoneInformation;
lpFileTime: PFileTime; islocal: Boolean; pBias: PLongint): Boolean;
var
bias: LongInt;
tzid: FixedUInt;
begin
bias := pTZinfo^.Bias;
tzid := CompTimeZoneID(pTZinfo, lpFileTime, islocal);
if( tzid = TIME_ZONE_ID_INVALID) then
begin
Result := False;
Exit;
end;
if (tzid = TIME_ZONE_ID_DAYLIGHT) then
Inc(bias, pTZinfo^.DaylightBias)
else if (tzid = TIME_ZONE_ID_STANDARD) then
Inc(bias, pTZinfo^.StandardBias);
pBias^ := bias;
Result := True;
end;
function TSuperTimeZone.CompTimeZoneID(const pTZinfo: PTimeZoneInformation;
lpFileTime: PFileTime; IsLocal: Boolean): FixedUInt;
var
Ret: Integer;
BeforeStandardDate, AfterDaylightDate: Boolean;
llTime: Int64;
SysTime: TSystemTime;
ftTemp: TFileTime;
year: Word;
begin
llTime := 0;
if (not GetDaylightDisabled) and (pTZinfo^.DaylightDate.wMonth <> 0) then
begin
(* if year is 0 then date is in day-of-week format, otherwise
* it's absolute date.
*)
if ((pTZinfo^.StandardDate.wMonth = 0) or
((pTZinfo^.StandardDate.wYear = 0) and
((pTZinfo^.StandardDate.wDay < 1) or
(pTZinfo^.StandardDate.wDay > 5) or
(pTZinfo^.DaylightDate.wDay < 1) or
(pTZinfo^.DaylightDate.wDay > 5)))) then
begin
SetLastError(ERROR_INVALID_PARAMETER);
Result := TIME_ZONE_ID_INVALID;
Exit;
end;
if (not IsLocal) then
begin
llTime := PInt64(lpFileTime)^;
Dec(llTime, Int64(pTZinfo^.Bias) * 600000000);
PInt64(@ftTemp)^ := llTime;
lpFileTime := @ftTemp;
end;
FileTimeToSystemTime(lpFileTime^, SysTime);
year := SysTime.wYear;
if (not IsLocal) then
begin
Dec(llTime, Int64(pTZinfo^.DaylightBias) * 600000000);
PInt64(@ftTemp)^ := llTime;
FileTimeToSystemTime(lpFileTime^, SysTime);
end;
(* check for daylight savings *)
if (year = SysTime.wYear) then
begin
ret := DayLightCompareDate(@SysTime, @pTZinfo.StandardDate);
if (ret = -2) then
begin
Result := TIME_ZONE_ID_INVALID;
Exit;
end;
beforeStandardDate := ret < 0;
end else
beforeStandardDate := SysTime.wYear < year;
if (not islocal) then
begin
Dec(llTime, (pTZinfo.StandardBias - pTZinfo.DaylightBias) * 600000000);
PInt64(@ftTemp)^ := llTime;
FileTimeToSystemTime(lpFileTime^, SysTime);
end;
if (year = SysTime.wYear) then
begin
ret := DayLightCompareDate(@SysTime, @pTZinfo.DaylightDate);
if (ret = -2) then
begin
Result := TIME_ZONE_ID_INVALID;
Exit;
end;
afterDaylightDate := ret >= 0;
end else
afterDaylightDate := SysTime.wYear > year;
Result := TIME_ZONE_ID_STANDARD;
if pTZinfo^.DaylightDate.wMonth < pTZinfo^.StandardDate.wMonth then
begin
(* Northern hemisphere *)
if BeforeStandardDate and AfterDaylightDate then
Result := TIME_ZONE_ID_DAYLIGHT;
end
else
begin
(* Down south *)
if BeforeStandardDate or AfterDaylightDate then
Result := TIME_ZONE_ID_DAYLIGHT;
end;
end
else
(* No transition date *)
Result := TIME_ZONE_ID_UNKNOWN;
end;
function TSuperTimeZone.DayLightCompareDate(const date, compareDate: PSystemTime): Integer;
var
limit_day, dayinsecs, weekofmonth: Integer;
First: Word;
begin
if (date^.wMonth < compareDate^.wMonth) then
begin
Result := -1; (* We are in a month before the date limit. *)
Exit;
end;
if (date^.wMonth > compareDate^.wMonth) then
begin
Result := 1; (* We are in a month after the date limit. *)
Exit;
end;
(* if year is 0 then date is in day-of-week format, otherwise
* it's absolute date.
*)
if (compareDate^.wYear = 0) then
begin
(* compareDate.wDay is interpreted as number of the week in the month
* 5 means: the last week in the month *)
weekofmonth := compareDate^.wDay;
(* calculate the day of the first DayOfWeek in the month *)
First := (6 + compareDate^.wDayOfWeek - date^.wDayOfWeek + date^.wDay) mod 7 + 1;
limit_day := First + 7 * (weekofmonth - 1);
(* check needed for the 5th weekday of the month *)
if (limit_day > MonthDays[(date^.wMonth=2) and IsLeapYear(date^.wYear)][date^.wMonth]) then
Dec(limit_day, 7);
end
else
limit_day := compareDate^.wDay;
(* convert to seconds *)
limit_day := ((limit_day * 24 + compareDate^.wHour) * 60 + compareDate^.wMinute ) * 60;
dayinsecs := ((date^.wDay * 24 + date^.wHour) * 60 + date^.wMinute ) * 60 + date^.wSecond;
(* and compare *)
if dayinsecs < limit_day then
Result := -1
else if dayinsecs > limit_day then
Result := 1
else
Result := 0; (* date is equal to the date limit. *)
end;
function TSuperTimeZone.SystemTimeToTzSpecificLocalTime(
const lpTimeZoneInformation: PTimeZoneInformation;
var lpUniversalTime, lpLocalTime: TSystemTime): BOOL;
var
ft: TFileTime;
lBias: LongInt;
llTime: Int64;
begin
Assert(lpTimeZoneInformation <> nil);
if (not SystemTimeToFileTime(lpUniversalTime, ft)) then
begin
Result := False;
Exit;
end;
llTime := PInt64(@ft)^;
if (not GetTimezoneBias(lpTimeZoneInformation, @ft, False, @lBias)) then
begin
Result := False;
Exit;
end;
(* convert minutes to 100-nanoseconds-ticks *)
Dec(llTime, Int64(lBias) * 600000000);
PInt64(@ft)^ := llTime;
Result := FileTimeToSystemTime(ft, lpLocalTime);
end;
{$ENDIF}
class function TSuperTimeZone.ParseISO8601Date(const ISO8601Date: SOString;
var st: TSystemTime; var dayofyear: Integer; var week: Word;
var bias: Integer; var havetz, havedate: Boolean): Boolean;
function get(var v: Word; c: SOChar): Boolean; {$IFDEF HAVE_INLINE} inline; {$ENDIF}
begin
if (c < #256) and (AnsiChar(c) in ['0' .. '9']) then
begin
Result := True;
v := v * 10 + Ord(c) - Ord('0');
end
else
Result := False;
end;
type
TState = (stStart, stYear, stMonth, stWeek, stWeekDay, stDay, stDayOfYear,
stHour, stMin, stSec, stMs, stUTC, stGMTH, stGMTM, stGMTend, stEnd);
TPerhaps = (yes, no, perhaps);
var
p: PSOChar;
sep: TPerhaps;
state: TState;
pos, v: Word;
inctz: Boolean;
st1: TSuperSystemTime absolute st;
label
error;
begin
p := PSOChar(ISO8601Date);
sep := perhaps;
state := stStart;
pos := 0;
inctz := False;
FillChar(st, SizeOf(st), 0);
dayofyear := 0;
week := 0;
bias := 0;
havedate := True;
havetz := False;
while True do
case state of
stStart:
case p^ of
'0' .. '9':
state := stYear;
'T', 't':
begin
state := stHour;
pos := 0;
Inc(p);
havedate := False;
end;
else
goto error;
end;
stYear:
case pos of
0 .. 1, 3:
if get(st1.Year, p^) then
begin
Inc(pos);
Inc(p);
end
else
goto error;
2:
case p^ of
'0' .. '9':
begin
st1.Year := st1.Year * 10 + Ord(p^) - Ord('0');
Inc(pos);
Inc(p);
end;
':':
begin
havedate := False;
st1.Hour := st1.Year;
st1.Year := 0;
Inc(p);
pos := 0;
state := stMin;
sep := yes;
end;
else
goto error;
end;
4:
case p^ of
'-':
begin
pos := 0;
Inc(p);
sep := yes;
state := stMonth;
end;
'0' .. '9':
begin
sep := no;
pos := 0;
state := stMonth;
end;
'W', 'w':
begin
pos := 0;
Inc(p);
state := stWeek;
end;
'T', 't', ' ':
begin
state := stHour;
pos := 0;
Inc(p);
st1.Month := 1;
st1.Day := 1;
end;
#0:
begin
st1.Month := 1;
st1.Day := 1;
state := stEnd;
end;
else
goto error;
end;
end;
stMonth:
case pos of
0:
case p^ of
'0' .. '9':
begin
st1.Month := Ord(p^) - Ord('0');
Inc(pos);
Inc(p);
end;
'W', 'w':
begin
pos := 0;
Inc(p);
state := stWeek;
end;
else
goto error;
end;
1:
if get(st1.Month, p^) then
begin
Inc(pos);
Inc(p);
end
else
goto error;
2:
case p^ of
'-':
if (sep in [yes, perhaps]) then
begin
pos := 0;
Inc(p);
state := stDay;
sep := yes;
end
else
goto error;
'0' .. '9':
if sep in [no, perhaps] then
begin