using SJZY.OTel;
namespace SJZY.Track.Application.Helpers;
public static class DateTimeHelper
{
///
/// convert to a Unix timestamp from a DateTime object.
///
/// The DateTime object to convert to a Unix timestamp.
/// The Unix timestamp as a long.
public static long ConvertToUnixTimestamp(DateTime dateTime)
{
return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
}
///
/// Formats a DateTime object into a specified format.
///
/// The DateTime object to format.
/// The format string.
/// The formatted date string.
public static string FormatDateTime(DateTime dateTime, string format)
{
return dateTime.ToString(format);
}
///
/// 时区格式化
///
///
///
///
public static string TimezoneFormat(double timezone, string format)
{
//时区是整数丢掉浮点数部分
return format.Replace("hh", ((int)timezone).ToString());
}
///
/// 转换主数据中心类似GMT-09:00这种的时区为浮点数
///
/// 分区
///
///
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);
}
}