Mercurial > silverbladetech
annotate MetroWpf/MetroWpf.Framework/Extensions/DateTimeOffsetExtensions.cs @ 65:5d0c5cc83a7a
Click event handler added
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sun, 22 Apr 2012 14:21:33 +0100 |
parents | 060f02cd4591 |
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 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
3 namespace MetroWpf |
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 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
6 /// Extension methods for the DateTimeOffset data type. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
7 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
8 public static class DateTimeOffsetExtensions |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
9 { |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
10 #region · Extensions · |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
11 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
12 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
13 /// Indicates whether the date is today. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
14 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
15 /// <param name="dto">The date.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
16 /// <returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
17 /// <c>true</c> if the specified date is today; otherwise, <c>false</c>. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
18 /// </returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
19 public static bool IsToday(this DateTimeOffset dto) |
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 return (dto.Date.IsToday()); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
22 } |
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 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
25 /// Sets the time on the specified DateTimeOffset value using the local system time zone. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
26 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
27 /// <param name="date">The base date.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
28 /// <param name="hours">The hours to be set.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
29 /// <param name="minutes">The minutes to be set.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
30 /// <param name="seconds">The seconds to be set.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
31 /// <returns>The DateTimeOffset including the new time value</returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
32 public static DateTimeOffset SetTime(this DateTimeOffset date, int hours, int minutes, int seconds) |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
33 { |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
34 return date.SetTime(new TimeSpan(hours, minutes, seconds)); |
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 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
37 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
38 /// Sets the time on the specified DateTime value using the local system time zone. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
39 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
40 /// <param name="date">The base date.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
41 /// <param name="time">The TimeSpan to be applied.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
42 /// <returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
43 /// The DateTimeOffset including the new time value |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
44 /// </returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
45 public static DateTimeOffset SetTime(this DateTimeOffset date, TimeSpan time) |
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 return date.SetTime(time, null); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
48 } |
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 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
51 /// Sets the time on the specified DateTime value using the specified time zone. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
52 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
53 /// <param name="date">The base date.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
54 /// <param name="time">The TimeSpan to be applied.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
55 /// <param name="localTimeZone">The local time zone.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
56 /// <returns>/// The DateTimeOffset including the new time value/// </returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
57 public static DateTimeOffset SetTime(this DateTimeOffset date, TimeSpan time, TimeZoneInfo localTimeZone) |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
58 { |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
59 var localDate = date.ToLocalDateTime(localTimeZone); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
60 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
61 localDate.SetTime(time); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
62 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
63 return localDate.ToDateTimeOffset(localTimeZone); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
64 } |
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 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
67 /// Converts a DateTimeOffset into a DateTime using the local system time zone. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
68 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
69 /// <param name="dateTimeUtc">The base DateTimeOffset.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
70 /// <returns>The converted DateTime</returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
71 public static DateTime ToLocalDateTime(this DateTimeOffset dateTimeUtc) |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
72 { |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
73 return dateTimeUtc.ToLocalDateTime(null); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
74 } |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
75 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
76 /// <summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
77 /// Converts a DateTimeOffset into a DateTime using the specified time zone. |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
78 /// </summary> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
79 /// <param name="dateTimeUtc">The base DateTimeOffset.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
80 /// <param name="localTimeZone">The time zone to be used for conversion.</param> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
81 /// <returns>The converted DateTime</returns> |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
82 public static DateTime ToLocalDateTime(this DateTimeOffset dateTimeUtc, TimeZoneInfo localTimeZone) |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
83 { |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
84 localTimeZone = (localTimeZone ?? TimeZoneInfo.Local); |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
85 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
86 return TimeZoneInfo.ConvertTime(dateTimeUtc, localTimeZone).DateTime; |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
87 } |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
88 |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
89 #endregion |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
90 } |
060f02cd4591
Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff
changeset
|
91 } |