Moment#

A date and time at minute precision, stored as minutes since 1970-01-01 00:00. It has no time zone and no daylight saving time: the value is a wall-clock reading. Every date in the API is a Moment, for example TaskData.StartDate and SchedulingConfig.ProjectStart.

Build one with FromYmdHm, FromDateTime or the constructor.

Declaration#

public readonly record struct Moment(long Minutes) : IComparable<Moment>

Moment is a value type with value equality: two moments are equal when their Minutes are equal. default(Moment) is 1970-01-01 00:00, not "no date". Fields that can be unset use Moment?.

Constructors#

Moment(long)#

public Moment(long Minutes)

Creates a moment from a minute count. The value isn't checked.

Name Type Description
Minutes long Minutes since 1970-01-01 00:00. Negative values are earlier dates.

Properties#

Minutes#

public long Minutes { get; init; }

Minutes since 1970-01-01 00:00. Subtract two moments' Minutes to get the calendar distance between them in minutes.

Methods#

ToDateTime#

public DateTime ToDateTime()

Returns the moment as a DateTime with DateTimeKind.Unspecified.

Errors: throws ArgumentOutOfRangeException when the moment is outside years 1-9999, which DateTime can't hold. Read Minutes to handle such dates.

ToString#

public override string ToString()

Returns the moment as YYYY-MM-DD HH:MM, for example 2026-03-02 08:00. A year outside 0-9999 gets an explicit sign and at least four digits: +10000, -0001.

Errors: throws InvalidOperationException when Minutes is outside years -262143 to 262142.

CompareTo#

public int CompareTo(Moment other)

Orders moments chronologically by Minutes. Sorted collections such as SortedDictionary<Moment, T> use it.

Operators#

public static bool operator <(Moment a, Moment b)
public static bool operator >(Moment a, Moment b)
public static bool operator <=(Moment a, Moment b)
public static bool operator >=(Moment a, Moment b)

Compare moments chronologically. == and != come from the record struct and compare Minutes.

Static methods#

FromYmdHm#

public static Moment? FromYmdHm(int year, int month, int day, int hour, int minute)

Builds a moment from calendar fields.

Name Type Description
year int Year, -262143 to 262142.
month int Month, 1-12.
day int Day of the month; must exist in that month (no February 30).
hour int Hour, 0-23.
minute int Minute, 0-59.

Returns: the moment, or null when any part is out of range or the date doesn't exist.

FromDateTime#

public static Moment FromDateTime(DateTime value)

Builds a moment from the date and time fields of value. Seconds and fractions of a second are dropped. DateTime.Kind is ignored, so a UTC value is read as is; convert it to the wall-clock time you schedule in first.

Name Type Description
value DateTime The date and time to convert.

Returns: the moment at the same wall-clock minute.

Example#

using Plen;

Moment start = Moment.FromYmdHm(2026, 3, 2, 8, 0)
    ?? throw new ArgumentException("invalid date");

Moment fromClr = Moment.FromDateTime(new DateTime(2026, 3, 2, 17, 30, 45));

Console.WriteLine(start);                         // 2026-03-02 08:00
Console.WriteLine(fromClr);                       // 2026-03-02 17:30
Console.WriteLine(fromClr.Minutes - start.Minutes); // 570
Console.WriteLine(start < fromClr);               // True

DateTime back = start.ToDateTime();               // Kind = Unspecified
Console.WriteLine(Moment.FromYmdHm(2026, 2, 30, 0, 0) is null); // True