Date and Time Derivative Models

H.Necessaire provides models for handling periods of time and inexact times and dates.

Date and Time Derivative Models
Photo by Christina Isabella / Unsplash

H.Necessaire provides models for handling periods of time and inexact times and dates.

Useful for scenarios like recurrence info, specific periods of time, approximate periods of time like historical data or incomplete details of certain subjects, filtering by certain date-time parts, etc.

These models are as follows:


dotnet add package H.Necessaire

H.Necessaire

PartialDateTime

Used for holding information about a possibly incomplete date and time.

Compared to DateTime, it can basically have any missing piece... like no year, no month, no day, no minute, etc.

Useful, for instance, when you plan some event in the future and you know the year and month only, not the exact date; or when you only know the birth year of someone; or when you deal with some historical data for which you don't know the exact date; or when you need to define a recurrence plan; etc. You get the idea.

public class PartialDateTime
{
    int? Year
    int? Month
    int? DayOfMonth
    
    int? Hour
    int? Minute
    int? Second
    
    int? Millisecond
    
    DateTimeKind DateTimeKind
    
    DayOfWeek[] WeekDays
}

PartialDateTime Model

Usage

Here's an example of using the PartialDateTime to represent a recurrent alarm

// Easily configure a daily alarm via PartialDateTime
PartialDateTime dailyAlarm = new PartialDateTime {
  Hour = 10,
  Minute = 30,
  DateTimeKind = DateTimeKind.Local,
};

Store a daily alarm info with PartialDateTime

Full working sample on GitHub Samples Repo:

H.Necessaire.Usage.Samples/Src/H.Necessaire.Samples/H.Necessaire.Samples.DateTimeDerivatives/Program.cs at master · hinteadan/H.Necessaire.Usage.Samples
Usage Samples for H.Necessaire Docs WebSite. Contribute to hinteadan/H.Necessaire.Usage.Samples development by creating an account on GitHub.

PeriodOfTime

Useful for holding an exact period of time.

Usefulness is obvious, for when you need to manage a precise period of time, say „The movie Blood Diamond will show from 2010-01-01 08:00:00 to 2010-02-01 16:30:42.

public class PeriodOfTime
{
    DateTime? From    
    DateTime? To
    
    TimeSpan? Duration
    
    bool IsSinceForever
    bool IsUntilForever
    bool IsInfinite
    bool IsTimeless
}

Usage

Simple example, let's model the entire current day

PeriodOfTime today = new PeriodOfTime {
  From = DateTime.Today,
  To = DateTime.Today.AddDays(1).AddMilliseconds(-1),
};

PartialPeriodOfTime

Useful for holding an inexact period of time.

It's similar to PeriodOfTime but the intervals are defined by PartialDateTime.

public class PartialPeriodOfTime
{
    PartialDateTime From 
    PartialDateTime To
    
    DayOfWeek[] WeekDays
    
    TimeSpan? Duration
    
    bool IsSinceForever
    bool IsUntilForever
    bool IsInfinite
    bool IsTimeless
}

Usage

As an example, let's model the working business hours.

PartialPeriodOfTime businessHours = new PartialPeriodOfTime
{
    From = new PartialDateTime {
        Hour = 9,
        Minute = 0,
        DateTimeKind = DateTimeKind.Local,
    },
    To = new PartialDateTime {
        Hour = 21,
        Minute = 0,
        DateTimeKind = DateTimeKind.Local,
    },
}
.OnWeekDays([
  DayOfWeek.Monday,
  DayOfWeek.Tuesday,
  DayOfWeek.Wednesday, 
  DayOfWeek.Thursday,
  DayOfWeek.Friday
]);

Modelling working business hours using PartialPeriodOfTime

Full working sample on GitHub Samples Repo:

H.Necessaire.Usage.Samples/Src/H.Necessaire.Samples/H.Necessaire.Samples.DateTimeDerivatives/Program.cs at master · hinteadan/H.Necessaire.Usage.Samples
Usage Samples for H.Necessaire Docs WebSite. Contribute to hinteadan/H.Necessaire.Usage.Samples development by creating an account on GitHub.

ApproximatePeriodOfTime

Very similar to PartialPeriodOfTime, useful for holding inexact periods of time but when the start and end dates are in a known time-frame.

For example when storing fuzzy historical events, say "Jesus lived somewhere between 6-4 BC and 30-33 AC".

public class ApproximatePeriodOfTime
{
    PartialPeriodOfTime StartPeriod
    PartialPeriodOfTime EndPeriod

    TimeSpan? MinimumDuration
    TimeSpan? MaximumDuration
    TimeSpan? AverageDuration
    
    bool IsSinceForever
    bool IsUntilForever
    bool IsInfinite
    bool IsTimeless
}

Usage

As an example let's model a rough road-trip plan

ApproximatePeriodOfTime roadtripPlanning = new ApproximatePeriodOfTime
{
    StartPeriod = new PartialDateTime {
        Year = DateTime.Today.Year,
        Month = 4,
        DateTimeKind = DateTimeKind.Local,
    },
    EndPeriod = new PartialDateTime {
        Year = DateTime.Today.Year,
        Month = 8,
        DateTimeKind = DateTimeKind.Local,
    },
};

PartialDateTime is implicitly casted to PartialPeriodOfTime

Full working sample on GitHub Samples Repo:

H.Necessaire.Usage.Samples/Src/H.Necessaire.Samples/H.Necessaire.Samples.DateTimeDerivatives/Program.cs at master · hinteadan/H.Necessaire.Usage.Samples
Usage Samples for H.Necessaire Docs WebSite. Contribute to hinteadan/H.Necessaire.Usage.Samples development by creating an account on GitHub.