[PTRun][TimeDate]Unix Time in milliseconds, fix negative unix time input, error message improvement (#29149)

* add unix time in ms

* update dev docs

* unit tests

* fix spell check

* tool tip

* fix negative unix time input, improve regex, update unit tests, improve error message

* Update error handling and tests

* add tests and fix spelling

* small fixes
This commit is contained in:
Heiko
2023-10-18 11:45:00 +02:00
committed by GitHub
parent cd99a2e848
commit 50cb279d2a
11 changed files with 94 additions and 29 deletions

View File

@@ -61,6 +61,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
{
// We use long instead of int for unix time stamp because int is too small after 03:14:07 UTC 2038-01-19
long unixTimestamp = (long)dateTimeNowUtc.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
long unixTimestampMilliseconds = (long)dateTimeNowUtc.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
int weekOfYear = calendar.GetWeekOfYear(dateTimeNow, DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
string era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
string eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));
@@ -89,6 +90,13 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
IconType = ResultIconType.DateTime,
},
new AvailableResult()
{
Value = unixTimestampMilliseconds.ToString(CultureInfo.CurrentCulture),
Label = Resources.Microsoft_plugin_timedate_Unix_Milliseconds,
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
IconType = ResultIconType.DateTime,
},
new AvailableResult()
{
Value = dateTimeNow.Hour.ToString(CultureInfo.CurrentCulture),
Label = Resources.Microsoft_plugin_timedate_Hour,

View File

@@ -86,6 +86,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
{
Title = Resources.Microsoft_plugin_timedate_ErrorResultTitle,
SubTitle = Resources.Microsoft_plugin_timedate_ErrorResultSubTitle,
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_timedate_ErrorResultTitle, Resources.Microsoft_plugin_timedate_ErrorResultSubTitle),
IcoPath = $"Images\\Warning.{theme}.png",
};
}

View File

@@ -121,8 +121,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
}
// If search term is only a number that can't be parsed return an error message
if (!isEmptySearchInput && results.Count == 0 && searchTerm.Any(char.IsNumber) && Regex.IsMatch(searchTerm, @"\w+\d+$") &&
!searchTerm.Contains(InputDelimiter) && !searchTerm.Any(char.IsWhiteSpace) && !searchTerm.Any(char.IsPunctuation))
if (!isEmptySearchInput && results.Count == 0 && Regex.IsMatch(searchTerm, @"\w+\d+.*$") && !searchTerm.Any(char.IsWhiteSpace) && (TimeAndDateHelper.IsSpecialInputParsing(searchTerm) || !Regex.IsMatch(searchTerm, @"\d+[\.:/]\d+")))
{
// Without plugin key word show only if message is not hidden by setting
if (isKeywordSearch || !TimeDateSettings.Instance.HideNumberMessageOnGlobalQuery)

View File

@@ -96,17 +96,24 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
// Known date/time format
return true;
}
else if (Regex.IsMatch(input, @"^u\d+") && input.Length <= 12 && long.TryParse(input.TrimStart('u'), out long secondsInt))
else if (Regex.IsMatch(input, @"^u[\+-]?\d{1,10}$") && long.TryParse(input.TrimStart('u'), out long secondsU))
{
// unix time stamp
// we use long instead of int because int is too small after 03:14:07 UTC 2038-01-19
timestamp = new DateTime(1970, 1, 1).AddSeconds(secondsInt).ToLocalTime();
timestamp = new DateTime(1970, 1, 1).AddSeconds(secondsU).ToLocalTime();
return true;
}
else if (Regex.IsMatch(input, @"^ft\d+") && long.TryParse(input.TrimStart("ft".ToCharArray()), out long secondsLong))
else if (Regex.IsMatch(input, @"^ums[\+-]?\d{1,13}$") && long.TryParse(input.TrimStart("ums".ToCharArray()), out long millisecondsUms))
{
// unix time stamp in milliseconds
// we use long instead of int because int is too small after 03:14:07 UTC 2038-01-19
timestamp = new DateTime(1970, 1, 1).AddMilliseconds(millisecondsUms).ToLocalTime();
return true;
}
else if (Regex.IsMatch(input, @"^ft\d+$") && long.TryParse(input.TrimStart("ft".ToCharArray()), out long secondsFt))
{
// windows file time
timestamp = new DateTime(secondsLong);
timestamp = new DateTime(secondsFt);
return true;
}
else
@@ -115,6 +122,16 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
return false;
}
}
/// <summary>
/// Test if input is special parsing for Unix time, Unix time in milliseconds or File time.
/// </summary>
/// <param name="input">String with date/time</param>
/// <returns>True if yes, otherwise false</returns>
internal static bool IsSpecialInputParsing(string input)
{
return Regex.IsMatch(input, @"^.*(u|ums|ft)\d");
}
}
/// <summary>