Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .NET way to store time periods like my custom class?

Tags:

date

.net

Is there a native .NET class that handles timespan like this? I haven't been able to find one.

Is there one that comes close?

Public Class Period

    Property FromDate As Date
    Property ToDate As Date

    Public Sub New(ByVal fromDate As Date, ByVal toDate As Date)

        If fromDate > toDate Then
            Throw New ArgumentException("fromDate must be less than Or equal toDate")
        End If

        _FromDate = fromDate
        _ToDate = toDate

    End Sub

    Public Overloads Shared Operator =(ByVal period1 As Period,
                                       ByVal period2 As Period) As Boolean

        Return period1.FromDate = period2.FromDate AndAlso
               period1.ToDate = period2.ToDate

    End Operator

    Public Overloads Shared Operator <>(ByVal period1 As Period,
                                        ByVal period2 As Period) As Boolean

        Return Not period1 = period2

    End Operator

    Public Overloads Shared Operator <(ByVal period1 As Period,
                                       ByVal period2 As Period) As Boolean

        Return period1.FromDate < period2.FromDate

    End Operator

    Public Overloads Shared Operator >(ByVal period1 As Period,
                                   ByVal period2 As Period) As Boolean

        Return period1.FromDate > period2.FromDate

    End Operator

    Public Overloads Shared Operator >=(ByVal period1 As Period,
                               ByVal period2 As Period) As Boolean

        Return period1.FromDate >= period2.FromDate

    End Operator

    Public Overloads Shared Operator <=(ByVal period1 As Period,
                                        ByVal period2 As Period) As Boolean

        Return period1.FromDate <= period2.FromDate

    End Operator

    Public Function Contains(ByVal checkDate As Date) As Boolean

        Return checkDate >= Me.FromDate AndAlso
               checkDate < Me.ToDate

    End Function

    Public Overrides Function ToString() As String
        Return Format(_FromDate, "MMM-yyyy") & "-" &  Format(_ToDate, "MMM-yyyy")            
    End Function

End Class

and a derived Month Period:

Public Class MonthPeriod : Inherits Period

    Private _MonthStartDate As Date

     Public Sub New(ByVal dateInMonth As Date)

        'Everything >= the 1st of the month to < the 1st of the next month
        MyBase.New(New Date(dateInMonth.Year, dateInMonth.Month, 1),
                   New Date(dateInMonth.Year, dateInMonth.Month, 1).AddMonths(1))

        _MonthStartDate = New Date(dateInMonth.Year, dateInMonth.Month, 1)

    End Sub

    Public Overrides Function ToString() As String
        Return Format(_MonthStartDate, "MMM-yyyy")
    End Function

End Class
like image 800
VJK Avatar asked Nov 12 '10 21:11

VJK


3 Answers

Short answer: I don't think there's any built-in class that's close to that. Probably the closest is TimeSpan but that's just a relative span with no concept of absolute start or end date.

like image 96
o. nate Avatar answered Oct 07 '22 04:10

o. nate


Use the .NET standard TimeSpan. If necessary, create a new class that inherits TimeSpan but adds new methods as required.

like image 42
winwaed Avatar answered Oct 07 '22 03:10

winwaed


I didn't read all your code. Your implementation may provide more functionality but the .Net Framework implementation is called TimeSpan.

It is in the System namespace.

http://msdn.microsoft.com/en-us/library/system.timespan(v=VS.90).aspx

TimeSpan is limited to days as the largest unit. i.e no measurement in terms on months or years. It could be extended if needed though.

like image 35
Gerald Davis Avatar answered Oct 07 '22 05:10

Gerald Davis