mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[PTRun][DateTime]Fix and improvements for Windows File Time and Unix Epoch Time (#29900)
* improvements and fixes for unix time and windows file time * spell fix
This commit is contained in:
@@ -260,7 +260,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UnixTimestampFormat()
|
||||
public void UnixTimestampSecondsFormat()
|
||||
{
|
||||
// Setup
|
||||
string formatLabel = "Unix epoch time";
|
||||
@@ -275,6 +275,22 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
|
||||
Assert.AreEqual(expectedResult.ToString(CultureInfo.CurrentCulture), result?.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UnixTimestampMillisecondsFormat()
|
||||
{
|
||||
// Setup
|
||||
string formatLabel = "Unix epoch time in milliseconds";
|
||||
DateTime timeValue = DateTime.Now.ToUniversalTime();
|
||||
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
|
||||
var expectedResult = (long)timeValue.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
|
||||
|
||||
// Act
|
||||
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expectedResult.ToString(CultureInfo.CurrentCulture), result?.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WindowsFileTimeFormat()
|
||||
{
|
||||
@@ -282,7 +298,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
|
||||
string formatLabel = "Windows file time (Int64 number)";
|
||||
DateTime timeValue = DateTime.Now;
|
||||
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
|
||||
var expectedResult = timeValue.Ticks.ToString(CultureInfo.CurrentCulture);
|
||||
var expectedResult = timeValue.ToFileTime().ToString(CultureInfo.CurrentCulture);
|
||||
|
||||
// Act
|
||||
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
@@ -60,8 +60,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
|
||||
if (isKeywordSearch || !TimeDateSettings.Instance.OnlyDateTimeNowGlobal)
|
||||
{
|
||||
// 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;
|
||||
long unixTimestamp = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeSeconds();
|
||||
long unixTimestampMilliseconds = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeMilliseconds();
|
||||
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));
|
||||
@@ -217,7 +217,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
|
||||
},
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = dateTimeNow.Ticks.ToString(CultureInfo.CurrentCulture),
|
||||
Value = dateTimeNow.ToFileTime().ToString(CultureInfo.CurrentCulture),
|
||||
Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
|
||||
IconType = ResultIconType.DateTime,
|
||||
|
||||
@@ -98,22 +98,23 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
|
||||
}
|
||||
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(secondsU).ToLocalTime();
|
||||
// Unix time stamp
|
||||
// We use long instead of int, because int is too small after 03:14:07 UTC 2038-01-19
|
||||
timestamp = DateTimeOffset.FromUnixTimeSeconds(secondsU).LocalDateTime;
|
||||
return true;
|
||||
}
|
||||
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();
|
||||
// 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 = DateTimeOffset.FromUnixTimeMilliseconds(millisecondsUms).LocalDateTime;
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^ft\d+$") && long.TryParse(input.TrimStart("ft".ToCharArray()), out long secondsFt))
|
||||
{
|
||||
// windows file time
|
||||
timestamp = new DateTime(secondsFt);
|
||||
// Windows file time
|
||||
// DateTime.FromFileTime returns as local time.
|
||||
timestamp = DateTime.FromFileTime(secondsFt);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user