[CmdPal] Added fallback for time and date (#38918)

* Added fallback for time and date

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* only support week/now/time/year query

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* Add week option

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* Changed setting for time date fallback.

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* update globalization string

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* use week of year.

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

* update

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>

---------

Signed-off-by: Shawn Yuan <shuai.yuan.zju@gmail.com>
This commit is contained in:
Shawn Yuan
2025-04-24 14:58:01 +08:00
committed by GitHub
parent 25c29ade8e
commit 100d560f9e
7 changed files with 172 additions and 81 deletions

View File

@@ -33,6 +33,7 @@ internal static class AvailableResultsList
var dateTimeNowUtc = dateTimeNow.ToUniversalTime();
var firstWeekRule = firstWeekOfYear ?? TimeAndDateHelper.GetCalendarWeekRule(settings.FirstWeekOfYear);
var firstDayOfTheWeek = firstDayOfWeek ?? TimeAndDateHelper.GetFirstDayOfWeek(settings.FirstDayOfWeek);
var weekOfYear = calendar.GetWeekOfYear(dateTimeNow, firstWeekRule, firstDayOfTheWeek);
results.AddRange(new[]
{
@@ -59,14 +60,20 @@ internal static class AvailableResultsList
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
IconType = ResultIconType.DateTime,
},
new AvailableResult()
{
Value = weekOfYear.ToString(CultureInfo.CurrentCulture),
Label = Resources.Microsoft_plugin_timedate_WeekOfYear,
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
IconType = ResultIconType.Date,
},
});
if (isKeywordSearch || !settings.OnlyDateTimeNowGlobal)
if (isKeywordSearch)
{
// We use long instead of int for unix time stamp because int is too small after 03:14:07 UTC 2038-01-19
var unixTimestamp = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeSeconds();
var unixTimestampMilliseconds = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeMilliseconds();
var weekOfYear = calendar.GetWeekOfYear(dateTimeNow, firstWeekRule, firstDayOfTheWeek);
var era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
var eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));
@@ -251,13 +258,6 @@ internal static class AvailableResultsList
IconType = ResultIconType.Date,
},
new AvailableResult()
{
Value = weekOfYear.ToString(CultureInfo.CurrentCulture),
Label = Resources.Microsoft_plugin_timedate_WeekOfYear,
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
IconType = ResultIconType.Date,
},
new AvailableResult()
{
Value = DateTimeFormatInfo.CurrentInfo.GetMonthName(dateTimeNow.Month),
Label = Resources.Microsoft_plugin_timedate_Month,

View File

@@ -75,11 +75,11 @@ public class SettingsManager : JsonSettingsManager
Resources.Microsoft_plugin_timedate_SettingFirstDayOfWeek,
_firstDayOfWeekChoices);
private readonly ToggleSetting _onlyDateTimeNowGlobal = new(
Namespaced(nameof(OnlyDateTimeNowGlobal)),
Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal,
Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description,
true); // TODO -- double check default value
private readonly ToggleSetting _enableFallbackItems = new(
Namespaced(nameof(EnableFallbackItems)),
Resources.Microsoft_plugin_timedate_SettingEnableFallbackItems,
Resources.Microsoft_plugin_timedate_SettingEnableFallbackItems_Description,
true);
private readonly ToggleSetting _timeWithSeconds = new(
Namespaced(nameof(TimeWithSecond)),
@@ -93,12 +93,6 @@ public class SettingsManager : JsonSettingsManager
Resources.Microsoft_plugin_timedate_SettingDateWithWeekday_Description,
false); // TODO -- double check default value
private readonly ToggleSetting _hideNumberMessageOnGlobalQuery = new(
Namespaced(nameof(HideNumberMessageOnGlobalQuery)),
Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery,
Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery,
true); // TODO -- double check default value
private readonly TextSetting _customFormats = new(
Namespaced(nameof(CustomFormats)),
Resources.Microsoft_plugin_timedate_Setting_CustomFormats,
@@ -145,14 +139,12 @@ public class SettingsManager : JsonSettingsManager
}
}
public bool OnlyDateTimeNowGlobal => _onlyDateTimeNowGlobal.Value;
public bool EnableFallbackItems => _enableFallbackItems.Value;
public bool TimeWithSecond => _timeWithSeconds.Value;
public bool DateWithWeekday => _dateWithWeekday.Value;
public bool HideNumberMessageOnGlobalQuery => _hideNumberMessageOnGlobalQuery.Value;
public List<string> CustomFormats => _customFormats.Value.Split(TEXTBOXNEWLINE).ToList();
internal static string SettingsJsonPath()
@@ -168,10 +160,7 @@ public class SettingsManager : JsonSettingsManager
{
FilePath = SettingsJsonPath();
/* The following two settings make no sense with current CmdPal behavior.
Settings.Add(_onlyDateTimeNowGlobal);
Settings.Add(_hideNumberMessageOnGlobalQuery); */
Settings.Add(_enableFallbackItems);
Settings.Add(_timeWithSeconds);
Settings.Add(_dateWithWeekday);
Settings.Add(_firstWeekOfYear);

View File

@@ -40,7 +40,7 @@ public sealed partial class TimeDateCalculator
var lastInputParsingErrorMsg = string.Empty;
// Switch search type
if (isEmptySearchInput || (!isKeywordSearch && settings.OnlyDateTimeNowGlobal))
if (isEmptySearchInput || (!isKeywordSearch))
{
// Return all results for system time/date on empty keyword search
// or only time, date and now results for system time on global queries if the corresponding setting is enabled
@@ -91,23 +91,6 @@ public sealed partial class TimeDateCalculator
}
}
/*htcfreek:Code obsolete with current CmdPal behavior.
// If search term is only a number that can't be parsed return an error message
if (!isEmptySearchInput && results.Count == 0 && Regex.IsMatch(query, @"\w+\d+.*$") && !query.Any(char.IsWhiteSpace) && (TimeAndDateHelper.IsSpecialInputParsing(query) || !Regex.IsMatch(query, @"\d+[\.:/]\d+")))
{
// Without plugin key word show only if message is not hidden by setting
if (!settings.HideNumberMessageOnGlobalQuery)
{
var er = ResultHelper.CreateInvalidInputErrorResult();
if (!string.IsNullOrEmpty(lastInputParsingErrorMsg))
{
er.Details = new Details() { Body = lastInputParsingErrorMsg };
}
results.Add(er);
}
} */
if (results.Count == 0)
{
var er = ResultHelper.CreateInvalidInputErrorResult();