source: Include/Classes/System/DateTime.ab@ 103

Last change on this file since 103 was 103, checked in by OverTaker, 17 years ago

列挙体をタイプセーフにしました。

File size: 11.5 KB
Line 
1' Classes/System/DateTime.ab
2
3#ifndef __SYSTEM_DATETIME_AB__
4#define __SYSTEM_DATETIME_AB__
5
6
7Class DateTime
8 m_Date As Int64
9Public
10 Static MaxValue = 3162240000000000000 As Int64 'Const
11 Static MinValue = 316224000000000 As Int64 'Const
12
13 Sub DateTime(ticks As Int64)
14 Ticks = ticks
15 Kind = DateTimeKind.Unspecified
16 End Sub
17
18 Sub DateTime(ticks As Int64, kind As DateTimeKind)
19 DateTime(ticks)
20 Kind = kind
21 End Sub
22
23 Sub DateTime(year As Long, month As Long, day As Long)
24 If year < 1 Or year > 9999 Or month < 1 Or month > 12 Or day < 1 Or day > DaysInMonth(year, month) Then
25 'ArgumentOutOfRangeException
26 debug
27 End If
28 DateTime(316224000000000)
29 AddYears(year - 1)
30
31 Dim days As Long
32 Dim i As Long
33 For i = 1 To month - 1
34 days += DaysInMonth(Year, i)
35 Next
36 days += day
37 AddDays(days - 1)
38 End Sub
39
40 Sub DateTime(year As Long, month As Long, day As Long, kind As DateTimeKind)
41 DateTime(year, month, day)
42 Kind = kind
43 End Sub
44
45 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long)
46 If hour < 0 Or hour > 23 Or minute < 0 Or minute > 59 Or second < 0 Or second > 59 Then
47 'ArgumentOutOfRangeException
48 debug
49 End If
50 DateTime(year, month, day)
51 AddHours(hour)
52 AddMinutes(minute)
53 AddSeconds(second)
54 End Sub
55
56 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, kind As DateTimeKind)
57 DateTime(year, month, day, hour, minute, second)
58 Kind = kind
59 End Sub
60
61 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long)
62 DateTime(year, month, day, hour, minute, second)
63 AddMilliseconds(millisecond)
64 End Sub
65
66 Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long, kind As DateTimeKind)
67 DateTime(year, month, day, hour, minute, second, millisecond)
68 Kind = kind
69 End Sub
70
71 Sub DateTime(ByRef time As SYSTEMTIME)
72 DateTime(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)
73 End Sub
74
75 Sub DateTime(ByRef time As SYSTEMTIME, kind As DateTimeKind)
76 DateTime(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, kind)
77 End Sub
78
79 'Copy Constructor
80 Sub DateTime( ByRef datetime As DateTime )
81 This.m_Date = datetime.m_Date
82 End Sub
83
84 Sub ~DateTime()
85 End Sub
86
87 Function Operator == (ByRef value As DateTime) As Boolean
88 Return Equals(value)
89 End Function
90
91 Function Operator <> (ByRef value As DateTime) As Boolean
92 Return Not Equals(value)
93 End Function
94
95 Function Operator > (ByRef value As DateTime) As Boolean
96 If DateTime.Compare(This, value) > 0 Then
97 Return True
98 Else
99 Return False
100 End If
101 End Function
102
103 Function Operator < (ByRef value As DateTime) As Boolean
104 If DateTime.Compare(This, value) < 0 Then
105 Return True
106 Else
107 Return False
108 End If
109 End Function
110
111 Function Operator >= (ByRef value As DateTime) As Boolean
112 If DateTime.Compare(This, value) => 0 Then
113 Return True
114 Else
115 Return False
116 End If
117 End Function
118
119 Function Operator <= (ByRef value As DateTime) As Boolean
120 If DateTime.Compare(This, value) <= 0 Then
121 Return True
122 Else
123 Return False
124 End If
125 End Function
126
127 'Property
128 Function Ticks() As Int64
129 Return (m_Date And &H3FFFFFFFFFFFFFFF)
130 End Function
131
132 Function Millisecond() As Long
133 Return (Ticks \ 10000 Mod 1000) As Long
134 End Function
135
136 Function Second() As Long
137 Return (Ticks \ 10000000 Mod 60) As Long
138 End Function
139
140 Function Minute() As Long
141 Return (Ticks \ 600000000 Mod 60) As Long
142 End Function
143
144 Function Hour() As Long
145 Return (Ticks \ 36000000000 Mod 24) As Long
146 End Function
147
148 Function Day() As Long
149 Dim day As Long
150 day = DayOfYear
151
152 Dim i As Long
153 For i = 1 To Month - 1
154 day -= DaysInMonth(Year, i)
155 Next
156 Return day
157 End Function
158
159 Function Month() As Long
160 Dim year As Long
161 Dim day As Long
162 year = Year
163 day = DayOfYear
164
165 Dim i As Long
166 For i = 1 To 12
167 day -= DaysInMonth(year, i)
168 If day <= 0 Then Return i
169 Next
170 Return 12
171 End Function
172
173 Function Year() As Long
174 Dim day As Long
175 day = totalDays()
176 Return Int((day + day \ 36523 - day \ 146097) / 365.25)
177 End Function
178
179 Function DayOfWeek() As DayOfWeek
180 Return totalDays() Mod 7 - 1
181 End Function
182
183 Function Kind() As DateTimeKind
184 Return kindFromBinary(m_Date)
185 End Function
186
187 Function DayOfYear() As Long
188 Dim day As Long
189 day = totalDays()
190 Return day - Int(Year * 365.25 - day \ 36523 + day \ 146097)
191 End Function
192
193 Function Date() As DateTime
194 Dim date As DateTime(Year, Month, Day, Kind)
195 Return date
196 End Function
197
198 Static Function Now() As DateTime
199 Dim time As SYSTEMTIME
200 GetLocalTime(time)
201 Dim date As DateTime(time, DateTimeKind.Local)
202 Return date
203 End Function
204
205 Static Function ToDay() As DateTime
206 Dim time As SYSTEMTIME
207 GetLocalTime(time)
208 Dim date As DateTime(time.wYear, time.wMonth, time.wDay, DateTimeKind.Local)
209 Return date
210 End Function
211
212 Static Function UtcNow() As DateTime
213 Dim time As SYSTEMTIME
214 GetSystemTime(time)
215 Dim date As DateTime(time, DateTimeKind.Utc)
216 Return date
217 End Function
218
219 'method
220 Static Function Compare(ByRef t1 As DateTime, ByRef t2 As DateTime) As Int64
221 Return t1.Ticks - t2.Ticks
222 End Function
223
224 Function Equals(ByRef value As DateTime) As Boolean
225 If value.m_Date = m_Date Then
226 Return True
227 Else
228 Return False
229 End If
230 End Function
231
232 Static Function Equals(ByRef t1 As DateTime, ByRef t2 As DateTime) As Boolean
233 If t1.m_Date = t2.m_Date Then
234 Return True
235 Else
236 Return False
237 End If
238 End Function
239
240 Sub AddTicks(value As Int64)
241 Dim ticks As Int64
242 ticks = Ticks
243 If (ticks > DateTime.MaxValue - value) Or (ticks < DateTime.MinValue - value) Then
244 'ArgumentOutOfRangeException
245 debug
246 End If
247 Ticks = ticks + value
248 End Sub
249
250 Sub AddMilliseconds(value As Double)
251 AddTicks((value * 10000) As Int64)
252 End Sub
253
254 Sub AddSeconds(value As Double)
255 AddTicks((value * 10000000) As Int64)
256 End Sub
257
258 Sub AddMinutes(value As Double)
259 AddTicks((value * 600000000) As Int64)
260 End Sub
261
262 Sub AddHours(value As Double)
263 AddTicks((value * 36000000000) As Int64)
264 End Sub
265
266 Sub AddDays(value As Double)
267 AddTicks((value * 864000000000) As Int64)
268 End Sub
269
270 Sub AddYears(value As Double)
271 Dim year As Long
272 Dim intValue As Long
273 year = Year
274 intValue = Int(value)
275 AddTicks(intValue * 315360000000000 + 864000000000 * ((year Mod 4 + intValue) \ 4 - (year Mod 100 + intValue) \ 100 + (year Mod 400 + intValue) \ 400))
276 If value < 0 Then
277 If (year Mod 4 + intValue <= 0 And year Mod 100 > 4) Or (year Mod 400 <= 4) Then
278 AddTicks(-864000000000)
279 End If
280 End If
281
282 If IsLeapYear(year) = TRUE Then
283 AddTicks(((value - intValue) * 316224000000000) As Int64)
284 Else
285 AddTicks(((value - intValue) * 315360000000000) As Int64)
286 End If
287 End Sub
288
289 Static Function DaysInMonth(year As Long, month As Long) As Long
290 If year < 1 Or year > 9999 Or month < 1 Or month > 12 Then
291 'ArgumentOutOfRangeException
292 debug
293 End If
294 If IsLeapYear(year) And month = 2 Then
295 Return 29
296 Else
297 Dim daysInMonth[11] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] As Byte
298 Return daysInMonth[month - 1]
299 End If
300 End Function
301
302 Static Function IsLeapYear(year As Long) As Boolean
303 If (year Mod 400) = 0 Then Return True
304 If (year Mod 100) = 0 Then Return False
305 If (year Mod 4) = 0 Then Return True
306 Return False
307 End Function
308
309 Function GetDateTimeFormats() As String
310 Return GetDateTimeFormats(NULL)
311 End Function
312
313 Function GetDateTimeFormats(format As *Byte) As String
314 Dim time As SYSTEMTIME
315 With time
316 .wYear = Year As Word
317 .wMonth = Month As Word
318 .wDay = Day As Word
319 .wHour = Hour As Word
320 .wMinute = Minute As Word
321 .wSecond = Second As Word
322 .wMilliseconds = Millisecond As Word
323 .wDayOfWeek = DayOfWeek() As Word
324 End With
325
326 Dim size As Long
327 size = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
328 GetDateTimeFormats.ReSize(size - 1)
329 GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, GetDateTimeFormats, size)
330
331 Dim temp As String
332 If format = NULL Then
333 size = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
334 temp.ReSize(size - 1)
335 GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, format, temp, size)
336 GetDateTimeFormats = GetDateTimeFormats + " " + temp
337 Else
338 size = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, GetDateTimeFormats, NULL, 0)
339 temp.ReSize(size - 1)
340 GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, GetDateTimeFormats, temp, size)
341 GetDateTimeFormats = temp
342 End If
343 End Function
344
345 Static Function FromBinary(dateData As Int64) As DateTime
346 Dim date As DateTime((dateData And &H3FFFFFFFFFFFFFFF), kindFromBinary(dateData))
347 Return date
348 End Function
349
350 Function ToBinary() As Int64
351 Return m_Date
352 End Function
353
354 Static Function FromFileTime(fileTime As FILETIME) As DateTime
355 Dim localTime As FILETIME
356 Dim time As SYSTEMTIME
357 FileTimeToLocalFileTime(fileTime, localTime)
358 FileTimeToSystemTime(localTime, time)
359
360 Dim date As DateTime(time, DateTimeKind.Local)
361 Return date
362 End Function
363
364 Function ToFileTime() As FILETIME
365 Dim time As SYSTEMTIME
366 With time
367 .wYear = Year As Word
368 .wMonth = Month As Word
369 .wDayOfWeek = DayOfWeek As Word
370 .wDay = Day As Word
371 .wHour = Hour As Word
372 .wMinute = Minute As Word
373 .wSecond = Second As Word
374 .wMilliseconds = Millisecond As Word
375 End With
376 Dim fileTime As FILETIME
377 SystemTimeToFileTime(time, fileTime)
378 Return fileTime
379 End Function
380
381 Static Function FromFileTimeUtc(fileTime As FILETIME) As DateTime
382 Dim time As SYSTEMTIME
383 FileTimeToSystemTime(fileTime, time)
384
385 Dim date As DateTime(time, DateTimeKind.Utc)
386 Return date
387 End Function
388
389 Function ToFileTimeUtc() As FILETIME
390 Dim fileTime As FILETIME
391 fileTime = ToFileTime()
392 If Kind = DateTimeKind.Utc Then
393 ToFileTimeUtc = fileTime
394 Else
395 LocalFileTimeToFileTime(fileTime, ToFileTimeUtc) 'Return
396 End If
397 End Function
398
399 Function ToLocalTime() As DateTime
400 If Kind = DateTimeKind.Local Then
401 ToLocalTime = This
402 Else
403 ToLocalTime = DateTime.FromFileTime(ToFileTime())
404 ToLocalTime.Kind = DateTimeKind.Local
405 End If
406 End Function
407
408 Function ToUniversalTime() As DateTime
409 If Kind = DateTimeKind.Utc Then
410 ToUniversalTime = This
411 Else
412 ToUniversalTime = DateTime.FromFileTimeUtc(ToFileTimeUtc())
413 ToUniversalTime.Kind = DateTimeKind.Utc
414 End If
415 End Function
416Private
417 Sub Ticks(value As Int64)
418 Dim kind As DateTimeKind
419 kind = Kind
420 m_Date = value
421 Kind = kind
422 End Sub
423
424 Sub Kind(kind As DateTimeKind)
425 Dim temp As Int64
426 temp = kind
427 temp = (temp << 62) And &HC000000000000000
428 m_Date = (m_Date And &H3FFFFFFFFFFFFFFF) Or temp
429 End Sub
430
431 Function totalDays() As Long
432 Return (Ticks \ 864000000000) As Long
433 End Function
434
435 Function kindFromBinary(dateData As Int64) As DateTimeKind
436 dateData = (dateData >> 62) And &H03
437 If dateData = &H01 Then
438 Return DateTimeKind.Local
439 ElseIf dateData = &H02 Then
440 Return DateTimeKind.Unspecified
441 ElseIf dateData = &H03 Then
442 Return DateTimeKind.Utc
443 End If
444 End Function
445End Class
446
447Enum DateTimeKind
448 Local
449 Unspecified
450 Utc
451End Enum
452
453Enum DayOfWeek
454 Sunday = 0
455 Monday
456 Tuesday
457 Wednesday
458 Thursday
459 Friday
460 Saturday
461End Enum
462
463#endif '__SYSTEM_DATETIME_AB__
Note: See TracBrowser for help on using the repository browser.