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]
|
[TestMethod]
|
||||||
public void UnixTimestampFormat()
|
public void UnixTimestampSecondsFormat()
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
string formatLabel = "Unix epoch time";
|
string formatLabel = "Unix epoch time";
|
||||||
@@ -275,6 +275,22 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
|
|||||||
Assert.AreEqual(expectedResult.ToString(CultureInfo.CurrentCulture), result?.Value);
|
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]
|
[TestMethod]
|
||||||
public void WindowsFileTimeFormat()
|
public void WindowsFileTimeFormat()
|
||||||
{
|
{
|
||||||
@@ -282,7 +298,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
|
|||||||
string formatLabel = "Windows file time (Int64 number)";
|
string formatLabel = "Windows file time (Int64 number)";
|
||||||
DateTime timeValue = DateTime.Now;
|
DateTime timeValue = DateTime.Now;
|
||||||
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
|
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
|
||||||
var expectedResult = timeValue.Ticks.ToString(CultureInfo.CurrentCulture);
|
var expectedResult = timeValue.ToFileTime().ToString(CultureInfo.CurrentCulture);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
|
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)
|
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
|
// 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 unixTimestamp = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeSeconds();
|
||||||
long unixTimestampMilliseconds = (long)dateTimeNowUtc.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
|
long unixTimestampMilliseconds = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeMilliseconds();
|
||||||
int weekOfYear = calendar.GetWeekOfYear(dateTimeNow, DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
|
int weekOfYear = calendar.GetWeekOfYear(dateTimeNow, DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
|
||||||
string era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
|
string era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
|
||||||
string eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));
|
string eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));
|
||||||
@@ -217,7 +217,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
|
|||||||
},
|
},
|
||||||
new AvailableResult()
|
new AvailableResult()
|
||||||
{
|
{
|
||||||
Value = dateTimeNow.Ticks.ToString(CultureInfo.CurrentCulture),
|
Value = dateTimeNow.ToFileTime().ToString(CultureInfo.CurrentCulture),
|
||||||
Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
|
Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
|
||||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
|
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
|
||||||
IconType = ResultIconType.DateTime,
|
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))
|
else if (Regex.IsMatch(input, @"^u[\+-]?\d{1,10}$") && long.TryParse(input.TrimStart('u'), out long secondsU))
|
||||||
{
|
{
|
||||||
// unix time stamp
|
// Unix time stamp
|
||||||
// we use long instead of int because int is too small after 03:14:07 UTC 2038-01-19
|
// 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();
|
timestamp = DateTimeOffset.FromUnixTimeSeconds(secondsU).LocalDateTime;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (Regex.IsMatch(input, @"^ums[\+-]?\d{1,13}$") && long.TryParse(input.TrimStart("ums".ToCharArray()), out long millisecondsUms))
|
else if (Regex.IsMatch(input, @"^ums[\+-]?\d{1,13}$") && long.TryParse(input.TrimStart("ums".ToCharArray()), out long millisecondsUms))
|
||||||
{
|
{
|
||||||
// unix time stamp in milliseconds
|
// Unix time stamp in milliseconds
|
||||||
// we use long instead of int because int is too small after 03:14:07 UTC 2038-01-19
|
// 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();
|
timestamp = DateTimeOffset.FromUnixTimeMilliseconds(millisecondsUms).LocalDateTime;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (Regex.IsMatch(input, @"^ft\d+$") && long.TryParse(input.TrimStart("ft".ToCharArray()), out long secondsFt))
|
else if (Regex.IsMatch(input, @"^ft\d+$") && long.TryParse(input.TrimStart("ft".ToCharArray()), out long secondsFt))
|
||||||
{
|
{
|
||||||
// windows file time
|
// Windows file time
|
||||||
timestamp = new DateTime(secondsFt);
|
// DateTime.FromFileTime returns as local time.
|
||||||
|
timestamp = DateTime.FromFileTime(secondsFt);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user