| 1 | Namespace System
|
|---|
| 2 |
|
|---|
| 3 | /*!
|
|---|
| 4 | @brief 時刻の種類
|
|---|
| 5 | */
|
|---|
| 6 | Enum DateTimeKind
|
|---|
| 7 | Local
|
|---|
| 8 | Unspecified
|
|---|
| 9 | Utc
|
|---|
| 10 | End Enum
|
|---|
| 11 |
|
|---|
| 12 | /*!
|
|---|
| 13 | @brief 曜日
|
|---|
| 14 | */
|
|---|
| 15 | Enum DayOfWeek
|
|---|
| 16 | Sunday = 0
|
|---|
| 17 | Monday
|
|---|
| 18 | Tuesday
|
|---|
| 19 | Wednesday
|
|---|
| 20 | Thursday
|
|---|
| 21 | Friday
|
|---|
| 22 | Saturday
|
|---|
| 23 | End Enum
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | /*!
|
|---|
| 27 | @brief 時刻を表すクラス
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | Class DateTime
|
|---|
| 31 | m_Date As Int64
|
|---|
| 32 | Public
|
|---|
| 33 | Static Const MaxValue = 3162240000000000000 As Int64 'Const TicksPerDay*366*10000
|
|---|
| 34 | Static Const MinValue = 316224000000000 As Int64 'Const TicksPerDay*366
|
|---|
| 35 |
|
|---|
| 36 | '----------------------------------------------------------------
|
|---|
| 37 | ' パブリック コンストラクタ
|
|---|
| 38 | '----------------------------------------------------------------
|
|---|
| 39 |
|
|---|
| 40 | /*!
|
|---|
| 41 | @brief コンストラクタ
|
|---|
| 42 | */
|
|---|
| 43 | Sub DateTime()
|
|---|
| 44 | initialize(MinValue, DateTimeKind.Unspecified)
|
|---|
| 45 | End Sub
|
|---|
| 46 |
|
|---|
| 47 | /*!
|
|---|
| 48 | @brief 時刻を指定して初期化する
|
|---|
| 49 | @param 100ナノ秒単位で表した時刻
|
|---|
| 50 | */
|
|---|
| 51 | Sub DateTime(ticks As Int64)
|
|---|
| 52 | initialize(ticks, DateTimeKind.Unspecified)
|
|---|
| 53 | End Sub
|
|---|
| 54 |
|
|---|
| 55 | /*!
|
|---|
| 56 | @brief 時刻を指定して初期化する
|
|---|
| 57 | @param 100ナノ秒単位で表した時刻
|
|---|
| 58 | @param 時刻の種類
|
|---|
| 59 | */
|
|---|
| 60 | Sub DateTime(ticks As Int64, kind As DateTimeKind)
|
|---|
| 61 | initialize(ticks, kind)
|
|---|
| 62 | End Sub
|
|---|
| 63 |
|
|---|
| 64 | /*!
|
|---|
| 65 | @brief 時刻を指定して初期化する
|
|---|
| 66 | @param 西暦
|
|---|
| 67 | @param 月
|
|---|
| 68 | @param 日
|
|---|
| 69 | */
|
|---|
| 70 | Sub DateTime(year As Long, month As Long, day As Long)
|
|---|
| 71 | initialize(year, month, day, 0, 0, 0, 0, DateTimeKind.Unspecified)
|
|---|
| 72 | End Sub
|
|---|
| 73 |
|
|---|
| 74 | /*!
|
|---|
| 75 | @brief 時刻を指定して初期化する
|
|---|
| 76 | @param 西暦
|
|---|
| 77 | @param 月
|
|---|
| 78 | @param 日
|
|---|
| 79 | @param 時刻の種類
|
|---|
| 80 | */
|
|---|
| 81 | Sub DateTime(year As Long, month As Long, day As Long, kind As DateTimeKind)
|
|---|
| 82 | initialize(year, month, day, 0, 0, 0, 0, kind)
|
|---|
| 83 | End Sub
|
|---|
| 84 |
|
|---|
| 85 | /*!
|
|---|
| 86 | @brief 時刻を指定して初期化する
|
|---|
| 87 | @param 西暦
|
|---|
| 88 | @param 月
|
|---|
| 89 | @param 日
|
|---|
| 90 | @param 時
|
|---|
| 91 | @param 分
|
|---|
| 92 | @param 秒
|
|---|
| 93 | */
|
|---|
| 94 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long)
|
|---|
| 95 | initialize(year, month, day, hour, minute, second, 0, DateTimeKind.Unspecified)
|
|---|
| 96 | End Sub
|
|---|
| 97 |
|
|---|
| 98 | /*!
|
|---|
| 99 | @brief 時刻を指定して初期化する
|
|---|
| 100 | @param 西暦
|
|---|
| 101 | @param 月
|
|---|
| 102 | @param 日
|
|---|
| 103 | @param 時
|
|---|
| 104 | @param 分
|
|---|
| 105 | @param 秒
|
|---|
| 106 | @param 時刻の種類
|
|---|
| 107 | */
|
|---|
| 108 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, kind As DateTimeKind)
|
|---|
| 109 | initialize(year, month, day, hour, minute, second, 0, kind)
|
|---|
| 110 | End Sub
|
|---|
| 111 |
|
|---|
| 112 | /*!
|
|---|
| 113 | @brief 時刻を指定して初期化する
|
|---|
| 114 | @param 西暦
|
|---|
| 115 | @param 月
|
|---|
| 116 | @param 日
|
|---|
| 117 | @param 時
|
|---|
| 118 | @param 分
|
|---|
| 119 | @param 秒
|
|---|
| 120 | @param ミリ秒
|
|---|
| 121 | */
|
|---|
| 122 | Sub DateTime(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long)
|
|---|
| 123 | initialize(year, month, day, hour, minute, second, millisecond, DateTimeKind.Unspecified)
|
|---|
| 124 | End Sub
|
|---|
| 125 |
|
|---|
| 126 | /*!
|
|---|
| 127 | @brief 時刻を指定して初期化する
|
|---|
| 128 | @param 西暦
|
|---|
| 129 | @param 月
|
|---|
| 130 | @param 日
|
|---|
| 131 | @param 時
|
|---|
| 132 | @param 分
|
|---|
| 133 | @param 秒
|
|---|
| 134 | @param ミリ秒
|
|---|
| 135 | @param 時刻の種類
|
|---|
| 136 | */
|
|---|
| 137 | 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)
|
|---|
| 138 | initialize(year, month, day, hour, minute, second, millisecond, kind)
|
|---|
| 139 | End Sub
|
|---|
| 140 |
|
|---|
| 141 | /*!
|
|---|
| 142 | @brief 時刻を指定して初期化する
|
|---|
| 143 | @param SYSTEMTIME構造体
|
|---|
| 144 | */
|
|---|
| 145 | Sub DateTime(ByRef time As SYSTEMTIME)
|
|---|
| 146 | initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, DateTimeKind.Unspecified)
|
|---|
| 147 | End Sub
|
|---|
| 148 |
|
|---|
| 149 | /*!
|
|---|
| 150 | @brief 時刻を指定して初期化する
|
|---|
| 151 | @param SYSTEMTIME構造体
|
|---|
| 152 | @param 時刻の種類
|
|---|
| 153 | */
|
|---|
| 154 | Sub DateTime(ByRef time As SYSTEMTIME, kind As DateTimeKind)
|
|---|
| 155 | initialize(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, kind)
|
|---|
| 156 | End Sub
|
|---|
| 157 |
|
|---|
| 158 | /*!
|
|---|
| 159 | @brief コピーコンストラクタ
|
|---|
| 160 | @param コピーするDateTime
|
|---|
| 161 | */
|
|---|
| 162 | Sub DateTime(dateTime As DateTime)
|
|---|
| 163 | This.m_Date = dateTime.m_Date
|
|---|
| 164 | End Sub
|
|---|
| 165 |
|
|---|
| 166 | '----------------------------------------------------------------
|
|---|
| 167 | ' オペレータ
|
|---|
| 168 | '----------------------------------------------------------------
|
|---|
| 169 |
|
|---|
| 170 | Function Operator + (value As TimeSpan) As DateTime
|
|---|
| 171 | Return New DateTime(Ticks + value.Ticks)
|
|---|
| 172 | End Function
|
|---|
| 173 |
|
|---|
| 174 | Function Operator - (value As DateTime) As TimeSpan
|
|---|
| 175 | Return TimeSpan.FromTicks(Ticks - value.Ticks)
|
|---|
| 176 | End Function
|
|---|
| 177 |
|
|---|
| 178 | Function Operator - (value As TimeSpan) As DateTime
|
|---|
| 179 | Return New DateTime(Ticks - value.Ticks)
|
|---|
| 180 | End Function
|
|---|
| 181 |
|
|---|
| 182 | Function Operator == (value As DateTime) As Boolean
|
|---|
| 183 | Return Equals(value)
|
|---|
| 184 | End Function
|
|---|
| 185 |
|
|---|
| 186 | Function Operator <> (value As DateTime) As Boolean
|
|---|
| 187 | Return Not Equals(value)
|
|---|
| 188 | End Function
|
|---|
| 189 |
|
|---|
| 190 | Function Operator > (value As DateTime) As Boolean
|
|---|
| 191 | If DateTime.Compare(This, value) > 0 Then
|
|---|
| 192 | Return True
|
|---|
| 193 | Else
|
|---|
| 194 | Return False
|
|---|
| 195 | End If
|
|---|
| 196 | End Function
|
|---|
| 197 |
|
|---|
| 198 | Function Operator < (value As DateTime) As Boolean
|
|---|
| 199 | If DateTime.Compare(This, value) < 0 Then
|
|---|
| 200 | Return True
|
|---|
| 201 | Else
|
|---|
| 202 | Return False
|
|---|
| 203 | End If
|
|---|
| 204 | End Function
|
|---|
| 205 |
|
|---|
| 206 | Function Operator >= (value As DateTime) As Boolean
|
|---|
| 207 | If DateTime.Compare(This, value) => 0 Then
|
|---|
| 208 | Return True
|
|---|
| 209 | Else
|
|---|
| 210 | Return False
|
|---|
| 211 | End If
|
|---|
| 212 | End Function
|
|---|
| 213 |
|
|---|
| 214 | Function Operator <= (value As DateTime) As Boolean
|
|---|
| 215 | If DateTime.Compare(This, value) <= 0 Then
|
|---|
| 216 | Return True
|
|---|
| 217 | Else
|
|---|
| 218 | Return False
|
|---|
| 219 | End If
|
|---|
| 220 | End Function
|
|---|
| 221 |
|
|---|
| 222 | '----------------------------------------------------------------
|
|---|
| 223 | ' パブリック プロパティ
|
|---|
| 224 | '----------------------------------------------------------------
|
|---|
| 225 |
|
|---|
| 226 | /*!
|
|---|
| 227 | @brief 時刻を100ナノ秒単位で取得する
|
|---|
| 228 | @return 時刻
|
|---|
| 229 | */
|
|---|
| 230 | Function Ticks() As Int64
|
|---|
| 231 | Return (m_Date And &H3FFFFFFFFFFFFFFF)
|
|---|
| 232 | End Function
|
|---|
| 233 |
|
|---|
| 234 | /*!
|
|---|
| 235 | @brief 時刻のミリ秒単位部分を取得する
|
|---|
| 236 | @return ミリ秒の部分
|
|---|
| 237 | */
|
|---|
| 238 | Function Millisecond() As Long
|
|---|
| 239 | Return (Ticks \ TimeSpan.TicksPerMillisecond Mod 1000) As Long
|
|---|
| 240 | End Function
|
|---|
| 241 |
|
|---|
| 242 | /*!
|
|---|
| 243 | @brief 時刻の秒単位部分を取得する
|
|---|
| 244 | @return 秒の部分
|
|---|
| 245 | */
|
|---|
| 246 | Function Second() As Long
|
|---|
| 247 | Return (Ticks \ TimeSpan.TicksPerSecond Mod 60) As Long
|
|---|
| 248 | End Function
|
|---|
| 249 |
|
|---|
| 250 | /*!
|
|---|
| 251 | @brief 時刻の分単位部分を取得する
|
|---|
| 252 | @return 分の部分
|
|---|
| 253 | */
|
|---|
| 254 | Function Minute() As Long
|
|---|
| 255 | Return (Ticks \ TimeSpan.TicksPerMinute Mod 60) As Long
|
|---|
| 256 | End Function
|
|---|
| 257 |
|
|---|
| 258 | /*!
|
|---|
| 259 | @brief 時刻の時単位部分を取得する
|
|---|
| 260 | @return 時の部分
|
|---|
| 261 | */
|
|---|
| 262 | Function Hour() As Long
|
|---|
| 263 | Return (Ticks \ TimeSpan.TicksPerHour Mod 24) As Long
|
|---|
| 264 | End Function
|
|---|
| 265 |
|
|---|
| 266 | /*!
|
|---|
| 267 | @brief 時刻の日単位部分を取得する
|
|---|
| 268 | @return 日の部分
|
|---|
| 269 | */
|
|---|
| 270 | Function Day() As Long
|
|---|
| 271 | Return DayOfYear - totalDaysOfMonth(Year, Month - 1)
|
|---|
| 272 | End Function
|
|---|
| 273 |
|
|---|
| 274 | /*!
|
|---|
| 275 | @brief 時刻の月単位部分を取得する
|
|---|
| 276 | @return 月の部分
|
|---|
| 277 | */
|
|---|
| 278 | Function Month() As Long
|
|---|
| 279 | Dim year = Year As Long
|
|---|
| 280 | Dim day = DayOfYear As Long
|
|---|
| 281 | Dim i = 1 As Long
|
|---|
| 282 | While day > totalDaysOfMonth(year, i)
|
|---|
| 283 | i++
|
|---|
| 284 | Wend
|
|---|
| 285 | Return i
|
|---|
| 286 | End Function
|
|---|
| 287 |
|
|---|
| 288 | /*!
|
|---|
| 289 | @brief 時刻の年単位部分を取得する
|
|---|
| 290 | @return 西暦
|
|---|
| 291 | */
|
|---|
| 292 | Function Year() As Long
|
|---|
| 293 | Dim day = (Ticks \ TimeSpan.TicksPerDay) As Long
|
|---|
| 294 | Dim year = Int((day + day \ 36524 - day \ 146097) / 365.25) + 1 As Long
|
|---|
| 295 | If day - yearToDay(year - 1) + 1 = 366 Then
|
|---|
| 296 | Return year + 1
|
|---|
| 297 | Else
|
|---|
| 298 | Return year
|
|---|
| 299 | End If
|
|---|
| 300 | End Function
|
|---|
| 301 |
|
|---|
| 302 | /*!
|
|---|
| 303 | @brief 時刻の曜日部分を取得する
|
|---|
| 304 | @return 曜日の部分
|
|---|
| 305 | */
|
|---|
| 306 | Function DayOfWeek() As DayOfWeek
|
|---|
| 307 | Return New DayOfWeek( (((Ticks \ TimeSpan.TicksPerDay) Mod 7) + 1) As Long, "DayOfWeek")
|
|---|
| 308 | End Function
|
|---|
| 309 |
|
|---|
| 310 | /*!
|
|---|
| 311 | @brief 時刻の種類を取得する
|
|---|
| 312 | @return 種類
|
|---|
| 313 | */
|
|---|
| 314 | Function Kind() As DateTimeKind
|
|---|
| 315 | Return kindFromBinary(m_Date)
|
|---|
| 316 | End Function
|
|---|
| 317 |
|
|---|
| 318 | /*!
|
|---|
| 319 | @brief その年の何日目かを取得する
|
|---|
| 320 | @return 日数
|
|---|
| 321 | */
|
|---|
| 322 | Function DayOfYear() As Long
|
|---|
| 323 | Return ((Ticks \ TimeSpan.TicksPerDay) - yearToDay(Year) + 1) As Long
|
|---|
| 324 | End Function
|
|---|
| 325 |
|
|---|
| 326 | /*!
|
|---|
| 327 | @brief 時刻の日付の部分を取得する
|
|---|
| 328 | @return 日付
|
|---|
| 329 | */
|
|---|
| 330 | Function Date() As DateTime
|
|---|
| 331 | Return New DateTime(Year, Month, Day, Kind)
|
|---|
| 332 | End Function
|
|---|
| 333 |
|
|---|
| 334 | /*!
|
|---|
| 335 | @brief 現在の時刻を取得する
|
|---|
| 336 | @return 時刻
|
|---|
| 337 | */
|
|---|
| 338 | Static Function Now() As DateTime
|
|---|
| 339 | Dim time As SYSTEMTIME
|
|---|
| 340 | GetLocalTime(time)
|
|---|
| 341 | Return New DateTime(time, DateTimeKind.Local)
|
|---|
| 342 | End Function
|
|---|
| 343 |
|
|---|
| 344 | /*!
|
|---|
| 345 | @brief 今日を表す時刻を取得する(時間は0時0分)
|
|---|
| 346 | @return 時刻
|
|---|
| 347 | */
|
|---|
| 348 | Static Function Today() As DateTime
|
|---|
| 349 | Dim time As SYSTEMTIME
|
|---|
| 350 | GetLocalTime(time)
|
|---|
| 351 | Return New DateTime(time.wYear, time.wMonth, time.wDay, DateTimeKind.Local)
|
|---|
| 352 | End Function
|
|---|
| 353 |
|
|---|
| 354 | /*!
|
|---|
| 355 | @brief 現在の時刻をUTC時刻で取得する
|
|---|
| 356 | @return 時刻
|
|---|
| 357 | */
|
|---|
| 358 | Static Function UtcNow() As DateTime
|
|---|
| 359 | Dim time As SYSTEMTIME
|
|---|
| 360 | GetSystemTime(time)
|
|---|
| 361 | Return New DateTime(time, DateTimeKind.Utc)
|
|---|
| 362 | End Function
|
|---|
| 363 |
|
|---|
| 364 | '----------------------------------------------------------------
|
|---|
| 365 | ' パブリック メソッド
|
|---|
| 366 | '----------------------------------------------------------------
|
|---|
| 367 |
|
|---|
| 368 | /*!
|
|---|
| 369 | @brief 二つの時刻の差を求める
|
|---|
| 370 | @return 差(単位は100ナノ秒)
|
|---|
| 371 | */
|
|---|
| 372 | Static Function Compare(t1 As DateTime, t2 As DateTime) As Int64
|
|---|
| 373 | Return t1.Ticks - t2.Ticks
|
|---|
| 374 | End Function
|
|---|
| 375 |
|
|---|
| 376 | /*!
|
|---|
| 377 | @brief 等しいどうかを取得する
|
|---|
| 378 | @param 比較する値
|
|---|
| 379 | @retval True 等しい
|
|---|
| 380 | @retval False 等しくない
|
|---|
| 381 | */
|
|---|
| 382 | Function Equals(value As DateTime) As Boolean
|
|---|
| 383 | If value.m_Date = m_Date Then
|
|---|
| 384 | Return True
|
|---|
| 385 | Else
|
|---|
| 386 | Return False
|
|---|
| 387 | End If
|
|---|
| 388 | End Function
|
|---|
| 389 |
|
|---|
| 390 | /*!
|
|---|
| 391 | @brief 等しいどうかを取得する
|
|---|
| 392 | @param 比較される値
|
|---|
| 393 | @param 比較する値
|
|---|
| 394 | @retval True 等しい
|
|---|
| 395 | @retval False 等しくない
|
|---|
| 396 | */
|
|---|
| 397 | Static Function Equals(t1 As DateTime, t2 As DateTime) As Boolean
|
|---|
| 398 | If t1.m_Date = t2.m_Date Then
|
|---|
| 399 | Return True
|
|---|
| 400 | Else
|
|---|
| 401 | Return False
|
|---|
| 402 | End If
|
|---|
| 403 | End Function
|
|---|
| 404 |
|
|---|
| 405 | /*!
|
|---|
| 406 | @brief ハッシュコードを取得する
|
|---|
| 407 | @return ハッシュコード
|
|---|
| 408 | */
|
|---|
| 409 | Override Function GetHashCode() As Long
|
|---|
| 410 | Return HIDWORD(m_Date) Xor LODWORD(m_Date)
|
|---|
| 411 | End Function
|
|---|
| 412 |
|
|---|
| 413 | /*!
|
|---|
| 414 | @brief 時刻を進める
|
|---|
| 415 | @param 進める時間
|
|---|
| 416 | @return 進めた後の時刻
|
|---|
| 417 | */
|
|---|
| 418 | Function Add(value As TimeSpan) As DateTime
|
|---|
| 419 | Return This + value
|
|---|
| 420 | End Function
|
|---|
| 421 |
|
|---|
| 422 | /*!
|
|---|
| 423 | @brief 時刻を進める
|
|---|
| 424 | @param 進める時間(100ナノ秒単位)
|
|---|
| 425 | @return 進めた後の時刻
|
|---|
| 426 | */
|
|---|
| 427 | Function AddTicks(value As Int64) As DateTime
|
|---|
| 428 | Return New DateTime(Ticks + value, Kind )
|
|---|
| 429 | End Function
|
|---|
| 430 |
|
|---|
| 431 | /*!
|
|---|
| 432 | @brief 時刻を進める
|
|---|
| 433 | @param 進める時間(ミリ秒単位)
|
|---|
| 434 | @return 進めた後の時刻
|
|---|
| 435 | */
|
|---|
| 436 | Function AddMilliseconds(value As Double) As DateTime
|
|---|
| 437 | Return AddTicks((value * TimeSpan.TicksPerMillisecond) As Int64)
|
|---|
| 438 | End Function
|
|---|
| 439 |
|
|---|
| 440 | /*!
|
|---|
| 441 | @brief 時刻を進める
|
|---|
| 442 | @param 進める時間(秒単位)
|
|---|
| 443 | @return 進めた後の時刻
|
|---|
| 444 | */
|
|---|
| 445 | Function AddSeconds(value As Double) As DateTime
|
|---|
| 446 | Return AddTicks((value * TimeSpan.TicksPerSecond) As Int64)
|
|---|
| 447 | End Function
|
|---|
| 448 |
|
|---|
| 449 | /*!
|
|---|
| 450 | @brief 時刻を進める
|
|---|
| 451 | @param 進める時間(分単位)
|
|---|
| 452 | @return 進めた後の時刻
|
|---|
| 453 | */
|
|---|
| 454 | Function AddMinutes(value As Double) As DateTime
|
|---|
| 455 | Return AddTicks((value * TimeSpan.TicksPerMinute) As Int64)
|
|---|
| 456 | End Function
|
|---|
| 457 |
|
|---|
| 458 | /*!
|
|---|
| 459 | @brief 時刻を進める
|
|---|
| 460 | @param 進める時間(時単位)
|
|---|
| 461 | @return 進めた後の時刻
|
|---|
| 462 | */
|
|---|
| 463 | Function AddHours(value As Double) As DateTime
|
|---|
| 464 | Return AddTicks((value * TimeSpan.TicksPerHour) As Int64)
|
|---|
| 465 | End Function
|
|---|
| 466 |
|
|---|
| 467 | /*!
|
|---|
| 468 | @brief 時刻を進める
|
|---|
| 469 | @param 進める時間(日単位)
|
|---|
| 470 | @return 進めた後の時刻
|
|---|
| 471 | */
|
|---|
| 472 | Function AddDays(value As Double) As DateTime
|
|---|
| 473 | Return AddTicks((value * TimeSpan.TicksPerDay) As Int64)
|
|---|
| 474 | End Function
|
|---|
| 475 |
|
|---|
| 476 | /*!
|
|---|
| 477 | @brief 時刻を進める
|
|---|
| 478 | @param 進める時間(年単位)
|
|---|
| 479 | @return 進めた後の時刻
|
|---|
| 480 | */
|
|---|
| 481 | Function AddYears(value As Double) As DateTime
|
|---|
| 482 | Dim date = New DateTime(Year + Int(value), Month, Day, Hour, Minute, Second, Millisecond, Kind)
|
|---|
| 483 | Dim ticks = Ticks _
|
|---|
| 484 | - (yearToDay(Year) + totalDaysOfMonth(Year, Month - 1) + Day - 1) * TimeSpan.TicksPerDay _
|
|---|
| 485 | - Hour * TimeSpan.TicksPerHour _
|
|---|
| 486 | - Minute * TimeSpan.TicksPerMinute _
|
|---|
| 487 | - Second * TimeSpan.TicksPerSecond _
|
|---|
| 488 | - Millisecond * TimeSpan.TicksPerMillisecond As Int64
|
|---|
| 489 | If IsLeapYear(Year + Int(value)) Then
|
|---|
| 490 | ticks += ( (value - Int(value)) * 366 * TimeSpan.TicksPerDay ) As Int64
|
|---|
| 491 | Else
|
|---|
| 492 | ticks += ( (value - Int(value)) * 365 * TimeSpan.TicksPerDay ) As Int64
|
|---|
| 493 | End If
|
|---|
| 494 | Return date.AddTicks(ticks)
|
|---|
| 495 | End Function
|
|---|
| 496 |
|
|---|
| 497 | /*!
|
|---|
| 498 | @brief 時刻の差を取得する
|
|---|
| 499 | @param 比較する値
|
|---|
| 500 | @return 時刻の差
|
|---|
| 501 | */
|
|---|
| 502 | Function Subtract(value As DateTime) As TimeSpan
|
|---|
| 503 | Return This - value
|
|---|
| 504 | End Function
|
|---|
| 505 |
|
|---|
| 506 | /*!
|
|---|
| 507 | @brief 時刻を戻す
|
|---|
| 508 | @param 戻す時間
|
|---|
| 509 | @return 時刻
|
|---|
| 510 | */
|
|---|
| 511 | Function Subtract(value As TimeSpan) As DateTime
|
|---|
| 512 | Return This - value
|
|---|
| 513 | End Function
|
|---|
| 514 |
|
|---|
| 515 | /*!
|
|---|
| 516 | @brief 指定した年月が、その月の何日目かを取得する
|
|---|
| 517 | @param 西暦
|
|---|
| 518 | @param 月
|
|---|
| 519 | @return 日数
|
|---|
| 520 | */
|
|---|
| 521 | Static Function DaysInMonth(year As Long, month As Long) As Long
|
|---|
| 522 | If year < 1 Or year > 9999 Then
|
|---|
| 523 | Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "year")
|
|---|
| 524 | End If
|
|---|
| 525 | If month < 1 Or month > 12 Then
|
|---|
| 526 | Throw New ArgumentOutOfRangeException("DateTime.DaysInMonth: One or more arguments are out of range value.", "month")
|
|---|
| 527 | End If
|
|---|
| 528 |
|
|---|
| 529 | If IsLeapYear(year) And month = 2 Then
|
|---|
| 530 | Return 29
|
|---|
| 531 | Else
|
|---|
| 532 | Dim daysInMonth[11] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] As Byte
|
|---|
| 533 | Return daysInMonth[month - 1]
|
|---|
| 534 | End If
|
|---|
| 535 | End Function
|
|---|
| 536 |
|
|---|
| 537 | /*!
|
|---|
| 538 | @brief 年が閏年かどうかを取得する
|
|---|
| 539 | @param 西暦
|
|---|
| 540 | @retval True 閏年
|
|---|
| 541 | @retval False 閏年でない
|
|---|
| 542 | */
|
|---|
| 543 | Static Function IsLeapYear(year As Long) As Boolean
|
|---|
| 544 | If (year Mod 400) = 0 Then Return True
|
|---|
| 545 | If (year Mod 100) = 0 Then Return False
|
|---|
| 546 | If (year Mod 4) = 0 Then Return True
|
|---|
| 547 | Return False
|
|---|
| 548 | End Function
|
|---|
| 549 |
|
|---|
| 550 | /*!
|
|---|
| 551 | @brief 時刻を文字列に変換する
|
|---|
| 552 | @return 時刻を表した文字列
|
|---|
| 553 | */
|
|---|
| 554 | Function GetDateTimeFormats() As String
|
|---|
| 555 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
|
|---|
| 556 | Dim dateFormatSize = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, NULL, NULL, 0)
|
|---|
| 557 | Dim timeFormatSize = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, NULL, NULL, 0)
|
|---|
| 558 | Dim strLength = dateFormatSize + timeFormatSize
|
|---|
| 559 | Dim dateTimeFormats = GC_malloc_atomic(SizeOf (TCHAR) * (strLength)) As PTSTR
|
|---|
| 560 | GetDateFormat(LOCALE_USER_DEFAULT, 0, time, NULL, dateTimeFormats, dateFormatSize)
|
|---|
| 561 | dateTimeFormats[dateFormatSize - 1] = &H20 As TCHAR 'Asc(" ") As TCHAR
|
|---|
| 562 | GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, NULL, dateTimeFormats + dateFormatSize, timeFormatSize)
|
|---|
| 563 | Return New String(dateTimeFormats, strLength)
|
|---|
| 564 | End Function
|
|---|
| 565 |
|
|---|
| 566 | /*!
|
|---|
| 567 | @brief 時刻を指定した書式で文字列に変換する
|
|---|
| 568 | @param 書式
|
|---|
| 569 | @return 時刻を表した文字列
|
|---|
| 570 | */
|
|---|
| 571 | Function GetDateTimeFormats(format As *TCHAR) As String
|
|---|
| 572 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生
|
|---|
| 573 | Dim dateFormatSize = GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, NULL, 0)
|
|---|
| 574 | Dim dateFormats = malloc(dateFormatSize) As PTSTR
|
|---|
| 575 | GetDateFormat(LOCALE_USER_DEFAULT, 0, time, format, dateFormats, dateFormatSize)
|
|---|
| 576 |
|
|---|
| 577 | Dim dateTimeFormatSize = GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, dateFormats, NULL, 0)
|
|---|
| 578 | Dim dateTimeFormats = malloc(dateTimeFormatSize) As PTSTR
|
|---|
| 579 | GetTimeFormat(LOCALE_USER_DEFAULT, 0, time, dateFormats, dateTimeFormats, dateTimeFormatSize)
|
|---|
| 580 |
|
|---|
| 581 | Return New String(dateTimeFormats)
|
|---|
| 582 | End Function
|
|---|
| 583 |
|
|---|
| 584 | /*!
|
|---|
| 585 | @brief バイナリデータからDateTimeを作成する
|
|---|
| 586 | @return DateTimeクラス
|
|---|
| 587 | */
|
|---|
| 588 | Static Function FromBinary(date As Int64) As DateTime
|
|---|
| 589 | Return New DateTime((date And &H3FFFFFFFFFFFFFFF), kindFromBinary(date))
|
|---|
| 590 | End Function
|
|---|
| 591 |
|
|---|
| 592 | /*!
|
|---|
| 593 | @brief バイナリデータに変換する
|
|---|
| 594 | @return バイナリデータ
|
|---|
| 595 | */
|
|---|
| 596 | Function ToBinary() As Int64
|
|---|
| 597 | Return m_Date
|
|---|
| 598 | End Function
|
|---|
| 599 |
|
|---|
| 600 | /*!
|
|---|
| 601 | @brief FILETIME構造体からDateTimeを作成する
|
|---|
| 602 | @return DateTimeクラス
|
|---|
| 603 | */
|
|---|
| 604 | Static Function FromFileTime(ByRef fileTime As FILETIME) As DateTime
|
|---|
| 605 | Dim localTime As FILETIME
|
|---|
| 606 | Dim time As SYSTEMTIME
|
|---|
| 607 | FileTimeToLocalFileTime(fileTime, localTime)
|
|---|
| 608 | FileTimeToSystemTime(localTime, time)
|
|---|
| 609 | Return New DateTime(time, DateTimeKind.Local)
|
|---|
| 610 | End Function
|
|---|
| 611 |
|
|---|
| 612 | /*!
|
|---|
| 613 | @brief FILETIME構造体に変換する
|
|---|
| 614 | @return FILETIME構造体
|
|---|
| 615 | */
|
|---|
| 616 | Function ToFileTime() As FILETIME
|
|---|
| 617 | Dim time = getSystemTime() 'As SYSTEMTIME を記述すると内部エラーが発生 rev.207
|
|---|
| 618 | Dim fileTime As FILETIME
|
|---|
| 619 | SystemTimeToFileTime(time, fileTime)
|
|---|
| 620 | Return fileTime
|
|---|
| 621 | End Function
|
|---|
| 622 |
|
|---|
| 623 | /*!
|
|---|
| 624 | @brief UTC時刻を表すFILETIME構造体からDateTimeを作成する
|
|---|
| 625 | @return DateTimeクラス
|
|---|
| 626 | */
|
|---|
| 627 | Static Function FromFileTimeUtc(ByRef fileTime As FILETIME) As DateTime
|
|---|
| 628 | Dim time As SYSTEMTIME
|
|---|
| 629 | FileTimeToSystemTime(fileTime, time)
|
|---|
| 630 | Return New DateTime(time, DateTimeKind.Utc)
|
|---|
| 631 | End Function
|
|---|
| 632 |
|
|---|
| 633 | /*!
|
|---|
| 634 | @brief UTC時刻のFILETIME構造体に変換する
|
|---|
| 635 | @return FILETIME構造体
|
|---|
| 636 | */
|
|---|
| 637 | Function ToFileTimeUtc() As FILETIME
|
|---|
| 638 | Dim fileTime = ToFileTime() 'As FILETIME を記述すると内部エラー rev.207
|
|---|
| 639 | If Kind = DateTimeKind.Utc Then
|
|---|
| 640 | Return fileTime
|
|---|
| 641 | Else
|
|---|
| 642 | Dim utcTime As FILETIME
|
|---|
| 643 | LocalFileTimeToFileTime(fileTime, utcTime)
|
|---|
| 644 | Return utcTime
|
|---|
| 645 | End If
|
|---|
| 646 | End Function
|
|---|
| 647 |
|
|---|
| 648 | /*!
|
|---|
| 649 | @brief 現地時刻に変換する
|
|---|
| 650 | @return 現地時刻に変換したDateTime
|
|---|
| 651 | */
|
|---|
| 652 | Function ToLocalTime() As DateTime
|
|---|
| 653 | If Kind = DateTimeKind.Local Then
|
|---|
| 654 | Return New DateTime(This)
|
|---|
| 655 | Else
|
|---|
| 656 | Dim fileTime = ToFileTime() '直接入れると計算できなくなります。 rev.207
|
|---|
| 657 | Return DateTime.FromFileTime(fileTime)
|
|---|
| 658 | End If
|
|---|
| 659 | End Function
|
|---|
| 660 |
|
|---|
| 661 | /*!
|
|---|
| 662 | @brief このインスタンスを文字列で取得する
|
|---|
| 663 | @return 文字列
|
|---|
| 664 | */
|
|---|
| 665 | Override Function ToString() As String
|
|---|
| 666 | Return GetDateTimeFormats()
|
|---|
| 667 | End Function
|
|---|
| 668 |
|
|---|
| 669 | /*!
|
|---|
| 670 | @brief 世界協定時刻(UTC)に変換する
|
|---|
| 671 | @return 世界協定時刻(UTC)に変換したDateTime
|
|---|
| 672 | */
|
|---|
| 673 | Function ToUniversalTime() As DateTime
|
|---|
| 674 | If Kind = DateTimeKind.Utc Then
|
|---|
| 675 | Return New DateTime(m_Date)
|
|---|
| 676 | Else
|
|---|
| 677 | Dim fileTime = ToFileTimeUtc() '直接入れると計算できなくなります。 rev.207
|
|---|
| 678 | Return DateTime.FromFileTimeUtc(fileTime)
|
|---|
| 679 | End If
|
|---|
| 680 | End Function
|
|---|
| 681 |
|
|---|
| 682 | '----------------------------------------------------------------
|
|---|
| 683 | ' プライベート メソッド
|
|---|
| 684 | '----------------------------------------------------------------
|
|---|
| 685 | Private
|
|---|
| 686 |
|
|---|
| 687 | /*!
|
|---|
| 688 | @brief インスタンスを初期化する
|
|---|
| 689 | @param 時刻(100ナノ秒単位)
|
|---|
| 690 | @param 時刻の種類
|
|---|
| 691 | */
|
|---|
| 692 | Sub initialize(ticks As Int64, kind As DateTimeKind)
|
|---|
| 693 | Kind = kind
|
|---|
| 694 | Ticks = ticks
|
|---|
| 695 | End Sub
|
|---|
| 696 |
|
|---|
| 697 | /*!
|
|---|
| 698 | @brief インスタンスを初期化する
|
|---|
| 699 | @param 西暦
|
|---|
| 700 | @param 月
|
|---|
| 701 | @param 日
|
|---|
| 702 | @param 時
|
|---|
| 703 | @param 分
|
|---|
| 704 | @param 秒
|
|---|
| 705 | @param ミリ秒
|
|---|
| 706 | @param 時刻の種類
|
|---|
| 707 | */
|
|---|
| 708 | Sub initialize(year As Long, month As Long, day As Long, hour As Long, minute As Long, second As Long, millisecond As Long, kind As DateTimeKind)
|
|---|
| 709 | If month < 1 Or month > 12 Then
|
|---|
| 710 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "month")
|
|---|
| 711 | End If
|
|---|
| 712 | If day < 1 Or day > DaysInMonth(year, month) Then
|
|---|
| 713 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "day")
|
|---|
| 714 | End If
|
|---|
| 715 | If hour < 0 Or hour => 24 Then
|
|---|
| 716 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "hour")
|
|---|
| 717 | End If
|
|---|
| 718 | If minute < 0 Or minute => 60 Then
|
|---|
| 719 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "minute")
|
|---|
| 720 | End If
|
|---|
| 721 | If second < 0 Or second => 60 Then
|
|---|
| 722 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "second")
|
|---|
| 723 | End If
|
|---|
| 724 | If millisecond < 0 Or millisecond => 1000 Then
|
|---|
| 725 | Throw New ArgumentOutOfRangeException("DateTime.initialize: One or more arguments are out of range value.", "millisecond")
|
|---|
| 726 | End If
|
|---|
| 727 |
|
|---|
| 728 | initialize(
|
|---|
| 729 | yearToDay(year) * TimeSpan.TicksPerDay _
|
|---|
| 730 | + totalDaysOfMonth(year, month - 1) * TimeSpan.TicksPerDay _
|
|---|
| 731 | + (day - 1) * TimeSpan.TicksPerDay _
|
|---|
| 732 | + hour * TimeSpan.TicksPerHour _
|
|---|
| 733 | + minute * TimeSpan.TicksPerMinute _
|
|---|
| 734 | + second * TimeSpan.TicksPerSecond _
|
|---|
| 735 | + millisecond * TimeSpan.TicksPerMillisecond,
|
|---|
| 736 | kind
|
|---|
| 737 | )
|
|---|
| 738 | End Sub
|
|---|
| 739 |
|
|---|
| 740 | /*!
|
|---|
| 741 | @brief 時刻を設定する
|
|---|
| 742 | @param 時刻(100ナノ秒単位)
|
|---|
| 743 | */
|
|---|
| 744 | Sub Ticks(value As Int64)
|
|---|
| 745 | If value < MinValue Or value > MaxValue Then
|
|---|
| 746 | Throw New ArgumentOutOfRangeException("DateTime.value: One or more arguments are out of range value.", "value")
|
|---|
| 747 | End If
|
|---|
| 748 |
|
|---|
| 749 | Dim temp = Kind As DateTimeKind
|
|---|
| 750 | m_Date = value
|
|---|
| 751 | Kind = temp
|
|---|
| 752 | End Sub
|
|---|
| 753 |
|
|---|
| 754 | /*!
|
|---|
| 755 | @brief 時刻の種類を設定する
|
|---|
| 756 | @param 時刻の種類
|
|---|
| 757 | */
|
|---|
| 758 | Sub Kind(kind As DateTimeKind)
|
|---|
| 759 | Dim temp As Int64
|
|---|
| 760 | temp = kind
|
|---|
| 761 | temp = (temp << 62) And &HC000000000000000
|
|---|
| 762 | m_Date = (m_Date And &H3FFFFFFFFFFFFFFF) Or temp
|
|---|
| 763 | End Sub
|
|---|
| 764 |
|
|---|
| 765 | /*!
|
|---|
| 766 | @brief バイナリデータから時刻の種類を取得する
|
|---|
| 767 | @return 時刻の種類
|
|---|
| 768 | */
|
|---|
| 769 | Static Function kindFromBinary(date As Int64) As DateTimeKind
|
|---|
| 770 | date = (date >> 62) And &H03
|
|---|
| 771 | If date = &H01 Then
|
|---|
| 772 | Return DateTimeKind.Local
|
|---|
| 773 | ElseIf date = &H02 Then
|
|---|
| 774 | Return DateTimeKind.Unspecified
|
|---|
| 775 | ElseIf date = &H03 Then
|
|---|
| 776 | Return DateTimeKind.Utc
|
|---|
| 777 | End If
|
|---|
| 778 | End Function
|
|---|
| 779 |
|
|---|
| 780 | /*!
|
|---|
| 781 | @brief インスタンスをSYSTEMTIME構造体に変換する
|
|---|
| 782 | @return SYSTEMTIME構造体
|
|---|
| 783 | */
|
|---|
| 784 | Function getSystemTime() As SYSTEMTIME
|
|---|
| 785 | Dim dayOfWeek = DayOfWeek As Long
|
|---|
| 786 | Dim time As SYSTEMTIME
|
|---|
| 787 | With time
|
|---|
| 788 | .wYear = Year As Word
|
|---|
| 789 | .wMonth = Month As Word
|
|---|
| 790 | .wDayOfWeek = dayOfWeek As Word
|
|---|
| 791 | .wDay = Day As Word
|
|---|
| 792 | .wHour = Hour As Word
|
|---|
| 793 | .wMinute = Minute As Word
|
|---|
| 794 | .wSecond = Second As Word
|
|---|
| 795 | .wMilliseconds = Millisecond As Word
|
|---|
| 796 | End With
|
|---|
| 797 | Return time
|
|---|
| 798 | End Function
|
|---|
| 799 |
|
|---|
| 800 | /*!
|
|---|
| 801 | @brief 西暦から日数に変換する
|
|---|
| 802 | @return 日数
|
|---|
| 803 | */
|
|---|
| 804 | Static Function yearToDay(year As Long) As Long
|
|---|
| 805 | year--
|
|---|
| 806 | Return (Int(year * 365.25) - Int(year * 0.01) + Int(year * 0.0025))
|
|---|
| 807 | End Function
|
|---|
| 808 |
|
|---|
| 809 | /*!
|
|---|
| 810 | @brief その月までその年の元旦から何日経っているかを取得する
|
|---|
| 811 | @return 日数
|
|---|
| 812 | */
|
|---|
| 813 | Static Function totalDaysOfMonth(year As Long, month As Long) As Long
|
|---|
| 814 | Dim days As Long
|
|---|
| 815 | Dim i As Long
|
|---|
| 816 | For i = 1 To month
|
|---|
| 817 | days += DaysInMonth(year, i)
|
|---|
| 818 | Next
|
|---|
| 819 | Return days
|
|---|
| 820 | End Function
|
|---|
| 821 | End Class
|
|---|
| 822 |
|
|---|
| 823 | End Namespace
|
|---|