Skip to content

Instantly share code, notes, and snippets.

@bradmb
Created July 8, 2014 21:22
Show Gist options
  • Save bradmb/e33b56b01626f68f5b52 to your computer and use it in GitHub Desktop.
Save bradmb/e33b56b01626f68f5b52 to your computer and use it in GitHub Desktop.
Time Zone Helper Class
using System;
using System.Collections.Generic;
using System.Linq;
namespace Snippet.Helpers.Timezone
{
/// <summary>
/// Class TimeZoneHelper.
/// </summary>
public static class TimeZoneHelper
{
/// <summary>
/// Outputs a list of all the supported time zones
/// </summary>
/// <returns>IEnumerable&lt;TimeZoneItem&gt;.</returns>
public static IEnumerable<TimeZoneItem> List()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
if (tz.IsDaylightSavingTime(DateTime.Now))
{
var timeData = new List<TimeZoneItem>();
timeData.Add(new TimeZoneItem { Name = "(UTC-07:00) Pacific Time", Id = "Pacific Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-07:00) Arizona", Id = "US Mountain Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-06:00) Mountain Time", Id = "Mountain Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-05:00) Central Time", Id = "Central Standard Time" });
return timeData;
}
else
{
var timeData = new List<TimeZoneItem>();
timeData.Add(new TimeZoneItem { Name = "(UTC-08:00) Pacific Time", Id = "Pacific Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-07:00) Arizona", Id = "US Mountain Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-07:00) Mountain Time", Id = "Mountain Standard Time" });
timeData.Add(new TimeZoneItem { Name = "(UTC-06:00) Central Time", Id = "Central Standard Time" });
return timeData;
}
}
/// <summary>
/// Converts a time from the specified time zone to UTC
/// </summary>
/// <param name="time">The time.</param>
/// <param name="timeZone">The time zone.</param>
/// <returns>DateTime.</returns>
public static DateTime ToUtc(this DateTime time, string timeZone)
{
var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
return TimeZoneInfo.ConvertTimeToUtc(time, tz);
}
/// <summary>
/// Converts a time from UTC to the specified time zone
/// </summary>
/// <param name="time">The time.</param>
/// <param name="timeZone">The time zone.</param>
/// <returns>DateTime.</returns>
public static DateTime FromUtc(this DateTime time, string timeZone)
{
var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
return TimeZoneInfo.ConvertTimeFromUtc(time, tz);
}
/// <summary>
/// Converts a timezone to the display name for the passed time (daylight time or standard time)
/// </summary>
/// <param name="timeZone">The time zone.</param>
/// <param name="date">The date.</param>
/// <returns>System.String.</returns>
public static string ToName(this string timeZone, DateTime date)
{
var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
return tz.IsDaylightSavingTime(date) ? tz.DaylightName : tz.Id;
}
/// <summary>
/// Converts a long timezone to it's abbreviation
/// </summary>
/// <param name="timeZone">The time zone.</param>
/// <param name="date">The date.</param>
/// <returns>System.String.</returns>
public static string ToShortTimeZone(this string timeZone, DateTime date)
{
var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
var tz = tzInfo.IsDaylightSavingTime(date) ? tzInfo.DaylightName.Split(' ') : timeZone.Split(' ');
return tz.Aggregate(string.Empty, (current, t) => current + t.Substring(0, 1));
}
}
/// <summary>
/// Class TimeZoneItem.
/// </summary>
public class TimeZoneItem
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>The identifier.</value>
public string Id { get; set; }
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment