diff --git a/MEOS.NET.Tests/TimeCastTests.cs b/MEOS.NET.Tests/TimeCastTests.cs index 3794148..9ffdc4c 100644 --- a/MEOS.NET.Tests/TimeCastTests.cs +++ b/MEOS.NET.Tests/TimeCastTests.cs @@ -1,3 +1,4 @@ +using MEOS.NET.Functions; using MEOS.NET.Types; namespace MEOS.NET.Tests @@ -64,5 +65,62 @@ public void LastDateOfDotNetRoundTrips() Assert.AreEqual(day, ((DateSpan)span).Lower()); } + + [TestMethod] + public void IntervalOfDaysAndTimeIsATimeSpan() + { + IntPtr interval = Meos.IntervalIn("2 days 03:00:00.5", -1); + + Assert.AreEqual(new TimeSpan(2, 3, 0, 0, 500), MEOSConvert.ToTimeSpan(interval)); + } + + [TestMethod] + public void TimeSpanIsAnIntervalOfWholeDaysAndTheRest() + { + IntPtr interval = MEOSConvert.ToInterval(TimeSpan.FromHours(36)); + + Assert.AreEqual("1 day 12:00:00", Meos.IntervalOut(interval)); + } + + [TestMethod] + public void IntervalWithMonthsIsRefused() + { + IntPtr interval = Meos.IntervalIn("1 mon 2 days", -1); + + Assert.ThrowsException( + () => MEOSConvert.ToTimeSpan(interval)); + } + + [TestMethod] + public void TimeRoundTripsAsATimeOnly() + { + long time = Meos.TimeIn("13:45:30.5", -1); + + TimeOnly clock = MEOSConvert.ToTimeOnly(time); + + Assert.AreEqual(new TimeOnly(13, 45, 30, 500), clock); + Assert.AreEqual("13:45:30.5", Meos.TimeOut(MEOSConvert.ToTimeADT(clock))); + } + + [TestMethod] + public void EndOfDayIsRefusedByATimeOnly() + { + long time = Meos.TimeIn("24:00:00", -1); + + Assert.ThrowsException( + () => MEOSConvert.ToTimeOnly(time)); + } + + [TestMethod] + public void TimeWithTimeZoneRoundTripsAsATimeAndItsOffset() + { + IntPtr timetz = Meos.TimetzIn("13:45:30+02", -1); + + (TimeOnly Time, TimeSpan Offset) parts = MEOSConvert.ToTimeWithOffset(timetz); + + Assert.AreEqual(new TimeOnly(13, 45, 30), parts.Time); + Assert.AreEqual(TimeSpan.FromHours(2), parts.Offset); + Assert.AreEqual("13:45:30+02", Meos.TimetzOut(MEOSConvert.ToTimetzADT(parts))); + } } } diff --git a/MEOS.NET/Types/MEOSConvert.g.cs b/MEOS.NET/Types/MEOSConvert.g.cs index 98e8662..33f303a 100644 --- a/MEOS.NET/Types/MEOSConvert.g.cs +++ b/MEOS.NET/Types/MEOSConvert.g.cs @@ -12,9 +12,12 @@ namespace MEOS.NET.Types /// DateOnly count from 0001-01-01, MEOS from 2000-01-01, and both sides /// state the Unix epoch exactly. A DateTime counts ticks of 100 ns, so one /// below a microsecond, which no MEOS timestamp holds, is refused; a value - /// the other side cannot state is refused by its constructor. + /// the other side cannot state is refused by its constructor. An interval, + /// a time and a time with time zone cast through their parts: a TimeSpan, + /// a TimeOnly and the pair of a TimeOnly and its offset, which .NET holds + /// in no single type. [System.CodeDom.Compiler.GeneratedCode("MEOS.NET.ObjectGen", "0.1.0")] - internal static class MEOSConvert + public static class MEOSConvert { private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000; @@ -41,14 +44,14 @@ private static DateTime FromUnixMicroseconds(long us, DateTimeKind kind) checked(us * TicksPerMicrosecond)), kind); /// A MEOS TimestampTz as a DateTime of UTC kind. - internal static DateTime ToDateTime(long timestamptz) + public static DateTime ToDateTime(long timestamptz) => FromUnixMicroseconds(Meos.TimestamptzToUnixUs(timestamptz), DateTimeKind.Utc); /// A DateTime as a MEOS TimestampTz: a moment of UTC or local /// kind at its instant, one of unspecified kind read in the zone of the /// MEOS session, as MEOS reads a timestamp without an offset. - internal static long ToTimestampTz(DateTime moment) + public static long ToTimestampTz(DateTime moment) => moment.Kind == DateTimeKind.Unspecified ? Meos.TimestampToTimestamptz( Meos.UnixUsToTimestamp(UnixMicroseconds(moment))) @@ -56,22 +59,142 @@ internal static long ToTimestampTz(DateTime moment) /// A MEOS Timestamp, which has no time zone, as a DateTime of /// unspecified kind. - internal static DateTime ToUnzonedDateTime(long timestamp) + public static DateTime ToUnzonedDateTime(long timestamp) => FromUnixMicroseconds(Meos.TimestampToUnixUs(timestamp), DateTimeKind.Unspecified); /// A DateTime as a MEOS Timestamp: the clock it reads. - internal static long ToTimestamp(DateTime moment) + public static long ToTimestamp(DateTime moment) => Meos.UnixUsToTimestamp(UnixMicroseconds(moment)); /// A MEOS DateADT as a DateOnly. - internal static DateOnly ToDateOnly(int date) + public static DateOnly ToDateOnly(int date) => DateOnly.FromDayNumber(checked(UnixEpochDayNumber + Meos.DateToUnixDays(date))); /// A DateOnly as a MEOS DateADT. - internal static int ToDateADT(DateOnly day) + public static int ToDateADT(DateOnly day) => Meos.UnixDaysToDate(day.DayNumber - UnixEpochDayNumber); + private const long MicrosecondsPerDay = TimeSpan.TicksPerDay / TicksPerMicrosecond; + + /// The whole microseconds of a count of ticks, refusing a tick + /// below a microsecond. + private static long WholeMicroseconds(long ticks, object value, string name) + { + if (ticks % TicksPerMicrosecond != 0) + { + throw new ArgumentOutOfRangeException(name, value, + "A MEOS value holds whole microseconds."); + } + + return ticks / TicksPerMicrosecond; + } + + /// A TimeSpan as a MEOS Interval, through interval_from_parts: + /// its whole days as days and the rest as microseconds. + public static IntPtr ToInterval(TimeSpan duration) + => Meos.IntervalFromParts(0, checked((int)(duration.Ticks / TimeSpan.TicksPerDay)), + WholeMicroseconds(duration.Ticks % TimeSpan.TicksPerDay, duration, + nameof(duration))); + + /// A MEOS Interval as a TimeSpan, through interval_to_parts; an + /// interval with months, which has no fixed length, or beyond the range + /// of a TimeSpan, is refused. + public static TimeSpan ToTimeSpan(IntPtr interval) + { + IntPtr parts = Marshal.AllocHGlobal(16); + try + { + Meos.IntervalToParts(interval, parts, parts + 4, parts + 8); + int months = Marshal.ReadInt32(parts); + int days = Marshal.ReadInt32(parts + 4); + long us = Marshal.ReadInt64(parts + 8); + if (months != 0) + { + throw new ArgumentOutOfRangeException(nameof(interval), months, + "A TimeSpan cannot state an interval with months."); + } + + long total = days * MicrosecondsPerDay + us; + if (total > TimeSpan.MaxValue.Ticks / TicksPerMicrosecond + || total < TimeSpan.MinValue.Ticks / TicksPerMicrosecond) + { + throw new ArgumentOutOfRangeException(nameof(interval), days, + "A TimeSpan cannot state this interval."); + } + + return TimeSpan.FromTicks(total * TicksPerMicrosecond); + } + finally + { + Marshal.FreeHGlobal(parts); + } + } + + /// A TimeOnly as a MEOS TimeADT, through time_from_parts. + public static long ToTimeADT(TimeOnly time) + => Meos.TimeFromParts(WholeMicroseconds(time.Ticks, time, nameof(time))); + + /// The TimeOnly of a count of microseconds from midnight; the + /// clock 24:00:00, which a TimeOnly ends before, is refused. + private static TimeOnly ClockTime(long us, string name) + { + if (us >= MicrosecondsPerDay) + { + throw new ArgumentOutOfRangeException(name, us, + "A TimeOnly cannot state the clock 24:00:00."); + } + + return new TimeOnly(us * TicksPerMicrosecond); + } + + /// A MEOS TimeADT as a TimeOnly, through time_to_parts. + public static TimeOnly ToTimeOnly(long time) + { + IntPtr parts = Marshal.AllocHGlobal(8); + try + { + Meos.TimeToParts(time, parts); + return ClockTime(Marshal.ReadInt64(parts), nameof(time)); + } + finally + { + Marshal.FreeHGlobal(parts); + } + } + + /// A time and its offset from UTC as a MEOS TimeTzADT, through + /// timetz_from_parts, whose offset is in whole seconds east of UTC. + public static IntPtr ToTimetzADT((TimeOnly Time, TimeSpan Offset) timetz) + { + if (timetz.Offset.Ticks % TimeSpan.TicksPerSecond != 0) + { + throw new ArgumentOutOfRangeException(nameof(timetz), timetz.Offset, + "A MEOS time zone offset holds whole seconds."); + } + + return Meos.TimetzFromParts( + WholeMicroseconds(timetz.Time.Ticks, timetz.Time, nameof(timetz)), + checked((int)(timetz.Offset.Ticks / TimeSpan.TicksPerSecond))); + } + + /// A MEOS TimeTzADT as a time and its offset from UTC, through + /// timetz_to_parts. + public static (TimeOnly Time, TimeSpan Offset) ToTimeWithOffset(IntPtr timetz) + { + IntPtr parts = Marshal.AllocHGlobal(16); + try + { + Meos.TimetzToParts(timetz, parts, parts + 8); + return (ClockTime(Marshal.ReadInt64(parts), nameof(timetz)), + TimeSpan.FromSeconds(Marshal.ReadInt32(parts + 8))); + } + finally + { + Marshal.FreeHGlobal(parts); + } + } + /// Each MEOS TimestampTz of an array, as a DateTime. internal static DateTime[] ToDateTimeArray(long[] moments) { diff --git a/tools/objectgen.py b/tools/objectgen.py index 722b11c..2a55919 100644 --- a/tools/objectgen.py +++ b/tools/objectgen.py @@ -1843,9 +1843,12 @@ def convert_file(self) -> str: /// DateOnly count from 0001-01-01, MEOS from 2000-01-01, and both sides /// state the Unix epoch exactly. A DateTime counts ticks of 100 ns, so one /// below a microsecond, which no MEOS timestamp holds, is refused; a value - /// the other side cannot state is refused by its constructor. + /// the other side cannot state is refused by its constructor. An interval, + /// a time and a time with time zone cast through their parts: a TimeSpan, + /// a TimeOnly and the pair of a TimeOnly and its offset, which .NET holds + /// in no single type. [System.CodeDom.Compiler.GeneratedCode("MEOS.NET.ObjectGen", "{GENERATOR_VERSION}")] - internal static class MEOSConvert + public static class MEOSConvert {{ private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000; @@ -1872,14 +1875,14 @@ def convert_file(self) -> str: checked(us * TicksPerMicrosecond)), kind); /// A MEOS TimestampTz as a DateTime of UTC kind. - internal static DateTime ToDateTime(long timestamptz) + public static DateTime ToDateTime(long timestamptz) => FromUnixMicroseconds(Meos.TimestamptzToUnixUs(timestamptz), DateTimeKind.Utc); /// A DateTime as a MEOS TimestampTz: a moment of UTC or local /// kind at its instant, one of unspecified kind read in the zone of the /// MEOS session, as MEOS reads a timestamp without an offset. - internal static long ToTimestampTz(DateTime moment) + public static long ToTimestampTz(DateTime moment) => moment.Kind == DateTimeKind.Unspecified ? Meos.TimestampToTimestamptz( Meos.UnixUsToTimestamp(UnixMicroseconds(moment))) @@ -1887,22 +1890,142 @@ def convert_file(self) -> str: /// A MEOS Timestamp, which has no time zone, as a DateTime of /// unspecified kind. - internal static DateTime ToUnzonedDateTime(long timestamp) + public static DateTime ToUnzonedDateTime(long timestamp) => FromUnixMicroseconds(Meos.TimestampToUnixUs(timestamp), DateTimeKind.Unspecified); /// A DateTime as a MEOS Timestamp: the clock it reads. - internal static long ToTimestamp(DateTime moment) + public static long ToTimestamp(DateTime moment) => Meos.UnixUsToTimestamp(UnixMicroseconds(moment)); /// A MEOS DateADT as a DateOnly. - internal static DateOnly ToDateOnly(int date) + public static DateOnly ToDateOnly(int date) => DateOnly.FromDayNumber(checked(UnixEpochDayNumber + Meos.DateToUnixDays(date))); /// A DateOnly as a MEOS DateADT. - internal static int ToDateADT(DateOnly day) + public static int ToDateADT(DateOnly day) => Meos.UnixDaysToDate(day.DayNumber - UnixEpochDayNumber); + private const long MicrosecondsPerDay = TimeSpan.TicksPerDay / TicksPerMicrosecond; + + /// The whole microseconds of a count of ticks, refusing a tick + /// below a microsecond. + private static long WholeMicroseconds(long ticks, object value, string name) + {{ + if (ticks % TicksPerMicrosecond != 0) + {{ + throw new ArgumentOutOfRangeException(name, value, + "A MEOS value holds whole microseconds."); + }} + + return ticks / TicksPerMicrosecond; + }} + + /// A TimeSpan as a MEOS Interval, through interval_from_parts: + /// its whole days as days and the rest as microseconds. + public static IntPtr ToInterval(TimeSpan duration) + => Meos.IntervalFromParts(0, checked((int)(duration.Ticks / TimeSpan.TicksPerDay)), + WholeMicroseconds(duration.Ticks % TimeSpan.TicksPerDay, duration, + nameof(duration))); + + /// A MEOS Interval as a TimeSpan, through interval_to_parts; an + /// interval with months, which has no fixed length, or beyond the range + /// of a TimeSpan, is refused. + public static TimeSpan ToTimeSpan(IntPtr interval) + {{ + IntPtr parts = Marshal.AllocHGlobal(16); + try + {{ + Meos.IntervalToParts(interval, parts, parts + 4, parts + 8); + int months = Marshal.ReadInt32(parts); + int days = Marshal.ReadInt32(parts + 4); + long us = Marshal.ReadInt64(parts + 8); + if (months != 0) + {{ + throw new ArgumentOutOfRangeException(nameof(interval), months, + "A TimeSpan cannot state an interval with months."); + }} + + long total = days * MicrosecondsPerDay + us; + if (total > TimeSpan.MaxValue.Ticks / TicksPerMicrosecond + || total < TimeSpan.MinValue.Ticks / TicksPerMicrosecond) + {{ + throw new ArgumentOutOfRangeException(nameof(interval), days, + "A TimeSpan cannot state this interval."); + }} + + return TimeSpan.FromTicks(total * TicksPerMicrosecond); + }} + finally + {{ + Marshal.FreeHGlobal(parts); + }} + }} + + /// A TimeOnly as a MEOS TimeADT, through time_from_parts. + public static long ToTimeADT(TimeOnly time) + => Meos.TimeFromParts(WholeMicroseconds(time.Ticks, time, nameof(time))); + + /// The TimeOnly of a count of microseconds from midnight; the + /// clock 24:00:00, which a TimeOnly ends before, is refused. + private static TimeOnly ClockTime(long us, string name) + {{ + if (us >= MicrosecondsPerDay) + {{ + throw new ArgumentOutOfRangeException(name, us, + "A TimeOnly cannot state the clock 24:00:00."); + }} + + return new TimeOnly(us * TicksPerMicrosecond); + }} + + /// A MEOS TimeADT as a TimeOnly, through time_to_parts. + public static TimeOnly ToTimeOnly(long time) + {{ + IntPtr parts = Marshal.AllocHGlobal(8); + try + {{ + Meos.TimeToParts(time, parts); + return ClockTime(Marshal.ReadInt64(parts), nameof(time)); + }} + finally + {{ + Marshal.FreeHGlobal(parts); + }} + }} + + /// A time and its offset from UTC as a MEOS TimeTzADT, through + /// timetz_from_parts, whose offset is in whole seconds east of UTC. + public static IntPtr ToTimetzADT((TimeOnly Time, TimeSpan Offset) timetz) + {{ + if (timetz.Offset.Ticks % TimeSpan.TicksPerSecond != 0) + {{ + throw new ArgumentOutOfRangeException(nameof(timetz), timetz.Offset, + "A MEOS time zone offset holds whole seconds."); + }} + + return Meos.TimetzFromParts( + WholeMicroseconds(timetz.Time.Ticks, timetz.Time, nameof(timetz)), + checked((int)(timetz.Offset.Ticks / TimeSpan.TicksPerSecond))); + }} + + /// A MEOS TimeTzADT as a time and its offset from UTC, through + /// timetz_to_parts. + public static (TimeOnly Time, TimeSpan Offset) ToTimeWithOffset(IntPtr timetz) + {{ + IntPtr parts = Marshal.AllocHGlobal(16); + try + {{ + Meos.TimetzToParts(timetz, parts, parts + 8); + return (ClockTime(Marshal.ReadInt64(parts), nameof(timetz)), + TimeSpan.FromSeconds(Marshal.ReadInt32(parts + 8))); + }} + finally + {{ + Marshal.FreeHGlobal(parts); + }} + }} + /// Each MEOS TimestampTz of an array, as a DateTime. internal static DateTime[] ToDateTimeArray(long[] moments) {{