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