-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.DateTime
Milestone
Description
Background and Motivation
In advanced DateTimeOffset scenarios, getting fast access to whether or not the DateTimeOffset has an offset and what it is in minutes is very useful.
Proposed API
namespace System
{
public readonly struct DateTimeOffset
{
// Existing field
private readonly short _offsetMinutes;
// Not adding per discussion below
+ // public bool IsUtc => _offsetMinutes == 0;
+ public int TotalOffsetMinutes => _offsetMinutes;
}
}Usage Examples
Before
public Value(DateTimeOffset value)
{
this = default;
TimeSpan offset = value.Offset;
if (offset.Ticks == 0)
{
// This is a UTC time
_union.Ticks = value.Ticks;
_object = TypeFlags.DateTimeOffset;
}
else if (PackedDateTimeOffset.TryCreate(value, offset, out var packed))
{
_union.PackedDateTimeOffset = packed;
_object = TypeFlags.PackedDateTimeOffset;
}
else
{
_object = value;
}
}After
public Value(DateTimeOffset value)
{
this = default;
int offset = value.TotalOffsetMinutes;
if (offset == 0)
{
// This is a UTC time
_union.Ticks = value.Ticks;
_object = TypeFlags.DateTimeOffset;
}
else if (PackedDateTimeOffset.TryCreate(value, offset, out var packed))
{
_union.PackedDateTimeOffset = packed;
_object = TypeFlags.PackedDateTimeOffset;
}
else
{
_object = value;
}
}Related to this, DateTimeOffset.Ticks could be calling an internal constructor to avoid the checks in DateTime. That isn't public surface area, but worth mentioning.
panost
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.DateTime