diff --git a/src/System.Time/System/DateTimeExtensions.cs b/src/System.Time/System/DateTimeExtensions.cs index eb5b7685e..2892cd549 100644 --- a/src/System.Time/System/DateTimeExtensions.cs +++ b/src/System.Time/System/DateTimeExtensions.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Globalization; - namespace System { /// @@ -31,195 +29,5 @@ namespace System { return new Time(dateTime.TimeOfDay.Ticks); } - - /// - /// Gets a object that is set to the current date and time in the specified time zone. - /// - /// The instance. - /// The current for the specified time zone. - public static DateTime NowInTimeZone(TimeZoneInfo timeZoneInfo) - { - // TODO: Propose placing this method directly in the System.DateTime struct - - DateTime utcNow = DateTime.UtcNow; - return TimeZoneInfo.ConvertTime(utcNow, timeZoneInfo); - } - - public static DateTimeOffset AddYears(this DateTime dateTime, int years, TimeZoneInfo timeZone) - { - return AddByDate(dateTime, (dt, val) => dt.AddYears(val), years, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddYears(this DateTime dateTime, int years, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTime, (dt, val) => dt.AddYears(val), years, timeZone, resolver); - } - - public static DateTimeOffset AddMonths(this DateTime dateTime, int months, TimeZoneInfo timeZone) - { - return AddByDate(dateTime, (dt, val) => dt.AddMonths(val), months, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddMonths(this DateTime dateTime, int months, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTime, (dt, val) => dt.AddMonths(val), months, timeZone, resolver); - } - - public static DateTimeOffset AddDays(this DateTime dateTime, int days, TimeZoneInfo timeZone) - { - return AddByDate(dateTime, (dt, val) => dt.AddDays(val), days, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddDays(this DateTime dateTime, int days, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTime, (dt, val) => dt.AddDays(val), days, timeZone, resolver); - } - - public static DateTimeOffset AddHours(this DateTime dateTime, double hours, TimeZoneInfo timeZone) - { - return dateTime.Add(TimeSpan.FromHours(hours), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddHours(this DateTime dateTime, double hours, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(TimeSpan.FromHours(hours), timeZone, resolver); - } - - public static DateTimeOffset AddMinutes(this DateTime dateTime, double minutes, TimeZoneInfo timeZone) - { - return dateTime.Add(TimeSpan.FromMinutes(minutes), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddMinutes(this DateTime dateTime, double minutes, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(TimeSpan.FromMinutes(minutes), timeZone, resolver); - } - - public static DateTimeOffset AddSeconds(this DateTime dateTime, double seconds, TimeZoneInfo timeZone) - { - return dateTime.Add(TimeSpan.FromSeconds(seconds), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddSeconds(this DateTime dateTime, double seconds, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(TimeSpan.FromSeconds(seconds), timeZone, resolver); - } - - public static DateTimeOffset AddMilliseconds(this DateTime dateTime, double milliseconds, TimeZoneInfo timeZone) - { - return dateTime.Add(TimeSpan.FromMilliseconds(milliseconds), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddMilliseconds(this DateTime dateTime, double milliseconds, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(TimeSpan.FromMilliseconds(milliseconds), timeZone, resolver); - } - - public static DateTimeOffset AddTicks(this DateTime dateTime, long ticks, TimeZoneInfo timeZone) - { - return dateTime.Add(TimeSpan.FromTicks(ticks), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddTicks(this DateTime dateTime, long ticks, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(TimeSpan.FromTicks(ticks), timeZone, resolver); - } - - public static DateTimeOffset Add(this DateTime dateTime, TimeSpan timeSpan, TimeZoneInfo timeZone) - { - return dateTime.Add(timeSpan, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset Subtract(this DateTime dateTime, TimeSpan timeSpan, TimeZoneInfo timeZone) - { - return dateTime.Add(timeSpan.Negate(), timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset Subtract(this DateTime dateTime, TimeSpan timeSpan, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return dateTime.Add(timeSpan.Negate(), timeZone, resolver); - } - - public static DateTimeOffset Add(this DateTime dateTime, TimeSpan timeSpan, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - DateTimeOffset dto = resolver(dateTime, timeZone); - var result = dto.Add(timeSpan); - return TimeZoneInfo.ConvertTime(result, timeZone); - } - - private static DateTimeOffset AddByDate(DateTime dateTime, Func operation, int value, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - if (dateTime.Kind != DateTimeKind.Unspecified) - { - dateTime = TimeZoneInfo.ConvertTime(dateTime, timeZone); - } - - var result = operation(dateTime, value); - return resolver(result, timeZone); - } - - /// - /// Converts the value of the current object to its equivalent long date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the long date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the invariant culture. - /// - public static string ToLongDateStringInvariant(this DateTime dateTime) - { - return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent short date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the short date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the invariant culture. - /// - public static string ToShortDateStringInvariant(this DateTime dateTime) - { - return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent - /// long time string representation. - /// - /// The instance. - /// A string that contains the long time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the invariant culture. - public static string ToLongTimeStringInvariant(this DateTime dateTime) - { - return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent - /// short time string representation. - /// - /// The instance. - /// A string that contains the short time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the invariant culture. - public static string ToShortTimeStringInvariant(this DateTime dateTime) - { - return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern, CultureInfo.InvariantCulture); - } } } diff --git a/src/System.Time/System/DateTimeOffsetExtensions.cs b/src/System.Time/System/DateTimeOffsetExtensions.cs index 82a5675bd..cb8dfd21a 100644 --- a/src/System.Time/System/DateTimeOffsetExtensions.cs +++ b/src/System.Time/System/DateTimeOffsetExtensions.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Globalization; - namespace System { /// @@ -31,218 +29,5 @@ namespace System { return new Time(dateTimeOffset.TimeOfDay.Ticks); } - - /// - /// Gets a object that is set to the current date, time, - /// and offset from Coordinated Universal Time (UTC) in the specified time zone. - /// - /// The instance. - /// The current for the specified time zone. - public static DateTimeOffset NowInTimeZone(TimeZoneInfo timeZoneInfo) - { - // TODO: Propose placing this method directly in the System.DateTimeOffset struct - - DateTimeOffset utcNow = DateTimeOffset.UtcNow; - return TimeZoneInfo.ConvertTime(utcNow, timeZoneInfo); - } - - public static DateTimeOffset AddYears(this DateTimeOffset dateTimeOffset, int years, TimeZoneInfo timeZone) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddYears(val), years, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddYears(this DateTimeOffset dateTimeOffset, int years, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddYears(val), years, timeZone, resolver); - } - - public static DateTimeOffset AddMonths(this DateTimeOffset dateTimeOffset, int months, TimeZoneInfo timeZone) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddMonths(val), months, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddMonths(this DateTimeOffset dateTimeOffset, int months, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddMonths(val), months, timeZone, resolver); - } - - public static DateTimeOffset AddDays(this DateTimeOffset dateTimeOffset, int days, TimeZoneInfo timeZone) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddDays(val), days, timeZone, TimeZoneOffsetResolvers.Default); - } - - public static DateTimeOffset AddDays(this DateTimeOffset dateTimeOffset, int days, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - return AddByDate(dateTimeOffset, (dt, val) => dt.AddDays(val), days, timeZone, resolver); - } - - public static DateTimeOffset AddHours(this DateTimeOffset dateTimeOffset, double hours, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(TimeSpan.FromHours(hours), timeZone); - } - - public static DateTimeOffset AddMinutes(this DateTimeOffset dateTimeOffset, double minutes, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(TimeSpan.FromMinutes(minutes), timeZone); - } - - public static DateTimeOffset AddSeconds(this DateTimeOffset dateTimeOffset, double seconds, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(TimeSpan.FromSeconds(seconds), timeZone); - } - - public static DateTimeOffset AddMilliseconds(this DateTimeOffset dateTimeOffset, double milliseconds, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(TimeSpan.FromMilliseconds(milliseconds), timeZone); - } - - public static DateTimeOffset AddTicks(this DateTimeOffset dateTimeOffset, long ticks, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(TimeSpan.FromTicks(ticks), timeZone); - } - - public static DateTimeOffset Subtract(this DateTimeOffset dateTimeOffset, TimeSpan timeSpan, TimeZoneInfo timeZone) - { - return dateTimeOffset.Add(timeSpan.Negate(), timeZone); - } - - public static DateTimeOffset Add(this DateTimeOffset dateTimeOffset, TimeSpan timeSpan, TimeZoneInfo timeZone) - { - var t = dateTimeOffset.Add(timeSpan); - return TimeZoneInfo.ConvertTime(t, timeZone); - } - - private static DateTimeOffset AddByDate(DateTimeOffset dateTimeOffset, Func operation, int value, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver) - { - dateTimeOffset = TimeZoneInfo.ConvertTime(dateTimeOffset, timeZone); - var dt = operation(dateTimeOffset.DateTime, value); - return resolver.Invoke(dt, timeZone); - } - - /// - /// Converts the value of the current object to its equivalent long date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the long date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the current thread culture. - /// - public static string ToLongDateString(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern); - } - - /// - /// Converts the value of the current object to its equivalent long date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the long date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the invariant culture. - /// - public static string ToLongDateStringInvariant(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent short date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the short date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the current thread culture. - /// - public static string ToShortDateString(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern); - } - - /// - /// Converts the value of the current object to its equivalent short date string - /// representation. - /// - /// The instance. - /// - /// A string that contains the short date string representation of the current object. - /// - /// - /// The value of the current object is formatted using the pattern defined by the - /// property associated with the invariant culture. - /// - public static string ToShortDateStringInvariant(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent - /// long time string representation. - /// - /// A string that contains the long time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the current thread culture. - public static string ToLongTimeString(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern); - } - - /// - /// Converts the value of the current object to its equivalent - /// long time string representation. - /// - /// The instance. - /// A string that contains the long time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the invariant culture. - public static string ToLongTimeStringInvariant(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture); - } - - /// - /// Converts the value of the current object to its equivalent - /// short time string representation. - /// - /// A string that contains the short time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the current thread culture. - public static string ToShortTimeString(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); - } - - /// - /// Converts the value of the current object to its equivalent - /// short time string representation. - /// - /// The instance. - /// A string that contains the short time string representation of the - /// current object. - /// The value of the current object is formatted - /// using the pattern defined by the - /// property associated with the invariant culture. - public static string ToShortTimeStringInvariant(this DateTimeOffset dateTimeOffset) - { - return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern, CultureInfo.InvariantCulture); - } } } diff --git a/src/System.Time/System/TimeZoneInfoExtensions.cs b/src/System.Time/System/TimeZoneInfoExtensions.cs deleted file mode 100644 index e4e1165d1..000000000 --- a/src/System.Time/System/TimeZoneInfoExtensions.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace System -{ - /// - /// Extension methods for . - /// - public static class TimeZoneInfoExtensions - { - /// - /// Gets a object that is set to the current date, time, - /// and offset from Coordinated Universal Time (UTC) in this time zone. - /// - /// The instance. - /// The current for the instance time zone. - public static DateTimeOffset GetCurrentDateTimeOffset(this TimeZoneInfo timeZoneInfo) - { - return DateTimeOffsetExtensions.NowInTimeZone(timeZoneInfo); - } - - /// - /// Gets a object that is set to the current date and time in this time zone. - /// - /// The instance. - /// The current for the instance time zone. - public static DateTime GetCurrentDateTime(this TimeZoneInfo timeZoneInfo) - { - return DateTimeExtensions.NowInTimeZone(timeZoneInfo); - } - - /// - /// Gets a object that is set to the current date in this time zone. - /// - /// The instance. - /// The current for the instance time zone. - public static Date GetCurrentDate(this TimeZoneInfo timeZoneInfo) - { - return Date.TodayInTimeZone(timeZoneInfo); - } - - /// - /// Gets a object that is set to the current time in this time zone. - /// - /// The instance. - /// The current for the instance time zone. - public static Time GetCurrentTime(this TimeZoneInfo timeZoneInfo) - { - return Time.NowInTimeZone(timeZoneInfo); - } - } -} diff --git a/src/System.Time/System/TimeZoneOffsetResolvers.cs b/src/System.Time/System/TimeZoneOffsetResolvers.cs deleted file mode 100644 index 31a20fc46..000000000 --- a/src/System.Time/System/TimeZoneOffsetResolvers.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace System -{ - public delegate DateTimeOffset TimeZoneOffsetResolver(DateTime dateTime, TimeZoneInfo timeZone); - - public static class TimeZoneOffsetResolvers - { - public static DateTimeOffset Default(DateTime dt, TimeZoneInfo timeZone) - { - if (dt.Kind != DateTimeKind.Unspecified) - { - var dto = new DateTimeOffset(dt); - return TimeZoneInfo.ConvertTime(dto, timeZone); - } - - if (timeZone.IsAmbiguousTime(dt)) - { - var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1)); - return new DateTimeOffset(dt, earlierOffset); - } - - if (timeZone.IsInvalidTime(dt)) - { - var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1)); - var laterOffset = timeZone.GetUtcOffset(dt.AddDays(1)); - var transitionGap = laterOffset - earlierOffset; - return new DateTimeOffset(dt.Add(transitionGap), laterOffset); - } - - return new DateTimeOffset(dt, timeZone.GetUtcOffset(dt)); - } - - // TODO: include other kinds of resolvers - } -} diff --git a/tests/System.Time.Tests/DateTimeOffsetTests.cs b/tests/System.Time.Tests/DateTimeOffsetTests.cs index 1907eb4d9..d5ba6736f 100644 --- a/tests/System.Time.Tests/DateTimeOffsetTests.cs +++ b/tests/System.Time.Tests/DateTimeOffsetTests.cs @@ -1,149 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Xunit; - namespace System._Time.Tests { public class DateTimeOffsetTests { -#if Windows - string TimeZoneId = "Pacific Standard Time"; -#else - string TimeZoneId = "America/Los_Angeles"; -#endif - - [Fact] - public void CanAddYearsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2014, 3, 9, 0, 0, 0, TimeSpan.FromHours(-8)); - var result = dto.AddYears(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMonthsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 2, 9, 0, 0, 0, TimeSpan.FromHours(-8)); - var result = dto.AddMonths(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 0, 0, 0, TimeSpan.FromHours(-8)); - var result = dto.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_LandInGap() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 7, 2, 30, 0, TimeSpan.FromHours(-8)); - var result = dto.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 30, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_LandInOverlap() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 10, 31, 1, 30, 0, TimeSpan.FromHours(-7)); - var result = dto.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 11, 1, 1, 30, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_StartWithMismatchedOffset() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 4, 0, 0, TimeSpan.FromHours(-4)); - var result = dto.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddHoursAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 1, 0, 0, TimeSpan.FromHours(-8)); - var result = dto.AddHours(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMinutesAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 0, TimeSpan.FromHours(-8)); - var result = dto.AddMinutes(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddSecondsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, TimeSpan.FromHours(-8)); - var result = dto.AddSeconds(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMillisecondsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, 999, TimeSpan.FromHours(-8)); - var result = dto.AddMilliseconds(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddTicksAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, 999, TimeSpan.FromHours(-8)).AddTicks(9999); - var result = dto.AddTicks(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } } } diff --git a/tests/System.Time.Tests/DateTimeTests.cs b/tests/System.Time.Tests/DateTimeTests.cs index 10b381c35..c5e0f9a78 100644 --- a/tests/System.Time.Tests/DateTimeTests.cs +++ b/tests/System.Time.Tests/DateTimeTests.cs @@ -1,162 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Xunit; - namespace System._Time.Tests { public class DateTimeTests { -#if Windows - string TimeZoneId = "Pacific Standard Time"; -#else - string TimeZoneId = "America/Los_Angeles"; -#endif - - [Fact] - public void CanAddYearsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2014, 3, 9, 0, 0, 0); - var result = dt.AddYears(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMonthsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 2, 9, 0, 0, 0); - var result = dt.AddMonths(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 0, 0, 0); - var result = dt.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_LandInGap() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 7, 2, 30, 0); - var result = dt.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 30, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_LandInOverlap() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 10, 31, 1, 30, 0); - var result = dt.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 11, 1, 1, 30, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddDaysAcrossDstTransition_StartWithMismatchedKind() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 8, 0, 0, DateTimeKind.Utc); - var result = dt.AddDays(1, tz); - - var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - - [Fact] - public void CanAddHoursAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 1, 0, 0); - var result = dt.AddHours(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddHoursAcrossDstTransition_StartWithMismatchedKind() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 9, 0, 0, DateTimeKind.Utc); - var result = dt.AddHours(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMinutesAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 1, 59, 0); - var result = dt.AddMinutes(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddSecondsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 1, 59, 59); - var result = dt.AddSeconds(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddMillisecondsAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 1, 59, 59, 999); - var result = dt.AddMilliseconds(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } - - [Fact] - public void CanAddTicksAcrossDstTransition() - { - var tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId); - var dt = new DateTime(2015, 3, 8, 1, 59, 59, 999).AddTicks(9999); - var result = dt.AddTicks(1, tz); - - var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7)); - Assert.Equal(expected, result); - Assert.Equal(expected.Offset, result.Offset); - } } }