DD.WellWorkover.Cloud/AsbCloudInfrastructure/DateTimeExtentions.cs

42 lines
1.5 KiB
C#
Raw Normal View History

using System;
namespace AsbCloudInfrastructure
{
public static class DateTimeExtentions
{
2023-01-11 16:00:11 +05:00
/// <summary>
/// Приветсти к UTC из времени куста
/// </summary>
/// <param name="date"></param>
/// <param name="remoteTimezoneOffsetHours"></param>
/// <returns></returns>
public static DateTimeOffset ToUtcDateTimeOffset(this DateTime date, double remoteTimezoneOffsetHours)
{
if (date == default)
return new DateTimeOffset();
var dateUtc = date.Kind switch
{
DateTimeKind.Local => date.ToUniversalTime(),
DateTimeKind.Unspecified => DateTime.SpecifyKind(date.AddHours(-remoteTimezoneOffsetHours), DateTimeKind.Utc),
_ => date,
};
2022-04-11 18:00:34 +05:00
return new DateTimeOffset(dateUtc);
}
2023-01-11 16:00:11 +05:00
/// <summary>
/// Привести ко времени куста из utc
/// </summary>
/// <param name="date"></param>
/// <param name="remoteTimezoneOffsetHours"></param>
/// <returns></returns>
public static DateTime ToRemoteDateTime(this DateTimeOffset date, double remoteTimezoneOffsetHours)
{
if (date == default)
return new DateTime(0, DateTimeKind.Unspecified);
var dateTz = date.ToOffset(TimeSpan.FromHours(remoteTimezoneOffsetHours));
return dateTz.DateTime;
}
}
}