Implement DateTimeOffset.TotalOffsetMinutes (#78943)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Tue, 29 Nov 2022 23:48:02 +0000 (15:48 -0800)
committerGitHub <noreply@github.com>
Tue, 29 Nov 2022 23:48:02 +0000 (15:48 -0800)
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Fixes https://github.com/dotnet/runtime/issues/52081

src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs
src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/tests/System/DateTimeOffsetTests.cs

index 7c05f4e..4177643 100644 (file)
@@ -139,7 +139,7 @@ namespace System
         }
 
         // Constructs a DateTimeOffset from a given year, month, day, hour,
-        // minute, second, millsecond and offset
+        // minute, second, millisecond and offset
         public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset)
         {
             _offsetMinutes = ValidateOffset(offset);
@@ -161,7 +161,7 @@ namespace System
         }
 
         // Constructs a DateTimeOffset from a given year, month, day, hour,
-        // minute, second, millsecond, Calendar and offset.
+        // minute, second, millisecond, Calendar and offset.
         public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, TimeSpan offset)
         {
             _offsetMinutes = ValidateOffset(offset);
@@ -417,6 +417,11 @@ namespace System
 
         public TimeSpan Offset => new TimeSpan(0, _offsetMinutes, 0);
 
+        /// <summary>
+        /// Gets the total number of minutes representing the time's offset from Coordinated Universal Time (UTC).
+        /// </summary>
+        public int TotalOffsetMinutes => _offsetMinutes;
+
         // Returns the second part of this DateTimeOffset. The returned value is
         // an integer between 0 and 59.
         //
index 80966d4..7d3a633 100644 (file)
@@ -1745,6 +1745,7 @@ namespace System
         public int Second { get { throw null; } }
         public long Ticks { get { throw null; } }
         public System.TimeSpan TimeOfDay { get { throw null; } }
+        public int TotalOffsetMinutes { get { throw null; } }
         public System.DateTime UtcDateTime { get { throw null; } }
         public static System.DateTimeOffset UtcNow { get { throw null; } }
         public long UtcTicks { get { throw null; } }
index 6c4ae73..4aa4e79 100644 (file)
@@ -1381,6 +1381,29 @@ namespace System.Tests
             Assert.Equal(expectedString, actual.ToString("u"));
         }
 
+        [Theory]
+        [InlineData(5)]
+        [InlineData(-5)]
+        [InlineData(0)]
+        [InlineData(14 * 60)]  // max offset
+        [InlineData(-14 * 60)] // min offset
+        public static void TotalNumberOfMinutesTest(int minutesCount)
+        {
+            DateTimeOffset dto = new DateTimeOffset(new DateTime(2022, 11, 12), TimeSpan.FromMinutes(minutesCount));
+            Assert.Equal(minutesCount, dto.TotalOffsetMinutes);
+            Assert.Equal(minutesCount, dto.Offset.TotalMinutes);
+        }
+
+        [Fact]
+        public static void TotalNumberOfMinutesNowTest()
+        {
+            DateTimeOffset dto = DateTimeOffset.UtcNow;
+            Assert.Equal(0, dto.TotalOffsetMinutes);
+
+            dto = DateTimeOffset.Now;
+            Assert.Equal(dto.Offset.TotalMinutes, dto.TotalOffsetMinutes);
+        }
+
         [Fact]
         public static void TryFormat_ToString_EqualResults()
         {