Skip to content

API Proposal: Add OffsetMinutes to DateTimeOffset #52081

@JeremyKuhne

Description

@JeremyKuhne

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.

cc: @KrzysztofCwalina, @tarekgh, @lonitra

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions