-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddateprimitives_iso8601.pas
169 lines (153 loc) · 4.36 KB
/
ddateprimitives_iso8601.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
// https://tools.ietf.org/html/rfc3339
{$MODE FPC}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
unit ddateprimitives_iso8601;
interface
uses
ddateprimitives;
type
TDateInteger = ddateprimitives.TDateInteger;
TYearWeekDay = record
Year: TDateInteger;
Week: TDateInteger; // 1-53
Day: TDateInteger; // 0-6 (Sun,Mon,Tue,Wed,Thu,Fri,Sat)
end;
//
// iso_week_start_from_year
//
// Returns the first day of the week-based year.
//
// Returns:
//
// Result: serial day number of first day of Y
//
function iso_week_start_from_year(Y: TDateInteger): TDateInteger;
//
// iso_week_from_civil
//
// Converts civil date to week calendar date.
//
// Parameters:
//
// Y-M-D represents civil date.
//
// Y: year is in range
// DATE_PRIMITIVES_YEAR_LOW .. DATE_PRIMITIVES_YEAR_HIGH
// M: month is in range 1..12
// D: day is in range 1..last_day_of_month(Y, M)
//
// The function does not check ranges of input arguments.
// Caller should check it by himself.
//
// Returns:
//
// Iso or Result: the week-based date structure
// Year, Week, Day: the week-based date as separated variables
//
procedure iso_week_from_civil(Y, M, D: TDateInteger;
out Iso: TYearWeekDay); overload;
function iso_week_from_civil(Y, M, D: TDateInteger): TYearWeekDay; overload; inline;
procedure iso_week_from_civil(Y, M, D: TDateInteger;
out Year, Week, Day: TDateInteger); overload; inline;
//
// civil_from_iso_week
//
// Converts week-based date to civil date.
//
// Parameters:
//
// Y,W,D represents week-based date.
//
// Y: year number in range
// DATE_PRIMITIVES_YEAR_LOW .. DATE_PRIMITIVES_YEAR_HIGH
// W: the week number in range 1..53
// D: the week day number in range 0..6
//
// The function does not check ranges of input arguments.
// Caller should check it by himself.
//
// Returns:
//
// Civil: structure for the resulting date
// Year,Month,Day: the triple as separated variables
//
procedure civil_from_iso_week(Y, W, D: TDateInteger;
out Civil: TYearMonthDay); overload;
function civil_from_iso_week(Y, W, D: TDateInteger): TYearMonthDay; overload; inline;
procedure civil_from_iso_week(Y, W, D: TDateInteger;
out Year, Month, Day: TDateInteger); overload; inline;
implementation
function iso_week_start_from_year(Y: TDateInteger): TDateInteger;
var
Tp: TDateInteger;
Wd: TDateInteger;
begin
Tp := days_from_civil(Y, 1, 4); // 4 january Y
Wd := weekday_from_days(Tp);
Exit(Tp - weekday_difference(Wd, 1)); // 1 = Monday
end;
procedure iso_week_from_civil(Y, M, D: TDateInteger;
out Iso: TYearWeekDay);
const
MONDAY = 1;
THURSDAY = 4;
var
Tp: TDateInteger;
IsoWeekStart: TDateInteger;
IsoWeekNextYearStart: TDateInteger;
Civil: TYearMonthDay;
begin
Tp := days_from_civil(Y, M, D);
IsoWeekStart := iso_week_start_from_year(Y);
if Tp < IsoWeekStart then begin
IsoWeekStart := iso_week_start_from_year(Y - 1);
end else begin
IsoWeekNextYearStart := iso_week_start_from_year(Y + 1);
end;
civil_from_days(IsoWeekStart + TDateInteger(THURSDAY - MONDAY), Civil);
Iso.Day := weekday_from_days(Tp);
Iso.Week := (Tp - IsoWeekStart) div 7 + 1;
Iso.Year := Civil.Year;
end;
function iso_week_from_civil(Y, M, D: TDateInteger): TYearWeekDay;
begin
iso_week_from_civil(Y, M, D, Result);
end;
procedure iso_week_from_civil(Y, M, D: TDateInteger;
out Year, Week, Day: TDateInteger);
var
Iso: TYearWeekDay;
begin
iso_week_from_civil(Y, M, D, Iso);
Year := Iso.Year;
Week := Iso.Week;
Day := Iso.Day;
end;
procedure civil_from_iso_week(Y, W, D: TDateInteger;
out Civil: TYearMonthDay);
var
Tp: TDateInteger;
begin
Tp := iso_week_start_from_year(Y) + (W - 1) * 7;
if D = 0 then begin
Inc(Tp, 6);
end else
Inc(Tp, D - 1);
civil_from_days(Tp, Civil);
end;
function civil_from_iso_week(Y, W, D: TDateInteger): TYearMonthDay;
begin
civil_from_iso_week(Y, W, D, Result);
end;
procedure civil_from_iso_week(Y, W, D: TDateInteger;
out Year, Month, Day: TDateInteger);
var
Civil: TYearMonthDay;
begin
civil_from_iso_week(Y, W, D, Civil);
Year := Civil.Year;
Month := Civil.Month;
Day := Civil.Day;
end;
end.