Date, time and timer

The library provides lightweight date/time primitives and a disposable timer.

Date

Energy.Base.Date is a lightweight date-only value type. It stores the year as a short, the month as a byte, the day as a byte, and an optional sbyte time zone.

It provides implicit conversion from System.DateTime and explicit conversion back to System.DateTime.

Energy.Base.Date date = DateTime.Now;

if (!date.IsEmpty)
{
    DateTime dt = (DateTime)date;
    Console.WriteLine("Date: {0:yyyy-MM-dd}", dt);
}

Time

Energy.Base.Time is a placeholder for a future time-only value. It currently contains no members and is reserved for future implementation.

Trap

Energy.Base.Trap is a disposable stopwatch that measures elapsed time. It can optionally raise a callback when the elapsed time exceeds a given limit.

using (Energy.Base.Trap trap = new Energy.Base.Trap(1.0))
{
    System.Threading.Thread.Sleep(500);
}

using (Energy.Base.Trap trap = new Energy.Base.Trap(1.0, (TimeSpan span) =>
{
    Console.WriteLine("Limit exceeded: {0}", span);
}))
{
    System.Threading.Thread.Sleep(1500);
}

Properties and methods

Member Description
Date.ToDateTime() Convert the date to a DateTime.
Date.IsEmpty True when all fields are zero.
Trap.Start Start timestamp.
Trap.Stop Stop timestamp.
Trap.Span Elapsed time.
Trap.Time Elapsed time in seconds.
Trap.Limit Optional limit in seconds.

See also

  • base-clock for date/time formatting and leap year helpers.