Remove extensions methods not related to Date and Time types

This commit is contained in:
Matt Johnson 2017-11-13 09:38:58 -08:00
Родитель 97ae47716e
Коммит dc66a047d9
6 изменённых файлов: 0 добавлений и 787 удалений

Просмотреть файл

@ -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
{
/// <summary>
@ -31,195 +29,5 @@ namespace System
{
return new Time(dateTime.TimeOfDay.Ticks);
}
/// <summary>
/// Gets a <see cref="DateTime"/> object that is set to the current date and time in the specified time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="DateTime"/> for the specified time zone.</returns>
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<DateTime, int, DateTime> 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);
}
/// <summary>
/// Converts the value of the current <see cref="DateTime"/> object to its equivalent long date string
/// representation.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> instance.</param>
/// <returns>
/// A string that contains the long date string representation of the current <see cref="DateTime"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTime"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.LongDatePattern" /> property associated with the invariant culture.
/// </remarks>
public static string ToLongDateStringInvariant(this DateTime dateTime)
{
return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTime"/> object to its equivalent short date string
/// representation.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> instance.</param>
/// <returns>
/// A string that contains the short date string representation of the current <see cref="DateTime"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTime"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.ShortDatePattern" /> property associated with the invariant culture.
/// </remarks>
public static string ToShortDateStringInvariant(this DateTime dateTime)
{
return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTime"/> object to its equivalent
/// long time string representation.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> instance.</param>
/// <returns>A string that contains the long time string representation of the
/// current <see cref="DateTime"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTime"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.LongTimePattern" />
/// property associated with the invariant culture.</remarks>
public static string ToLongTimeStringInvariant(this DateTime dateTime)
{
return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTime"/> object to its equivalent
/// short time string representation.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> instance.</param>
/// <returns>A string that contains the short time string representation of the
/// current <see cref="DateTime"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTime"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.ShortTimePattern" />
/// property associated with the invariant culture.</remarks>
public static string ToShortTimeStringInvariant(this DateTime dateTime)
{
return dateTime.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern, CultureInfo.InvariantCulture);
}
}
}

Просмотреть файл

@ -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
{
/// <summary>
@ -31,218 +29,5 @@ namespace System
{
return new Time(dateTimeOffset.TimeOfDay.Ticks);
}
/// <summary>
/// Gets a <see cref="DateTimeOffset"/> object that is set to the current date, time,
/// and offset from Coordinated Universal Time (UTC) in the specified time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="DateTimeOffset"/> for the specified time zone.</returns>
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<DateTime, int, DateTime> operation, int value, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver)
{
dateTimeOffset = TimeZoneInfo.ConvertTime(dateTimeOffset, timeZone);
var dt = operation(dateTimeOffset.DateTime, value);
return resolver.Invoke(dt, timeZone);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent long date string
/// representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>
/// A string that contains the long date string representation of the current <see cref="DateTimeOffset"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTimeOffset"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.LongDatePattern" /> property associated with the current thread culture.
/// </remarks>
public static string ToLongDateString(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent long date string
/// representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>
/// A string that contains the long date string representation of the current <see cref="DateTimeOffset"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTimeOffset"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.LongDatePattern" /> property associated with the invariant culture.
/// </remarks>
public static string ToLongDateStringInvariant(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent short date string
/// representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>
/// A string that contains the short date string representation of the current <see cref="DateTimeOffset"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTimeOffset"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.ShortDatePattern" /> property associated with the current thread culture.
/// </remarks>
public static string ToShortDateString(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent short date string
/// representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>
/// A string that contains the short date string representation of the current <see cref="DateTimeOffset"/> object.
/// </returns>
/// <remarks>
/// The value of the current <see cref="DateTimeOffset"/> object is formatted using the pattern defined by the
/// <see cref="DateTimeFormatInfo.ShortDatePattern" /> property associated with the invariant culture.
/// </remarks>
public static string ToShortDateStringInvariant(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent
/// long time string representation.
/// </summary>
/// <returns>A string that contains the long time string representation of the
/// current <see cref="DateTimeOffset"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTimeOffset"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.LongTimePattern" />
/// property associated with the current thread culture.</remarks>
public static string ToLongTimeString(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent
/// long time string representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>A string that contains the long time string representation of the
/// current <see cref="DateTimeOffset"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTimeOffset"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.LongTimePattern" />
/// property associated with the invariant culture.</remarks>
public static string ToLongTimeStringInvariant(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent
/// short time string representation.
/// </summary>
/// <returns>A string that contains the short time string representation of the
/// current <see cref="DateTimeOffset"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTimeOffset"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.ShortTimePattern" />
/// property associated with the current thread culture.</remarks>
public static string ToShortTimeString(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
}
/// <summary>
/// Converts the value of the current <see cref="DateTimeOffset"/> object to its equivalent
/// short time string representation.
/// </summary>
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> instance.</param>
/// <returns>A string that contains the short time string representation of the
/// current <see cref="DateTimeOffset"/> object.</returns>
/// <remarks>The value of the current <see cref="DateTimeOffset"/> object is formatted
/// using the pattern defined by the <see cref="DateTimeFormatInfo.ShortTimePattern" />
/// property associated with the invariant culture.</remarks>
public static string ToShortTimeStringInvariant(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern, CultureInfo.InvariantCulture);
}
}
}

Просмотреть файл

@ -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
{
/// <summary>
/// Extension methods for <see cref="TimeZoneInfo"/>.
/// </summary>
public static class TimeZoneInfoExtensions
{
/// <summary>
/// Gets a <see cref="DateTimeOffset"/> object that is set to the current date, time,
/// and offset from Coordinated Universal Time (UTC) in this time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="DateTimeOffset"/> for the instance time zone.</returns>
public static DateTimeOffset GetCurrentDateTimeOffset(this TimeZoneInfo timeZoneInfo)
{
return DateTimeOffsetExtensions.NowInTimeZone(timeZoneInfo);
}
/// <summary>
/// Gets a <see cref="DateTime"/> object that is set to the current date and time in this time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="DateTime"/> for the instance time zone.</returns>
public static DateTime GetCurrentDateTime(this TimeZoneInfo timeZoneInfo)
{
return DateTimeExtensions.NowInTimeZone(timeZoneInfo);
}
/// <summary>
/// Gets a <see cref="Date"/> object that is set to the current date in this time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="Date"/> for the instance time zone.</returns>
public static Date GetCurrentDate(this TimeZoneInfo timeZoneInfo)
{
return Date.TodayInTimeZone(timeZoneInfo);
}
/// <summary>
/// Gets a <see cref="Time"/> object that is set to the current time in this time zone.
/// </summary>
/// <param name="timeZoneInfo">The <see cref="TimeZoneInfo"/> instance.</param>
/// <returns>The current <see cref="Time"/> for the instance time zone.</returns>
public static Time GetCurrentTime(this TimeZoneInfo timeZoneInfo)
{
return Time.NowInTimeZone(timeZoneInfo);
}
}
}

Просмотреть файл

@ -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
}
}

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -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);
}
}
}