From b18cc65cc80d1f21b47f6ad25a673adbaeab6084 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 23 Apr 2013 13:33:55 +0400 Subject: Prettify datetime output --- Juick/Classes/DateHelper.cs | 67 +++++++++++++++++++++++++++ Juick/DataTemplates/PostItemDataTemplate.xaml | 4 +- Juick/Juick.csproj | 1 + Juick/ViewModels/PostItem.cs | 9 +++- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 Juick/Classes/DateHelper.cs diff --git a/Juick/Classes/DateHelper.cs b/Juick/Classes/DateHelper.cs new file mode 100644 index 0000000..072c56a --- /dev/null +++ b/Juick/Classes/DateHelper.cs @@ -0,0 +1,67 @@ +using System; +using System.Net; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Ink; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Shapes; +using System.Globalization; + +namespace Juick.Classes +{ + public class DateHelper + { + public static string PrettyDate(DateTime dateTime) + { + var timeSpan = DateTime.Now - dateTime; + + // span is less than or equal to 60 seconds, measure in seconds. + if (timeSpan <= TimeSpan.FromSeconds(60)) + { + return timeSpan.Seconds > 5 + ? "about " + timeSpan.Seconds + " seconds ago" + : "just now"; + } + + // span is less than or equal to 60 minutes, measure in minutes. + if (timeSpan <= TimeSpan.FromMinutes(60)) + { + return timeSpan.Minutes > 1 + ? "about " + timeSpan.Minutes + " minutes ago" + : "about a minute ago"; + } + + // span is less than or equal to 24 hours, measure in hours. + if (timeSpan <= TimeSpan.FromHours(24)) + { + return timeSpan.Hours > 1 + ? "about " + timeSpan.Hours + " hours ago" + : "about an hour ago"; + } + + // span is less than or equal to 30 days (1 month), measure in days. + if (timeSpan <= TimeSpan.FromDays(30)) + { + return timeSpan.Days > 1 + ? "about " + timeSpan.Days + " days ago" + : "about a day ago"; + } + + // span is less than or equal to 365 days (1 year), measure in months. + if (timeSpan <= TimeSpan.FromDays(365)) + { + return timeSpan.Days > 30 + ? "about " + timeSpan.Days / 30 + " months ago" + : "about a month ago"; + } + + // span is greater than 365 days (1 year), measure in years. + return timeSpan.Days > 365 + ? "about " + timeSpan.Days / 365 + " years ago" + : "about a year ago"; + } + } +} diff --git a/Juick/DataTemplates/PostItemDataTemplate.xaml b/Juick/DataTemplates/PostItemDataTemplate.xaml index 285a5dc..4668a48 100644 --- a/Juick/DataTemplates/PostItemDataTemplate.xaml +++ b/Juick/DataTemplates/PostItemDataTemplate.xaml @@ -1,7 +1,7 @@  @@ -36,7 +36,7 @@ Style="{StaticResource PhoneTextAccentStyle}" FontSize="{StaticResource PhoneFontSizeSmall}" Margin="5,0,5,5" VerticalAlignment="Top" - TextWrapping="Wrap" HorizontalAlignment="Left"/> + TextWrapping="NoWrap" TextAlignment="Right"/> diff --git a/Juick/Juick.csproj b/Juick/Juick.csproj index 9d7c770..c3fe200 100644 --- a/Juick/Juick.csproj +++ b/Juick/Juick.csproj @@ -82,6 +82,7 @@ App.xaml + diff --git a/Juick/ViewModels/PostItem.cs b/Juick/ViewModels/PostItem.cs index 97935b3..88f6b86 100644 --- a/Juick/ViewModels/PostItem.cs +++ b/Juick/ViewModels/PostItem.cs @@ -1,6 +1,8 @@ using System; using System.Net; using JuickApi; +using Juick.Classes; +using System.Globalization; namespace Juick.ViewModels { @@ -11,8 +13,11 @@ namespace Juick.ViewModels MID = message.Mid; RID = message.Rid; Username = message.User.UName; - Status = string.Format("Posted on: {0}, replies: {1}", message.Timestamp, message.Replies); - + // Juick timestamp in utc: 2013-04-22 13:14:30 + var timestamp = DateTime.ParseExact(message.Timestamp, "yyyy-MM-dd HH:mm:ss", null); + Status = string.Format("{0}", DateHelper.PrettyDate(timestamp.ToLocalTime())); + if (message.Replies > 0) + Status = string.Format("{0}, replies: {1}", Status, message.Replies); MessageText = HttpUtility.HtmlDecode(message.Body); if (message.Tags != null) -- cgit v1.2.3