comparison Chronosv2/source/Extensions/DateTimeOffsetExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2
3 namespace Chronos.Extensions
4 {
5 /// <summary>
6 /// Extension methods for the DateTimeOffset data type.
7 /// </summary>
8 public static class DateTimeOffsetExtensions
9 {
10 #region · Extensions ·
11
12 /// <summary>
13 /// Indicates whether the date is today.
14 /// </summary>
15 /// <param name="dto">The date.</param>
16 /// <returns>
17 /// <c>true</c> if the specified date is today; otherwise, <c>false</c>.
18 /// </returns>
19 public static bool IsToday(this DateTimeOffset dto)
20 {
21 return (dto.Date.IsToday());
22 }
23
24 /// <summary>
25 /// Sets the time on the specified DateTimeOffset value using the local system time zone.
26 /// </summary>
27 /// <param name="date">The base date.</param>
28 /// <param name="hours">The hours to be set.</param>
29 /// <param name="minutes">The minutes to be set.</param>
30 /// <param name="seconds">The seconds to be set.</param>
31 /// <returns>The DateTimeOffset including the new time value</returns>
32 public static DateTimeOffset SetTime(this DateTimeOffset date, int hours, int minutes, int seconds)
33 {
34 return date.SetTime(new TimeSpan(hours, minutes, seconds));
35 }
36
37 /// <summary>
38 /// Sets the time on the specified DateTime value using the local system time zone.
39 /// </summary>
40 /// <param name="date">The base date.</param>
41 /// <param name="time">The TimeSpan to be applied.</param>
42 /// <returns>
43 /// The DateTimeOffset including the new time value
44 /// </returns>
45 public static DateTimeOffset SetTime(this DateTimeOffset date, TimeSpan time)
46 {
47 return date.SetTime(time, null);
48 }
49
50 /// <summary>
51 /// Sets the time on the specified DateTime value using the specified time zone.
52 /// </summary>
53 /// <param name="date">The base date.</param>
54 /// <param name="time">The TimeSpan to be applied.</param>
55 /// <param name="localTimeZone">The local time zone.</param>
56 /// <returns>/// The DateTimeOffset including the new time value/// </returns>
57 public static DateTimeOffset SetTime(this DateTimeOffset date, TimeSpan time, TimeZoneInfo localTimeZone)
58 {
59 var localDate = date.ToLocalDateTime(localTimeZone);
60
61 localDate.SetTime(time);
62
63 return localDate.ToDateTimeOffset(localTimeZone);
64 }
65
66 /// <summary>
67 /// Converts a DateTimeOffset into a DateTime using the local system time zone.
68 /// </summary>
69 /// <param name="dateTimeUtc">The base DateTimeOffset.</param>
70 /// <returns>The converted DateTime</returns>
71 public static DateTime ToLocalDateTime(this DateTimeOffset dateTimeUtc)
72 {
73 return dateTimeUtc.ToLocalDateTime(null);
74 }
75
76 /// <summary>
77 /// Converts a DateTimeOffset into a DateTime using the specified time zone.
78 /// </summary>
79 /// <param name="dateTimeUtc">The base DateTimeOffset.</param>
80 /// <param name="localTimeZone">The time zone to be used for conversion.</param>
81 /// <returns>The converted DateTime</returns>
82 public static DateTime ToLocalDateTime(this DateTimeOffset dateTimeUtc, TimeZoneInfo localTimeZone)
83 {
84 localTimeZone = (localTimeZone ?? TimeZoneInfo.Local);
85
86 return TimeZoneInfo.ConvertTime(dateTimeUtc, localTimeZone).DateTime;
87 }
88
89 #endregion
90 }
91 }