增加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

@@ -23,6 +23,8 @@ namespace XCharts.Runtime
private static Dictionary<double, Dictionary<string, string>> s_NumberToStr = new Dictionary<double, Dictionary<string, string>>();
private static Dictionary<int, Dictionary<string, string>> s_PrecisionToStr = new Dictionary<int, Dictionary<string, string>>();
private static Dictionary<string, Dictionary<int, string>> s_StringIntDict = new Dictionary<string, Dictionary<int, string>>();
private static Dictionary<double, DateTime> s_TimestampToDateTimeDict = new Dictionary<double, DateTime>();
private static Dictionary<double, TimeSpan> s_NumberToTimeSpanDict = new Dictionary<double, TimeSpan>();
private static CultureInfo GetDefaultCultureInfo()
{
@@ -64,10 +66,19 @@ namespace XCharts.Runtime
}
if (!s_NumberToStr[value].ContainsKey(formatter))
{
bool isDateFormatter = false;
string newFormatter = null;
if (string.IsNullOrEmpty(formatter))
{
s_NumberToStr[value][formatter] = value.ToString();
}
else if (DateTimeUtil.IsDateOrTimeRegex(formatter,ref isDateFormatter, ref newFormatter))
{
if(isDateFormatter)
s_NumberToStr[value][formatter] = NumberToDateStr(value, newFormatter);
else
s_NumberToStr[value][formatter] = NumberToTimeStr(value, newFormatter);
}
else if (formatter.StartsWith(NUMERIC_FORMATTER_D) ||
formatter.StartsWith(NUMERIC_FORMATTER_d) ||
formatter.StartsWith(NUMERIC_FORMATTER_X) ||
@@ -89,6 +100,56 @@ namespace XCharts.Runtime
return NumberToStr(value, numericFormatter);
}
public static string NumberToDateStr(double timestamp, string formatter)
{
var dt = NumberToDateTime(timestamp);
try
{
return dt.ToString(formatter, ci);
}
catch (Exception)
{
XLog.LogError("Not support DateTime format: " + formatter);
return timestamp.ToString();
}
}
public static string NumberToTimeStr(double timestamp, string formatter)
{
try
{
var ts = NumberToTimeSpan(timestamp);
#if UNITY_2018_3_OR_NEWER
return ts.ToString(formatter, ci);
#else
return ts.ToString();
#endif
}
catch (Exception)
{
XLog.LogError("Not support TimeSpan format: " + formatter);
return timestamp.ToString();
}
}
public static DateTime NumberToDateTime(double timestamp)
{
if (!s_TimestampToDateTimeDict.ContainsKey(timestamp))
{
s_TimestampToDateTimeDict[timestamp] = DateTimeUtil.GetDateTime(timestamp);
}
return s_TimestampToDateTimeDict[timestamp];
}
public static TimeSpan NumberToTimeSpan(double timestamp)
{
if(!s_NumberToTimeSpanDict.ContainsKey(timestamp))
{
s_NumberToTimeSpanDict[timestamp] = TimeSpan.FromSeconds(timestamp);
}
return s_NumberToTimeSpanDict[timestamp];
}
public static string ColorToStr(Color color)
{
if (s_ColorToStr.ContainsKey(color))