-
Notifications
You must be signed in to change notification settings - Fork 3
/
convertermodel.pas
150 lines (123 loc) · 3.39 KB
/
convertermodel.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
unit ConverterModel;
{$mode objfpc}{$H+}
interface
uses
Classes, Sysutils,
ModelInterface;
type
{ TConverter }
TConverter = class(TInterfacedObject, ICalculatorModel)
protected
FISODate : string;
FUnixDate : integer;
FOnISODateChanged : TNotifyEvent;
FOnUnixDateChanged : TNotifyEvent;
class function IsISODateString ( Date : string) : boolean;
class function ISOStringToDateTime( Date : string ) : TDateTime;
public
procedure SetISODate ( Date : string);
procedure SetUnixDate( Date : integer );
procedure SetOnISODateChanged( Func : TNotifyEvent );
procedure SetOnUnixDateChanged( Func : TNotifyEvent );
function GetISODate : string;
function GetUnixDate : integer;
function GetOnISODateChanged : TNotifyEvent;
function GetOnUnixDateChanged : TNotifyEvent;
property ISODate : string read GetISODate write SetIsoDate;
property UnixDate : integer read GetUnixDate write SetUnixDate;
property OnISODateChanged : TNotifyEvent read GetOnISODateChanged write SetOnISODateChanged;
property OnUnixDateChanged : TNotifyEvent read GetOnUnixDateChanged write SetOnUnixDateChanged;
end;
implementation
uses
regexpr, dateutils;
{ TConverter }
class function TConverter.IsISODateString(Date : String) : Boolean;
var
Pattern : TRegExpr;
begin
Pattern := TRegExpr.Create;
Pattern.Expression := '[0-9]{4}-[0-9]{2}-[0-9]{2}';
if not Pattern.Exec(Date) then
Result := false
else
Result := true;
end;
class function TConverter.ISOStringToDateTime(Date : String) : TDatetime;
begin
if not IsISODateString(Date) then
raise Exception.Create('datestring is not in format YYYY-mm-dd');
try
Result := StrToDate(Date, 'YYYY-MM-DD', '-');
except
on e : Exception do
raise e;
end;
end;
procedure TConverter.SetISODate(Date : String);
var
NativeDate : TDateTime;
NewUnixDate : integer;
begin
if Date = FISODate then
Exit;
try
WriteLn('TConverter.SetISODate ', Date);
NativeDate := ISOStringToDateTime(Date);
FISODate := Date;
NewUnixDate := DateTimeToUnix(NativeDate);
if Assigned(FOnISODateChanged) then
FOnISODateChanged(self)
else
WriteLn('FOnISODateChanged is not assigned.');
SetUnixDate(NewUnixDate);
except
Writeln('Error');
end;
end;
function TConverter.GetISODate : String;
begin
Result := FISODate;
end;
procedure TConverter.SetUnixDate(Date : Integer);
var
NativeDate : TDateTime;
NewISODate : string;
begin
if Date = FUnixDate then
Exit;
try
WriteLn('TConverter.SetUnixDate ', Date);
NativeDate := UnixToDateTime(Date);
FUnixDate := Date;
NewISODate := FormatDateTime('YYYY-MM-DD', NativeDate);
if Assigned(FOnUnixDateChanged) then
FOnUnixDateChanged(self)
else
WriteLn('FOnUnixDateChanged is not assigned.');
SetISODate(NewISODate);
except
Writeln('Error');
end;
end;
function TConverter.GetUnixDate : Integer;
begin
Result := FUnixDate;
end;
procedure TConverter.SetOnISODateChanged(Func : TNotifyEvent);
begin
FOnISODateChanged := Func;
end;
procedure TConverter.SetOnUnixDateChanged(Func : TNotifyEvent);
begin
FOnUnixDateChanged := Func;
end;
function TConverter.GetOnISODateChanged : TNotifyEvent;
begin
Result := FOnISODateChanged;
end;
function TConverter.GetOnUnixDateChanged : TNotifyEvent;
begin
Result := FOnUnixDateChanged;
end;
end.