'''
''' Provides a date data type that understands the concept
''' of an empty date value.
'''
'''
''' See Chapter 5 for a full discussion of the need for this
''' data type and the design choices behind it.
'''
_
Public NotInheritable Class SmartDate
Implements IComparable
Private mDate As Date
Private mEmptyIsMin As Boolean
#Region " Constructors "
'''
''' Creates a new SmartDate object.
'''
'''
''' The SmartDate created will use the min possible
''' date to represent an empty date.
'''
Public Sub New()
mEmptyIsMin = True
mDate = Date.MinValue
End Sub
'''
''' Creates a new SmartDate object.
'''
''' Indicates whether an empty date is the min or max date value.
Public Sub New(ByVal EmptyIsMin As Boolean)
mEmptyIsMin = EmptyIsMin
If mEmptyIsMin Then
mDate = Date.MinValue
Else
mDate = Date.MaxValue
End If
End Sub
'''
''' Creates a new SmartDate object.
'''
'''
''' The SmartDate created will use the min possible
''' date to represent an empty date.
'''
''' The initial value of the object.
Public Sub New(ByVal Value As Date)
mEmptyIsMin = True
mDate = Value
End Sub
'''
''' Creates a new SmartDate object.
'''
''' The initial value of the object.
''' Indicates whether an empty date is the min or max date value.
Public Sub New(ByVal Value As Date, ByVal EmptyIsMin As Boolean)
mEmptyIsMin = EmptyIsMin
mDate = Value
End Sub
'''
''' Creates a new SmartDate object.
'''
'''
''' The SmartDate created will use the min possible
''' date to represent an empty date.
'''
''' The initial value of the object (as text).
Public Sub New(ByVal Value As String)
mEmptyIsMin = True
Me.Text = Value
End Sub
'''
''' Creates a new SmartDate object.
'''
''' The initial value of the object (as text).
''' Indicates whether an empty date is the min or max date value.
Public Sub New(ByVal Value As String, ByVal EmptyIsMin As Boolean)
mEmptyIsMin = EmptyIsMin
Me.Text = Value
End Sub
#End Region
#Region " Text Support "
Private mFormat As String = "g" '"Short date"
'''
''' Gets or sets the format string used to format a date
''' value when it is returned as text.
'''
'''
''' The format string should follow the requirements for the
''' Visual Basic .NET Format() statement.
'''
''' A format string.
Public Property FormatString() As String
Get
Return mFormat
End Get
Set(ByVal Value As String)
mFormat = Value
End Set
End Property
'''
''' Gets or sets the date value.
'''
'''
'''
''' This property can be used to set the date value by passing a
''' text representation of the date. Any text date representation
''' that can be parsed by the .NET runtime is valid.
'''
''' When the date value is retrieved via this property, the text
''' is formatted by using the format specified by the
''' property. The default is the
''' "Short Date" format.
'''
'''
'''
Public Property Text() As String
Get
Return DateToString(mDate, mFormat, mEmptyIsMin)
End Get
Set(ByVal Value As String)
mDate = StringToDate(Value, mEmptyIsMin)
End Set
End Property
#End Region
#Region " Date Support "
'''
''' Gets or sets the date value.
'''
Public Property [Date]() As Date
Get
Return mDate
End Get
Set(ByVal Value As Date)
mDate = Value
End Set
End Property
#End Region
#Region " System.Object overrides "
'''
''' Returns a text representation of the date value.
'''
Public Overrides Function ToString() As String
Return Me.Text
End Function
'''
''' Returns True if the objects are equal.
'''
Public Overloads Shared Function Equals(ByVal objA As Object, ByVal objB As Object) As Boolean
If TypeOf objA Is SmartDate AndAlso TypeOf objB Is SmartDate Then
Return DirectCast(objA, SmartDate).Equals(DirectCast(objB, SmartDate))
Else
Return False
End If
End Function
'''
''' Returns True if the object is equal to this SmartDate.
'''
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is SmartDate Then
Return Me.Equals(DirectCast(obj, SmartDate))
ElseIf TypeOf obj Is Date Then
Return Me.Equals(DirectCast(obj, Date))
ElseIf TypeOf obj Is String Then
Return Me.Equals(CStr(obj))
ElseIf obj Is DBNull.Value Then
Return Me.DBValue.Equals(DBNull.Value)
Else
Return False
End If
End Function
'''
''' Returns True if the SmartDate is equal to this SmartDate.
'''
Public Overloads Function Equals(ByVal obj As SmartDate) As Boolean
Return Me.CompareTo(obj) = 0
End Function
'''
''' Returns True if the date is equal to this SmartDate.
'''
Public Overloads Function Equals(ByVal obj As Date) As Boolean
Return Me.CompareTo(obj) = 0
End Function
'''
''' Returns True if the text (as a date) is equal to this SmartDate.
'''
Public Overloads Function Equals(ByVal obj As String) As Boolean
Return Me.CompareTo(obj) = 0
End Function
'''
''' Returns a hash code for this object.
'''
Public Overrides Function GetHashCode() As Integer
Return mDate.GetHashCode
End Function
#End Region
#Region " DBValue "
''' MODIFIED BY JOHN TO SET AS WELL AS GET from an object
''this is designed to work with an infragistics datetime editor
'that defaults to DBNull for empty dates and vb "Date" type for actualy
'date selections
'''
''' Gets / Sets a database-friendly version of the date value.
'''
'''
'''
''' If the SmartDate contains an empty date, this returns DBNull. Otherwise
''' the actual date value is returned as type Date.
'''
''' This property is very useful when setting parameter values for
''' a Command object, since it automatically stores null values into
''' the database for empty date values.
'''
''' When you also use the SafeDataReader and its GetSmartDate method,
''' you can easily read a null value from the database back into a
''' SmartDate object so it remains considered as an empty date value.
'''
'''
Public Property DBValue() As Object
Get
If Me.IsEmpty Then
Return DBNull.Value
Else
Return mDate
End If
End Get
Set(ByVal Value As Object)
If Value Is DBNull.Value Then
If mEmptyIsMin Then
mDate = Date.MinValue
Else
mDate = Date.MaxValue
End If
Else
mDate = CDate(Value)
End If
End Set
End Property
#End Region
#Region " Empty Dates "
'''
''' Indicates whether this object contains an empty date.
'''
''' True if the date is empty.
Public ReadOnly Property IsEmpty() As Boolean
Get
If mEmptyIsMin Then
Return mDate.Equals(Date.MinValue)
Else
Return mDate.Equals(Date.MaxValue)
End If
End Get
End Property
'''
''' Indicates whether an empty date is the min or max possible date value.
'''
'''
''' Whether an empty date is considered to be the smallest or largest possible
''' date is only important for comparison operations. This allows you to
''' compare an empty date with a real date and get a meaningful result.
'''
''' True if an empty date is the smallest date, False if it is the largest.
Public ReadOnly Property EmptyIsMin() As Boolean
Get
Return mEmptyIsMin
End Get
End Property
#End Region
#Region " Conversion Functions "
'''
''' Converts a text date representation into a Date value.
'''
'''
''' An empty string is assumed to represent an empty date. An empty date
''' is returned as the MinValue of the Date datatype.
'''
''' The text representation of the date.
''' A Date value.
Public Shared Function StringToDate(ByVal Value As String) As Date
Return StringToDate(Value, True)
End Function
'''
''' Converts a text date representation into a Date value.
'''
'''
''' An empty string is assumed to represent an empty date. An empty date
''' is returned as the MinValue or MaxValue of the Date datatype depending
''' on the EmptyIsMin parameter.
'''
''' The text representation of the date.
''' Indicates whether an empty date is the min or max date value.
''' A Date value.
Public Shared Function StringToDate(ByVal Value As String, ByVal EmptyIsMin As Boolean) As Date
If Len(Value) = 0 Then
If EmptyIsMin Then
Return Date.MinValue
Else
Return Date.MaxValue
End If
ElseIf IsDate(Value) Then
Return CDate(Value)
Else
Select Case LCase(Trim(Value))
Case "t", "today", "."
Return Now
Case "y", "yesterday", "-"
Return DateAdd(DateInterval.Day, -1, Now)
Case "tom", "tomorrow", "+"
Return DateAdd(DateInterval.Day, 1, Now)
Case Else
Throw New ArgumentException("String value can not be converted to a date")
End Select
End If
End Function
'''
''' Converts a date value into a text representation.
'''
'''
''' The date is considered empty if it matches the min value for
''' the Date datatype. If the date is empty, this
''' method returns an empty string. Otherwise it returns the date
''' value formatted based on the FormatString parameter.
'''
''' The date value to convert.
''' The format string used to format the date into text.
''' Text representation of the date value.
Public Shared Function DateToString(ByVal Value As Date, ByVal FormatString As String) As String
Return DateToString(Value, FormatString, True)
End Function
'''
''' Converts a date value into a text representation.
'''
'''
''' Whether the date value is considered empty is determined by
''' the EmptyIsMin parameter value. If the date is empty, this
''' method returns an empty string. Otherwise it returns the date
''' value formatted based on the FormatString parameter.
'''
''' The date value to convert.
''' The format string used to format the date into text.
''' Indicates whether an empty date is the min or max date value.
''' Text representation of the date value.
Public Shared Function DateToString(ByVal Value As Date, ByVal FormatString As String, ByVal EmptyIsMin As Boolean) As String
If EmptyIsMin AndAlso Value = Date.MinValue Then
Return ""
ElseIf Not EmptyIsMin AndAlso Value = Date.MaxValue Then
Return ""
Else
Return Format(Value, FormatString)
End If
End Function
#End Region
#Region " Manipulation Functions "
'''
''' Compares one SmartDate to another.
'''
'''
''' This method works the same as the CompareTo method
''' on the Date datetype, with the exception that it
''' understands the concept of empty date values.
'''
''' The date to which we are being compared.
''' A value indicating if the comparison date is less than, equal to or greater than this date.
Public Function CompareTo(ByVal Value As SmartDate) As Integer
If Me.IsEmpty AndAlso Value.IsEmpty Then
Return 0
Else
Return mDate.CompareTo(Value.Date)
End If
End Function
'''
''' Compares one SmartDate to another.
'''
'''
''' This method works the same as the CompareTo method
''' on the Date datetype, with the exception that it
''' understands the concept of empty date values.
'''
''' The date to which we are being compared.
''' A value indicating if the comparison date is less than, equal to or greater than this date.
Public Function CompareTo(ByVal Value As Object) As Integer _
Implements IComparable.CompareTo
If TypeOf Value Is SmartDate Then
Return CompareTo(DirectCast(Value, SmartDate))
Else
Throw New ArgumentException("Value is not a SmartDate")
End If
End Function
'''
''' Compares a SmartDate to a text date value.
'''
''' The date to which we are being compared.
''' A value indicating if the comparison date is less than, equal to or greater than this date.
Public Function CompareTo(ByVal Value As String) As Integer
If Not IsDate(Value) Then
Throw New ArgumentException("Value must be a valid date")
Else
Return mDate.CompareTo(CDate(Value))
End If
End Function
'''
''' Compares a SmartDate to a date value.
'''
''' The date to which we are being compared.
''' A value indicating if the comparison date is less than, equal to or greater than this date.
Public Function CompareTo(ByVal Value As Date) As Integer
Return mDate.CompareTo(Value)
End Function
'''
''' Adds a TimeSpan onto the object.
'''
Public Sub Add(ByVal Value As TimeSpan)
mDate = mDate.Add(Value)
End Sub
'''
''' Subtracts a TimeSpan from the object.
'''
Public Sub Subtract(ByVal Value As TimeSpan)
mDate = mDate.Subtract(Value)
End Sub
#End Region
End Class