Skip to content

Commit

Permalink
Add comparison operators and FromDotnetTicks to DateTimeNanos (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon4BZ authored Aug 5, 2024
1 parent 4a494d1 commit a1199d7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
74 changes: 61 additions & 13 deletions csharp/DateTimeNanos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@ namespace ParquetSharp
[StructLayout(LayoutKind.Sequential)]
public readonly struct DateTimeNanos : IEquatable<DateTimeNanos>, IComparable, IComparable<DateTimeNanos>
{
/// <summary>
/// Minimum DateTime representable: 1677-09-21 00:12:43.
/// </summary>
public static readonly DateTime MinDateTimeValue = new DateTimeNanos(long.MinValue).DateTime;

/// <summary>
/// Maximum DateTime representable: 2262-04-11 23:47:16.
/// </summary>
public static readonly DateTime MaxDateTimeValue = new DateTimeNanos(long.MaxValue).DateTime;

public DateTimeNanos(long ticks)
{
Ticks = ticks;
}

public DateTimeNanos(DateTime dateTime)
{
Ticks = (dateTime.Ticks - DateTimeOffset) * (1_000_000L / TimeSpan.TicksPerMillisecond);
Ticks = DotnetTicksToNanosSinceEpoch(dateTime.Ticks);
}

/// <summary>
/// Make a new <see cref="DateTimeNanos"/> object from a specified dotnet ticks value
/// </summary>
public static DateTimeNanos FromDotnetTicks(long dotnetTicks)
{
return new DateTimeNanos(DotnetTicksToNanosSinceEpoch(dotnetTicks));
}

/// <summary>
Expand All @@ -28,7 +46,7 @@ public DateTimeNanos(DateTime dateTime)
/// <summary>
/// Convert to System.DateTime with reduced precision.
/// </summary>
public DateTime DateTime => new DateTime(DateTimeOffset + Ticks / (1_000_000L / TimeSpan.TicksPerMillisecond));
public DateTime DateTime => new(NanosSinceEpochToDotnetTicks(Ticks));

public bool Equals(DateTimeNanos other)
{
Expand Down Expand Up @@ -60,6 +78,36 @@ public int CompareTo(DateTimeNanos other)
return Ticks.CompareTo(other.Ticks);
}

public static bool operator ==(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks == right.Ticks;
}

public static bool operator !=(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks != right.Ticks;
}

public static bool operator <(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks < right.Ticks;
}

public static bool operator <=(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks <= right.Ticks;
}

public static bool operator >=(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks >= right.Ticks;
}

public static bool operator >(DateTimeNanos left, DateTimeNanos right)
{
return left.Ticks > right.Ticks;
}

/// <summary>
/// Converts this DateTimeNanos object to a string using a default formatting string with nanosecond precision
/// and the current culture's formatting conventions.
Expand Down Expand Up @@ -89,18 +137,18 @@ public string ToString(string? format, IFormatProvider? formatProvider = null)
return DateTime.ToString(adjustedFormat, formatProvider);
}

/// <summary>
/// Minimum DateTime representable: 1677-09-21 00:12:43.
/// </summary>
public static readonly DateTime MinDateTimeValue = new DateTimeNanos(long.MinValue).DateTime;

/// <summary>
/// Maximum DateTime representable: 2262-04-11 23:47:16.
/// </summary>
public static readonly DateTime MaxDateTimeValue = new DateTimeNanos(long.MaxValue).DateTime;
private const long DateTimeOffsetTicks = 621355968000000000; // new DateTime(1970, 01, 01).Ticks
private const string DefaultFormat = "yyyy-MM-dd HH:mm:ss.fffffffff";
private const long NanosPerTick = 1_000_000L / TimeSpan.TicksPerMillisecond;

private const long DateTimeOffset = 621355968000000000; // new DateTime(1970, 01, 01).Ticks
private static long DotnetTicksToNanosSinceEpoch(long dotnetTicks)
{
return (dotnetTicks - DateTimeOffsetTicks) * NanosPerTick;
}

private const string DefaultFormat = "yyyy-MM-dd HH:mm:ss.fffffffff";
private static long NanosSinceEpochToDotnetTicks(long nanosSinceEpoch)
{
return DateTimeOffsetTicks + nanosSinceEpoch / NanosPerTick;
}
}
}
7 changes: 7 additions & 0 deletions csharp/PublicAPI/net471/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,13 @@ static ParquetSharp.Arrow.ArrowReaderProperties.GetDefault() -> ParquetSharp.Arr
static ParquetSharp.Arrow.ArrowWriterProperties.GetDefault() -> ParquetSharp.Arrow.ArrowWriterProperties!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, ParquetSharp.LogicalTypeFactory! logicalTypeFactory, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.DateTimeNanos.FromDotnetTicks(long dotnetTicks) -> ParquetSharp.DateTimeNanos
static ParquetSharp.DateTimeNanos.operator !=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator ==(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DefaultWriterProperties.Compression.get -> ParquetSharp.Compression?
static ParquetSharp.DefaultWriterProperties.Compression.set -> void
static ParquetSharp.DefaultWriterProperties.CompressionLevel.get -> int?
Expand Down
7 changes: 7 additions & 0 deletions csharp/PublicAPI/net6/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,13 @@ static ParquetSharp.Arrow.ArrowReaderProperties.GetDefault() -> ParquetSharp.Arr
static ParquetSharp.Arrow.ArrowWriterProperties.GetDefault() -> ParquetSharp.Arrow.ArrowWriterProperties!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, ParquetSharp.LogicalTypeFactory! logicalTypeFactory, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.DateTimeNanos.FromDotnetTicks(long dotnetTicks) -> ParquetSharp.DateTimeNanos
static ParquetSharp.DateTimeNanos.operator !=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator ==(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DefaultWriterProperties.Compression.get -> ParquetSharp.Compression?
static ParquetSharp.DefaultWriterProperties.Compression.set -> void
static ParquetSharp.DefaultWriterProperties.CompressionLevel.get -> int?
Expand Down
7 changes: 7 additions & 0 deletions csharp/PublicAPI/netstandard2.1/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,13 @@ static ParquetSharp.Arrow.ArrowReaderProperties.GetDefault() -> ParquetSharp.Arr
static ParquetSharp.Arrow.ArrowWriterProperties.GetDefault() -> ParquetSharp.Arrow.ArrowWriterProperties!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, ParquetSharp.LogicalTypeFactory! logicalTypeFactory, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.Column.CreateSchemaNode(ParquetSharp.Column![]! columns, string! nodeName = "schema") -> ParquetSharp.Schema.GroupNode!
static ParquetSharp.DateTimeNanos.FromDotnetTicks(long dotnetTicks) -> ParquetSharp.DateTimeNanos
static ParquetSharp.DateTimeNanos.operator !=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator <=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator ==(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DateTimeNanos.operator >=(ParquetSharp.DateTimeNanos left, ParquetSharp.DateTimeNanos right) -> bool
static ParquetSharp.DefaultWriterProperties.Compression.get -> ParquetSharp.Compression?
static ParquetSharp.DefaultWriterProperties.Compression.set -> void
static ParquetSharp.DefaultWriterProperties.CompressionLevel.get -> int?
Expand Down

0 comments on commit a1199d7

Please sign in to comment.