andrew4582

Time

Oct 1st, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4. The goal of this class is to provide an easy interface for a web
  5. developer to make a web application that uses timezones that are
  6. user specific that might be different from the server. The server
  7. might not even be sitting at UTC.
  8. */
  9. public static class Time {
  10.  
  11.     // The user's timezone
  12.     public static string LocalTimeZoneId {
  13.         get {
  14.             var timeZone = RequestData.GetValue<string>("LocalTimeZone");
  15.             if (timeZone == null) {
  16.                 throw new InvalidOperationException("The Request Local Time Zone has not been set");
  17.             }
  18.  
  19.             return timeZone;
  20.         }
  21.         set {
  22.             // If an invalid id is passed, it will throw an InvalidOperationException
  23.             RequestData.SetValue("LocalTimeZone", value);
  24.         }
  25.     }
  26.  
  27.     public static DateTime NowInLocal {
  28.         get {
  29.             return UtcToLocal(DateTime.UtcNow);
  30.         }
  31.     }
  32.  
  33.     public static DateTime LocalToUtc(DateTime localTime) {
  34.         return TimeZoneInfo.ConvertTimeToUtc(localTime, TimeZoneInfo.FindSystemTimeZoneById(LocalTimeZoneId));
  35.     }
  36.  
  37.     public static DateTime UtcToLocal(DateTime utcTime) {
  38.         return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.FindSystemTimeZoneById(LocalTimeZoneId));
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment