增加numericFormatterdatetime的支持

This commit is contained in:
monitor1394
2024-09-11 08:12:14 +08:00
parent 9a98d6a5df
commit 768cf8d404
10 changed files with 227 additions and 44 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
namespace XCharts.Runtime
@@ -7,10 +8,11 @@ namespace XCharts.Runtime
public static class DateTimeUtil
{
#if UNITY_2018_3_OR_NEWER
private static readonly DateTime k_DateTime1970 = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
private static readonly DateTime k_LocalDateTime1970 = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
#else
private static readonly DateTime k_DateTime1970 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
private static readonly DateTime k_LocalDateTime1970 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
#endif
private static readonly DateTime k_DateTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static readonly int ONE_SECOND = 1;
public static readonly int ONE_MINUTE = ONE_SECOND * 60;
public static readonly int ONE_HOUR = ONE_MINUTE * 60;
@@ -26,22 +28,54 @@ namespace XCharts.Runtime
//private static string s_MinuteDateFormatter = "mm:ss";
private static string s_SecondDateFormatter = "HH:mm:ss";
//private static string s_FullDateFormatter = "yyyy-MM-dd HH:mm:ss";
private static Regex s_DateOrTimeRegex = new Regex(@"^(date|time)\s*[:\s]+(.*)", RegexOptions.IgnoreCase);
public static bool IsDateOrTimeRegex(string regex)
{
return regex.StartsWith("date") || regex.StartsWith("time");
}
public static bool IsDateOrTimeRegex(string regex, ref bool date, ref string formatter)
{
if(IsDateOrTimeRegex(regex))
{
if(regex == "date" || regex == "time")
{
date = regex == "date";
formatter = "";
return true;
}
var mc = s_DateOrTimeRegex.Matches(regex);
date = mc[0].Groups[1].Value == "date";
formatter = mc[0].Groups[2].Value;
return true;
}
return false;
}
public static int GetTimestamp()
{
return (int)(DateTime.Now - k_DateTime1970).TotalSeconds;
return (int)(DateTime.Now - k_LocalDateTime1970).TotalSeconds;
}
public static int GetTimestamp(DateTime time)
public static int GetTimestamp(DateTime time, bool local = false)
{
return (int)(time - k_DateTime1970).TotalSeconds;
if (local)
{
return (int)(time - k_LocalDateTime1970).TotalSeconds;
}
else
{
return (int)(time - k_DateTime1970).TotalSeconds;
}
}
public static int GetTimestamp(string dateTime)
public static int GetTimestamp(string dateTime, bool local = false)
{
try
{
return GetTimestamp(DateTime.Parse(dateTime));
return GetTimestamp(DateTime.Parse(dateTime), local);
}
catch (Exception e)
{
@@ -49,14 +83,9 @@ namespace XCharts.Runtime
}
}
public static DateTime GetDateTime(double timestamp)
public static DateTime GetDateTime(double timestamp, bool local = true)
{
return k_DateTime1970.AddSeconds(timestamp);
}
public static DateTime GetDateTime(int timestamp)
{
return k_DateTime1970.AddSeconds(timestamp);
return local ? k_LocalDateTime1970.AddSeconds(timestamp) : k_DateTime1970.AddSeconds(timestamp);
}
public static string GetDefaultDateTimeString(int timestamp, double range = 0)