2021-12-30 17:05:44 +05:00
|
|
|
|
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>
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public static DateTimeOffset ToUtcDateTimeOffset(this DateTime date, double remoteTimezoneOffsetHours)
|
|
|
|
|
{
|
|
|
|
|
if (date == default)
|
|
|
|
|
return new DateTimeOffset();
|
|
|
|
|
|
|
|
|
|
var dateUtc = date.Kind switch
|
|
|
|
|
{
|
|
|
|
|
DateTimeKind.Local => date.ToUniversalTime(),
|
2022-01-05 17:50:45 +05:00
|
|
|
|
DateTimeKind.Unspecified => DateTime.SpecifyKind(date.AddHours(-remoteTimezoneOffsetHours), DateTimeKind.Utc),
|
2021-12-30 17:05:44 +05:00
|
|
|
|
_ => date,
|
|
|
|
|
};
|
2022-04-11 18:00:34 +05:00
|
|
|
|
return new DateTimeOffset(dateUtc);
|
2021-12-30 17:05:44 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 16:00:11 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Привести ко времени куста из utc
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="date"></param>
|
|
|
|
|
/// <param name="remoteTimezoneOffsetHours"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-12-30 17:05:44 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|