Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/settings-ui/Settings.UI.Library/UpdatingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,35 @@ public string NewVersion
}

public string LastCheckedDateLocalized
{
get
{
var dt = LastCheckedDateTime;
return dt?.ToString(CultureInfo.CurrentCulture) ?? string.Empty;
}
}

[JsonIgnore]
public DateTime? LastCheckedDateTime
{
get
{
try
{
if (LastCheckedDate == null)
{
return string.Empty;
return null;
}

long seconds = long.Parse(LastCheckedDate, CultureInfo.CurrentCulture);
var date = DateTimeOffset.FromUnixTimeSeconds(seconds).UtcDateTime;
return date.ToLocalTime().ToString(CultureInfo.CurrentCulture);
return date.ToLocalTime();
}
catch (Exception)
{
}

return string.Empty;
return null;
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/settings-ui/Settings.UI/Helpers/FriendlyDateHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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;

namespace Microsoft.PowerToys.Settings.UI.Helpers;

public static class FriendlyDateHelper
{
/// <summary>
/// Formats a <see cref="DateTime"/> as a friendly relative string.
/// Today → "Today at 1:22 PM", Yesterday → "Yesterday at 3:45 PM",
/// older dates fall back to the full culture-specific date/time format.
/// </summary>
public static string Format(DateTime? dateTime)
{
if (dateTime is not DateTime dt)
{
return string.Empty;
}

var resourceLoader = ResourceLoaderInstance.ResourceLoader;
var today = DateTime.Now.Date;
var time = dt.ToString("t", CultureInfo.CurrentCulture);

if (dt.Date == today)
{
var fmt = resourceLoader.GetString("General_LastCheckedDate_TodayAt");
if (!string.IsNullOrEmpty(fmt))
{
return string.Format(CultureInfo.CurrentCulture, fmt, time);
}
}

if (dt.Date == today.AddDays(-1))
{
var fmt = resourceLoader.GetString("General_LastCheckedDate_YesterdayAt");
if (!string.IsNullOrEmpty(fmt))
{
return string.Format(CultureInfo.CurrentCulture, fmt, time);
}
}

return dt.ToString(CultureInfo.CurrentCulture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<TextBlock x:Uid="YoureUpToDate" FontWeight="SemiBold" />
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Style="{StaticResource CaptionTextBlockStyle}">
<Run x:Uid="General_VersionLastChecked" />
<Run Text="{x:Bind UpdateSettingsConfig.LastCheckedDateLocalized, Mode=OneTime}" />
<Run Text="{x:Bind LastCheckedDateFriendly, Mode=OneTime}" />
</TextBlock>
</StackPanel>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Services;
using Microsoft.PowerToys.Settings.UI.Views;
Expand All @@ -15,11 +16,14 @@ public sealed partial class CheckUpdateControl : UserControl

public UpdatingSettings UpdateSettingsConfig { get; set; }

public string LastCheckedDateFriendly { get; set; }

public CheckUpdateControl()
{
InitializeComponent();
UpdateSettingsConfig = UpdatingSettings.LoadSettings();
UpdateAvailable = UpdateSettingsConfig != null && (UpdateSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToInstall || UpdateSettingsConfig.State == UpdatingSettings.UpdatingState.ReadyToDownload);
LastCheckedDateFriendly = FriendlyDateHelper.Format(UpdateSettingsConfig?.LastCheckedDateTime);
}

private void SWVersionButtonClicked(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
Expand Down
8 changes: 8 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,14 @@ opera.exe</value>
<data name="General_VersionLastChecked.Text" xml:space="preserve">
<value>Last checked: </value>
</data>
<data name="General_LastCheckedDate_TodayAt" xml:space="preserve">
<value>Today at {0}</value>
<comment>Friendly date format when the last update check was today. {0} is the localized short time, e.g. "Today at 1:22 PM".</comment>
</data>
<data name="General_LastCheckedDate_YesterdayAt" xml:space="preserve">
<value>Yesterday at {0}</value>
<comment>Friendly date format when the last update check was yesterday. {0} is the localized short time, e.g. "Yesterday at 3:45 PM".</comment>
</data>
<data name="General_SettingsBackupInfo_DateHeader.Text" xml:space="preserve">
<value>Created at:</value>
</data>
Expand Down
6 changes: 3 additions & 3 deletions src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public GeneralViewModel(ISettingsRepository<GeneralSettings> settingsRepository,
_updatingState = UpdatingSettingsConfig.State;
_newAvailableVersion = UpdatingSettingsConfig.NewVersion;
_newAvailableVersionLink = UpdatingSettingsConfig.ReleasePageLink;
_updateCheckedDate = UpdatingSettingsConfig.LastCheckedDateLocalized;
_updateCheckedDate = FriendlyDateHelper.Format(UpdatingSettingsConfig.LastCheckedDateTime);

_newUpdatesToastIsGpoDisabled = GPOWrapper.GetDisableNewUpdateToastValue() == GpoRuleConfigured.Enabled;
_autoDownloadUpdatesIsGpoDisabled = GPOWrapper.GetDisableAutomaticUpdateDownloadValue() == GpoRuleConfigured.Enabled;
Expand Down Expand Up @@ -1383,15 +1383,15 @@ public void RefreshUpdatingState()
}
else
{
bool dateChanged = UpdateCheckedDate == UpdatingSettingsConfig.LastCheckedDateLocalized;
bool dateChanged = UpdateCheckedDate == FriendlyDateHelper.Format(UpdatingSettingsConfig.LastCheckedDateTime);
bool fileDownloaded = string.IsNullOrEmpty(UpdatingSettingsConfig.DownloadedInstallerFilename);
IsNewVersionDownloading = !(dateChanged || fileDownloaded);
}

PowerToysUpdatingState = UpdatingSettingsConfig.State;
PowerToysNewAvailableVersion = UpdatingSettingsConfig.NewVersion;
PowerToysNewAvailableVersionLink = UpdatingSettingsConfig.ReleasePageLink;
UpdateCheckedDate = UpdatingSettingsConfig.LastCheckedDateLocalized;
UpdateCheckedDate = FriendlyDateHelper.Format(UpdatingSettingsConfig.LastCheckedDateTime);

_isNoNetwork = PowerToysUpdatingState == UpdatingSettings.UpdatingState.NetworkError;
NotifyPropertyChanged(nameof(IsNoNetwork));
Expand Down
Loading