forked from SigmaSciences/mORMot-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.mORMot.Utils.pas
133 lines (110 loc) · 3.56 KB
/
Web.mORMot.Utils.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
unit Web.mORMot.Utils;
{-------------------------------------------------------------------------------
Adapted from mORMot v1 CrossPlatform units.
See original file for copyright and licence information at:
https://github.com/synopse/mORMot
-------------------------------------------------------------------------------}
interface
uses
SysUtils,
{$ifndef WEBLIB}
DateUtils,
{$endif}
JS,
Web,
Web.mORMot.Types,
Web.JSTypes;
/// compute a TTimeLog value from Delphi date/time type
function DateTimeToTTimeLog(Value: TDateTime): TTimeLog;
/// convert a TTimeLog value into the Delphi date/time type
function TTimeLogToDateTime(Value: TTimeLog): TDateTime;
/// compute the ISO-8601 JSON text representation of the current date/time value
// - e.g. "2015-06-27T20:59:29"
function NowToIso8601: string;
/// compute the unquoted ISO-8601 text representation of a date/time value
// - e.g. 'YYYY-MM-DD' 'Thh:mm:ss' or 'YYYY-MM-DDThh:mm:ss'
// - if Date is 0, will return ''
function DateTimeToIso8601(Value: TDateTime): string;
function toRawUtf8(Value: JSValue): RawUtf8; // If Value is not a string, returns ''
function toDouble(Value: JSValue): Double;
function Iso8601ToDateTime(Value: JSValue): TDateTime;
implementation
//------------------------------------------------------------------------------
function DateTimeToTTimeLog(Value: TDateTime): TTimeLog;
var
HH,MM,SS,MS,Y,M,D: Word;
//V: Int64;
begin
DecodeTime(Value, HH, MM, SS, MS);
DecodeDate(Value, Y, M, D);
//{$ifdef ISSMS} // JavaScript truncates to 32 bit binary
result := SS+MM*$40+(HH+D*$20+M*$400+Y*$4000-$420)*$1000;
//{$else}
//V := HH+D shl 5+M shl 10+Y shl 14-(1 shl 5+1 shl 10);
//result := SS+MM shl 6+V shl 12;
//{$endif}
end;
//------------------------------------------------------------------------------
function TTimeLogToDateTime(Value: TTimeLog): TDateTime;
var
Y: cardinal;
M: cardinal;
D: cardinal;
Time: TDateTime;
begin
//{$ifdef ISSMS} // JavaScript truncates to 32 bit binary
Y := (Value div $4000000) and 4095;
M := 1 + (Value shr (6+6+5+5)) and 15;
D := 1 + (Value shr (6+6+5)) and 31;
//{$else}
//Y := (Value shr (6+6+5+5+4)) and 4095;
//{$endif}
if (Y = 0) or not TryEncodeDate(Y, M, D, {DateTimeZone.UTC,} result) then
result := 0;
if (Value and (1 shl (6+6+5)-1)<>0) and
TryEncodeTime((Value shr (6+6)) and 31,
(Value shr 6) and 63,Value and 63, 0, Time) then
result := result + Time;
end;
//------------------------------------------------------------------------------
function NowToIso8601: string;
begin
result := DateTimeToIso8601(Now);
end;
//------------------------------------------------------------------------------
{$ifdef WEBLIB}
function DateTimeToIso8601(Value: TDateTime): string;
begin // e.g. YYYY-MM-DD Thh:mm:ss or YYYY-MM-DDThh:mm:ss
if Value=0 then
result := '' else
if frac(Value)=0 then
result := FormatDateTime('yyyy"-"mm"-"dd',Value) else
if trunc(Value)=0 then
result := FormatDateTime('"T"hh":"nn":"ss',Value) else
result := FormatDateTime('yyyy"-"mm"-"dd"T"hh":"nn":"ss',Value);
end;
{$else}
function DateTimeToIso8601(Value: TDateTime): string;
begin
Result := DateToISO8601(Value);
end;
{$endif}
function toRawUtf8(Value: JSValue): RawUtf8;
begin
if IsString(Value) then
Result:=String(Value)
else
Result:='';
end;
function toDouble(Value: JSValue): Double;
begin
Result := toNumber(Value);
end;
function Iso8601ToDateTime(Value: JSValue): TDateTime;
begin
if isNumber(Value) then
Result := toDouble(Value)
else
Result := ISO8601ToDate(toString(Value));
end;
end.