60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using SJZY.OTel;
|
|
|
|
namespace SJZY.Track.Application.Helpers;
|
|
|
|
public static class DateTimeHelper
|
|
{
|
|
/// <summary>
|
|
/// convert to a Unix timestamp from a DateTime object.
|
|
/// </summary>
|
|
/// <param name="dateTime">The DateTime object to convert to a Unix timestamp.</param>
|
|
/// <returns>The Unix timestamp as a long.</returns>
|
|
public static long ConvertToUnixTimestamp(DateTime dateTime)
|
|
{
|
|
return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Formats a DateTime object into a specified format.
|
|
/// </summary>
|
|
/// <param name="dateTime">The DateTime object to format.</param>
|
|
/// <param name="format">The format string.</param>
|
|
/// <returns>The formatted date string.</returns>
|
|
public static string FormatDateTime(DateTime dateTime, string format)
|
|
{
|
|
return dateTime.ToString(format);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时区格式化
|
|
/// </summary>
|
|
/// <param name="timezone"></param>
|
|
/// <param name="format"></param>
|
|
/// <returns></returns>
|
|
public static string TimezoneFormat(double timezone, string format)
|
|
{
|
|
//时区是整数丢掉浮点数部分
|
|
return format.Replace("hh", ((int)timezone).ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换主数据中心类似GMT-09:00这种的时区为浮点数
|
|
/// </summary>
|
|
/// <param name="timeZone">分区</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public static float? ConvertTimeZoneToFloat(string timeZone)
|
|
{
|
|
if (string.IsNullOrEmpty(timeZone)|| !timeZone.StartsWith("GMT"))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var sign = timeZone[3] == '+' ? 1 : -1;
|
|
var parts = timeZone.Substring(4).Split(':');
|
|
var hours = int.Parse(parts[0]);
|
|
var minutes = int.Parse(parts[1]);
|
|
|
|
return sign * (hours + minutes / 60f);
|
|
}
|
|
} |