📄 cdatetime.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 1 'Persistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cDateTime"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2004 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: cDateTime
'
''
' Represents a point in time.
'
' @remarks The cDateTime class can represent a point in time from
' 1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM. The time is represented
' in a gregorian calendar set.
'
' <pre>
' Dim dt As cDateTime
' Set dt = NewDate(#1/1/2005 8:30:00AM#")
' </pre>
'
' @see Constructors
' @see cDateTimeStatic
' @see DateTimeFormatInfo
' @see TimeSpan
' @see IComparable
' @see IFormattable
'
Option Explicit
Implements IObject
Implements IComparable
Implements IFormattable
Private Const PROP_MILLISECONDS As String = "Milliseconds"
Private Const PROP_KIND As String = "Kind"
Private Const DEF_MILLISECONDS As Long = 0@
Private Const DEF_KIND As Long = 0
Private Const SECONDS_PER_MINUTE As Long = 60
Private Const MINUTES_PER_HOUR As Long = 60
Private Const HOURS_PER_DAY As Long = 24
Private Const MONTHS_PER_YEAR As Long = 12
Private Const MILLISECONDS_PER_SECOND As Currency = 1000@
Private Const MILLISECONDS_PER_MINUTE As Currency = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE
Private Const MILLISECONDS_PER_HOUR As Currency = MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR
Private Const MILLISECONDS_PER_DAY As Currency = MILLISECONDS_PER_HOUR * HOURS_PER_DAY
Private Const DAYS_TO_18991231 As Long = 693593
Private Const MILLISECONDS_TO_18991231 As Currency = 1@ * DAYS_TO_18991231 * MILLISECONDS_PER_DAY
Private Const DAYS_PER_YEAR As Long = 365
Private Const DAYS_PER_4_YEARS As Long = DAYS_PER_YEAR * 4 + 1
Private Const DAYS_PER_100_YEARS As Long = DAYS_PER_4_YEARS * 25 - 1
Private Const DAYS_PER_400_YEARS As Long = DAYS_PER_100_YEARS * 4 + 1
Private Const DAYS_TO_YEAR_10000 As Currency = DAYS_PER_400_YEARS * 25 - 366
Private Const MAX_MILLISECONDS As Currency = DAYS_TO_YEAR_10000 * MILLISECONDS_PER_DAY
Private Const DAYS_TO_16011231 As Currency = DAYS_PER_400_YEARS * 4
Private Const MILLISECONDS_TO_16011231 As Currency = DAYS_TO_16011231 * MILLISECONDS_PER_DAY
Private Const FILETIME_MINIMUM As Currency = 50491123200000@
' Currency works perfectly as the 64bit Integer in .NET. Even though the
' time is represented in milliseconds here, the fractional portion is exactly
' 10000 ticks, or .0001 is exactly 100 nanoseconds, just as in .NET. We get
' the precision as used in .NET and the 64bit Integer.
Private mMilliseconds As Currency
Private mKind As DateTimeKind
''
' Returns the kind of cDateTime.
'
' @return The kind.
' @remarks A <b>cDateTime</b> can be Universal Coordinated Time (UTC),
' local time, or unspecified.
'
Public Property Get Kind() As DateTimeKind
Kind = mKind
End Property
''
' Returns if this cDateTime instance is currently in daylight savings time.
'
' @return If this instance is in daylight savings.
' @remarks This uses the current time zone settings of the system to
' determine when daylight savings should be.
'
Public Property Get IsDayLightSavingsTime() As Boolean
IsDayLightSavingsTime = TimeZone.CurrentTimeZone.IsDayLightSavingTime(Me)
End Property
''
' Converts the current instance to an Ole Automation date representation
' that is normally used in Visual Basic.
'
' @return The Visual Basic compatible date.
' @remarks It is possible that this instance holds a date outside of the
' valid range of dates that Visual Basic can represent. If this happens,
' then a normal VB error will be raised during the assignment.
'
Public Function ToOADate() As Date
If mMilliseconds = 0@ Then Exit Function
If mMilliseconds < MILLISECONDS_PER_DAY Then
ToOADate = 1# * mMilliseconds / MILLISECONDS_PER_DAY
Else
Dim Days As Double
Days = Int(mMilliseconds / MILLISECONDS_PER_DAY)
Dim Time As Double
Time = (mMilliseconds - (Days * MILLISECONDS_PER_DAY)) / MILLISECONDS_PER_DAY
Days = Days - DAYS_TO_18991231
If Days < 0# Then
ToOADate = Days - Time
Else
ToOADate = Days + Time
End If
End If
End Function
''
' Returns the Year part of the current date representation.
'
' @return The year of the date.
'
Public Property Get Year() As Long
Call GetDateParts(YearPart, Year)
End Property
''
' Returns the Day part of the current date representation.
'
' @return The day of the date.
'
Public Property Get Day() As Long
Call GetDateParts(DayPart, , , Day)
End Property
''
' Returns the Month part of the current date representation.
'
' @return The month of the date.
'
Public Property Get Month() As Long
Call GetDateParts(MonthPart, , Month)
End Property
''
' Returns the day within the calendar year.
'
' @return The day within the year.
' @remarks The value is calculated using the Gregorian calendar,
' and will range from 1-365 or 1-366 for leap years.
'
Public Property Get DayOfYear() As Long
Call GetDateParts(DayOfTheYear, , , , DayOfYear)
End Property
''
' Returns the day of the week for the current date.
'
' @return The day of the week.
' @remarks The DayOfWeekEnum is zero based and therefore not
' directly compatible with vbDayOfWeek. 1 must be added to
' align to the 1-based values of vbDayOfWeek.
'
Public Property Get DayOfWeek() As DayOfWeek
DayOfWeek = Int(mMilliseconds / MILLISECONDS_PER_DAY + 1) Mod 7
End Property
''
' Returns the Hour part of the current date representation.
'
' @return The current hour.
'
Public Property Get Hour() As Long
Hour = Int(mMilliseconds / MILLISECONDS_PER_HOUR) Mod HOURS_PER_DAY
End Property
''
' Returns the Minute part of the current date representation.
'
' @return The current minute.
'
Public Property Get Minute() As Long
Minute = Int(mMilliseconds / MILLISECONDS_PER_MINUTE) Mod MINUTES_PER_HOUR
End Property
''
' Returns the Second part of the current date representation.
'
' @return The current second.
'
Public Property Get Second() As Long
Second = Modulus(Int(mMilliseconds / MILLISECONDS_PER_SECOND), SECONDS_PER_MINUTE)
End Property
''
' Returns the Millisecond part of the current date representation.
'
' @return The current millisecond.
'
Public Property Get Millisecond() As Long
Millisecond = Modulus(mMilliseconds, MILLISECONDS_PER_SECOND)
End Property
''
' Returns the whole date represented in milliseconds.
'
' @return The number of milliseconds from 1/1/0001.
' @remarks The return value is Currency and can represent fractions
' of milliseconds called ticks. There are 10000 ticks per millisecond,
' so the fractional portion is the number of ticks.
'
Public Property Get TotalMilliseconds() As Currency
TotalMilliseconds = mMilliseconds
End Property
''
' Returns the whole date represented in ticks.
'
' @return The date in ticks.
' @remarks The return value is a Decimal containing the
' the number of whole ticks.
'
Public Property Get Ticks() As Variant
Ticks = CDec(mMilliseconds) * 10000
End Property
''
' Returns the date portion of the current date representation.
'
' @return The date portion.
' @remarks The date portion does not include the time portion from the
' original date representation. The new date will have a time of 12:00AM.
'
Public Property Get DateOnly() As cDateTime
Set DateOnly = cDateTime.FromMilliseconds(mMilliseconds - Modulus(mMilliseconds, MILLISECONDS_PER_DAY))
End Property
''
' Returns the time portion of the current date representation.
'
' @return The time portion.
' @remarks The time portion does not include the original date portion.
' It now will have a date of 1/1/0001.
'
Public Property Get TimeOfDay() As TimeSpan
Set TimeOfDay = TimeSpan.FromMilliseconds(mMilliseconds - (mMilliseconds - Modulus(mMilliseconds, MILLISECONDS_PER_DAY)))
End Property
''
' Adds a specified amount of time to the current date, returning a
' new cDateTime instance with the calcuated value.
'
' @param ts The amount of time to add.
' @return The new date object containing the new calculated time.
'
Public Function Add(ByRef ts As TimeSpan) As cDateTime
If ts Is Nothing Then _
Throw Cor.NewArgumentNullException(Environment.GetResourceString(ArgumentNull_TimeSpan, "ts"), "ts")
Set Add = cDateTime.FromMilliseconds(mMilliseconds + ts.TotalMilliseconds, mKind)
End Function
''
' Adds a specified number of milliseconds to the current date, returning a
' new cDateTime instance with the calcuated value.
'
' @param value The number of milliseconds to add.
' @return The new date containing the new calculated time.
'
Public Function AddMilliseconds(ByVal Value As Double) As cDateTime
If Value < 0# Then
Value = Fix(Value - 0.5)
Else
Value = Fix(Value + 0.5)
End If
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -