[PTRun][DateTime]Setting for First week of year and First day of week (#33406)

## Summary of the Pull Request

This PR implements two new plugin settings:
- **First week of year**

![image](https://github.com/microsoft/PowerToys/assets/61519853/c866ffc2-2a21-438c-9a1a-5f4c7f68a22e)

- **First day of week**

![image](https://github.com/microsoft/PowerToys/assets/61519853/b2ec125b-d87c-40c5-8793-743a1ffae237)

## Detailed Description of the Pull Request / Additional comments

For both settings the users can decide to be in sync with the system
settings (default) or to use their own setting. The order of days for
the `first day of week` setting is based on the current system culture.

PT Run respects these settings for the relevant results:
- calendar week
- week of month
- number of day in week
This commit is contained in:
Heiko
2024-07-18 15:29:01 +02:00
committed by GitHub
parent 98cfeb0776
commit 7808033436
9 changed files with 483 additions and 43 deletions

View File

@@ -22,10 +22,12 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
var result = settings?.Length;
// Assert
Assert.AreEqual(4, result);
Assert.AreEqual(6, result);
}
[DataTestMethod]
[DataRow("CalendarFirstWeekRule")]
[DataRow("FirstDayOfWeek")]
[DataRow("OnlyDateTimeNowGlobal")]
[DataRow("TimeWithSeconds")]
[DataRow("DateWithWeekday")]
@@ -59,5 +61,21 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Assert
Assert.AreEqual(valueExpected, result);
}
[DataTestMethod]
[DataRow("CalendarFirstWeekRule", -1)]
[DataRow("FirstDayOfWeek", -1)]
public void DefaultEnumValues(string name, int valueExpected)
{
// Setup
TimeDateSettings setting = TimeDateSettings.Instance;
// Act
PropertyInfo propertyInfo = setting?.GetType()?.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance);
var result = propertyInfo?.GetValue(setting);
// Assert
Assert.AreEqual(valueExpected, result);
}
}
}

View File

@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using Microsoft.PowerToys.Run.Plugin.TimeDate.Components;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class TimeAndDateHelperTests
{
[DataTestMethod]
[DataRow(-1, null)] // default setting
[DataRow(0, CalendarWeekRule.FirstDay)]
[DataRow(1, CalendarWeekRule.FirstFullWeek)]
[DataRow(2, CalendarWeekRule.FirstFourDayWeek)]
[DataRow(30, null)] // wrong setting
public void GetCalendarWeekRuleBasedOnPluginSetting(int setting, CalendarWeekRule? valueExpected)
{
// Act
var result = TimeAndDateHelper.GetCalendarWeekRule(setting);
// Assert
if (valueExpected == null)
{
// falls back to system setting.
Assert.AreEqual(DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, result);
}
else
{
Assert.AreEqual(valueExpected, result);
}
}
[DataTestMethod]
[DataRow(-1, null)] // default setting
[DataRow(1, DayOfWeek.Monday)]
[DataRow(2, DayOfWeek.Tuesday)]
[DataRow(3, DayOfWeek.Wednesday)]
[DataRow(4, DayOfWeek.Thursday)]
[DataRow(5, DayOfWeek.Friday)]
[DataRow(6, DayOfWeek.Saturday)]
[DataRow(0, DayOfWeek.Sunday)]
[DataRow(70, null)] // wrong setting
public void GetFirstDayOfWeekBasedOnPluginSetting(int setting, DayOfWeek? valueExpected)
{
// Act
var result = TimeAndDateHelper.GetFirstDayOfWeek(setting);
// Assert
if (valueExpected == null)
{
// falls back to system setting.
Assert.AreEqual(DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek, result);
}
else
{
Assert.AreEqual(valueExpected, result);
}
}
}
}

View File

@@ -65,7 +65,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void LocalFormatsWithShortTimeAndShortDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest());
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest(), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@@ -100,7 +100,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void LocalFormatsWithShortTimeAndLongDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest());
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest(), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@@ -135,7 +135,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void LocalFormatsWithLongTimeAndShortDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest());
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest(), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@@ -170,7 +170,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void LocalFormatsWithLongTimeAndLongDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest());
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest(), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@@ -189,7 +189,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithShortTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest(true));
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest(true), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
@@ -209,7 +209,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithShortTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest(true));
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest(true), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
@@ -229,7 +229,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithLongTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest(true));
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest(true), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
@@ -249,7 +249,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithLongTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest(true));
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest(true), CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
@@ -265,7 +265,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Setup
string formatLabel = "Unix epoch time";
DateTime timeValue = DateTime.Now.ToUniversalTime();
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = (long)timeValue.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
// Act
@@ -281,7 +281,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Setup
string formatLabel = "Unix epoch time in milliseconds";
DateTime timeValue = DateTime.Now.ToUniversalTime();
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = (long)timeValue.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
// Act
@@ -297,7 +297,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Setup
string formatLabel = "Windows file time (Int64 number)";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = timeValue.ToFileTime().ToString(CultureInfo.CurrentCulture);
// Act
@@ -313,7 +313,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Setup
string formatLabel = "Era";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = DateTimeFormatInfo.CurrentInfo.GetEraName(CultureInfo.CurrentCulture.Calendar.GetEra(timeValue));
// Act
@@ -329,7 +329,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Setup
string formatLabel = "Era abbreviation";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
var expectedResult = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(CultureInfo.CurrentCulture.Calendar.GetEra(timeValue));
// Act
@@ -339,6 +339,48 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow(CalendarWeekRule.FirstDay, "3")]
[DataRow(CalendarWeekRule.FirstFourDayWeek, "2")]
[DataRow(CalendarWeekRule.FirstFullWeek, "2")]
public void DifferentFirstWeekSettingConfigurations(CalendarWeekRule weekRule, string expectedWeekOfYear)
{
// Setup
DateTime timeValue = new DateTime(2021, 1, 12);
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, weekRule, DayOfWeek.Sunday);
// Act
var resultWeekOfYear = helperResults.FirstOrDefault(x => x.Label.Equals("week of the year (calendar week, week number)", StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedWeekOfYear, resultWeekOfYear?.Value);
}
[DataTestMethod]
[DataRow(DayOfWeek.Monday, "2", "2", "5")]
[DataRow(DayOfWeek.Tuesday, "3", "3", "4")]
[DataRow(DayOfWeek.Wednesday, "3", "3", "3")]
[DataRow(DayOfWeek.Thursday, "3", "3", "2")]
[DataRow(DayOfWeek.Friday, "3", "3", "1")]
[DataRow(DayOfWeek.Saturday, "2", "2", "7")]
[DataRow(DayOfWeek.Sunday, "2", "2", "6")]
public void DifferentFirstDayOfWeekSettingConfigurations(DayOfWeek dayOfWeek, string expectedWeekOfYear, string expectedWeekOfMonth, string expectedDayInWeek)
{
// Setup
DateTime timeValue = new DateTime(2024, 1, 12); // Friday
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue, CalendarWeekRule.FirstDay, dayOfWeek);
// Act
var resultWeekOfYear = helperResults.FirstOrDefault(x => x.Label.Equals("week of the year (calendar week, week number)", StringComparison.OrdinalIgnoreCase));
var resultWeekOfMonth = helperResults.FirstOrDefault(x => x.Label.Equals("week of the month", StringComparison.OrdinalIgnoreCase));
var resultDayInWeek = helperResults.FirstOrDefault(x => x.Label.Equals("day of the week (week day)", StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedWeekOfYear, resultWeekOfYear?.Value);
Assert.AreEqual(expectedWeekOfMonth, resultWeekOfMonth?.Value);
Assert.AreEqual(expectedDayInWeek, resultDayInWeek?.Value);
}
[TestCleanup]
public void CleanUp()
{