-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathHGM.Common.DateUtils.pas
135 lines (106 loc) · 3.18 KB
/
HGM.Common.DateUtils.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
unit HGM.Common.DateUtils;
interface
uses
System.SysUtils, System.DateUtils;
type
TDatePeriod = record
private
function GetDaysBetween: Integer;
public
DateBegin: TDateTime;
DateEnd: TDateTime;
property DaysBetween: Integer read GetDaysBetween;
procedure SetValue(ADateBegin, ADateEnd: TDateTime);
class function Create(DateBegin, DateEnd: TDateTime): TDatePeriod; static;
end;
function HumanDateTime(Value: TDateTime; ShowTime: Boolean = True; WeekDay: Boolean = False): string;
function SecondsToTime(Value: Double): TTime;
function HumanTime(Value: TTime): string;
function HumanDate(Value: TDate): string;
function SecondsToMinFormat(const Value: Int64): string;
function MSecondsToMinFormat(const Value: Int64): string;
implementation
uses
System.StrUtils;
function SecondsToMinFormat(const Value: Int64): string;
begin
var Mins := Value div SecsPerMin;
var Secs := Value - (Mins * SecsPerMin);
Result := Format(IfThen(Value < 0, '-') + '%d:%.2d', [Abs(Mins), Abs(Secs)]);
end;
function MSecondsToMinFormat(const Value: Int64): string;
begin
var Secs := Value div (MSecsPerSec);
var MSecs := Value - (Secs * MSecsPerSec);
var Mins := Secs div (SecsPerMin);
Secs := Secs - (Mins * SecsPerMin);
Result := Format(IfThen(Value < 0, '-') + '%d:%.2d.%.3d', [Abs(Mins), Abs(Secs), Abs(MSecs)]);
end;
function HumanTime(Value: TTime): string;
var
H, M, S, Ms: Word;
begin
DecodeTime(Value, H, M, S, Ms);
Result := '';
if H > 0 then
Result := Result + H.ToString + ' ÷. ';
if M > 0 then
Result := Result + M.ToString + ' ìèí. ';
if S > 0 then
Result := Result + S.ToString + ' ñåê. ';
end;
function HumanDate(Value: TDate): string;
var
D: Integer;
begin
Result := '';
D := Trunc(Value);
if D > 0 then
Result := Result + D.ToString + ' ä. ';
end;
function SecondsToTime(Value: Double): TTime;
begin
Result := Value / SecsPerDay;
end;
function HumanDateTime(Value: TDateTime; ShowTime: Boolean; WeekDay: Boolean): string;
function AddWeekDay: string;
begin
if WeekDay then
Result := FormatDateTime(', ddd', Value)
else
Result := '';
end;
begin
if IsSameDay(Value, Today + 2) then
Result := 'Ïîñëåçàâòðà' + AddWeekDay
else if IsSameDay(Value, Today + 1) then
Result := 'Çàâòðà' + AddWeekDay
else if IsSameDay(Value, Today) then
Result := 'Ñåãîäíÿ' + AddWeekDay
else if IsSameDay(Value, Yesterday) then
Result := 'Â÷åðà' + AddWeekDay
else if IsSameDay(Value, Yesterday - 1) then
Result := 'Ïîçàâ÷åðà' + AddWeekDay
else if YearOf(Value) = YearOf(Now) then
Result := FormatDateTime('DD mmm', Value) + AddWeekDay
else
Result := FormatDateTime('DD mmm YYYY', Value) + AddWeekDay;
if ShowTime then
Result := Result + FormatDateTime(' â HH:NN:SS', Value);
end;
{ TDatePeriod }
class function TDatePeriod.Create(DateBegin, DateEnd: TDateTime): TDatePeriod;
begin
Result.DateBegin := DateBegin;
Result.DateEnd := DateEnd;
end;
function TDatePeriod.GetDaysBetween: Integer;
begin
Result := System.DateUtils.DaysBetween(DateBegin, DateEnd);
end;
procedure TDatePeriod.SetValue(ADateBegin, ADateEnd: TDateTime);
begin
DateBegin := ADateBegin;
DateEnd := ADateEnd;
end;
end.