annotate MetroWpf/MetroWpf.Xaml/Converters/AgeToReadableStringConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
rev   line source
15
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
1 using System;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
2 using System.Windows.Data;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
3 using System.Globalization;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
4
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
5 namespace MetroWpf.Xaml.Converters
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
6 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
7 public class AgeToReadableTextConverter: IValueConverter
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
8 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
9 #region IValueConverter Members
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
10
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
11 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
12 /// Converts a DateTime into a string.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
13 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
14 /// <param name="value">The DateTime to convert.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
15 /// <param name="targetType">The target type of the conversion.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
16 /// <param name="parameter">The conversion parameter.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
17 /// <param name="culture">The conversion culture.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
18 /// <returns>A string representation of the provided date and time.</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
19 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
20 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
21 string dateTimeString = string.Empty;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
22 DateTime inputDateTime = DateTime.MinValue;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
23
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
24 if (value != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
25 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
26 if (DateTime.TryParse(value.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDateTime))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
27 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
28 var diff = DateTime.Now - inputDateTime;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
29 if (diff.TotalDays >= ( 1.0 - .5/24 ))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
30 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
31 if (diff.TotalDays > 1.25)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
32 dateTimeString = string.Format("{0} days ago", RoundToHalf(diff.TotalDays));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
33 else
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
34 dateTimeString = "1 day ago";
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
35 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
36 else if (diff.TotalHours > 8)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
37 dateTimeString = string.Format("{0} hours ago", RoundToHalf(diff.TotalHours));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
38 else if (diff.TotalHours > 1)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
39 dateTimeString = string.Format("{0} hours {1} minutes ago", diff.Hours, diff.Minutes);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
40 else
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
41 dateTimeString = string.Format("{0} minutes ago", diff.Minutes);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
42 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
43 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
44
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
45 return dateTimeString;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
46 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
47
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
48 private double RoundToHalf(double p)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
49 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
50 int doubleValue = (int)( (p+.25) * 2 );
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
51 return (double)doubleValue / 2.0;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
52 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
53
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
54 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
55 /// Converts a date and time string into a DateTime object. Not implemented.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
56 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
57 /// <param name="value">The string to convert.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
58 /// <param name="targetType">The target type of the conversion.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
59 /// <param name="parameter">The conversion parameter.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
60 /// <param name="culture">The conversion culture.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
61 /// <returns>A DateTime object of the provided string. Not implemented.</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
62 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
63 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
64 throw new NotImplementedException();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
65 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
66
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
67 #endregion
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
68 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
69
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
70 }