Last active
August 29, 2015 14:04
-
-
Save benerdin/73c1265cdf0e8a7bc994 to your computer and use it in GitHub Desktop.
TimeZoneConverter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| namespace TimeZones | |
| { | |
| /// <summary> | |
| /// Responsible for converting between different time zones. | |
| /// </summary> | |
| public class TimeZoneConverter | |
| { | |
| /// <summary> | |
| /// Instantiates a converter for the current time zone. | |
| /// </summary> | |
| public TimeZoneConverter() | |
| : this(TimeZone.CurrentTimeZone.StandardName) | |
| { } | |
| /// <summary> | |
| /// Instantiates a converter for the specified <paramref name="baseTimeZoneId"/>. | |
| /// </summary> | |
| /// <param name="baseTimeZoneId">The ID of the base time zone (e.g. "Eastern Stnadard Time").</param> | |
| public TimeZoneConverter(string baseTimeZoneId) | |
| { | |
| // Retrieve the system time zones. | |
| var timeZones = TimeZoneInfo.GetSystemTimeZones(); | |
| // Attempt to set the base time zone. | |
| _baseTimeZone = timeZones.FirstOrDefault( | |
| tz => tz.Id.Equals(baseTimeZoneId, StringComparison.OrdinalIgnoreCase)); | |
| if (_baseTimeZone == null) | |
| { | |
| // Let the caller know that what they did is invalid. | |
| throw new InvalidOperationException( | |
| string.Format("You attempted to construct a TimeZoneConverter with an invalid time zone: {0}", | |
| baseTimeZoneId) | |
| ); | |
| } | |
| // Remember some standard time zones. | |
| _easternStandardTime = timeZones.First( | |
| tz => tz.Id.Equals(EasternStandardTime, StringComparison.OrdinalIgnoreCase)); | |
| _pacificStandardTime = timeZones.First( | |
| tz => tz.Id.Equals(PacificStandardTime, StringComparison.OrdinalIgnoreCase)); | |
| _koreaStandardTime = timeZones.First( | |
| tz => tz.Id.Equals(KoreaStandardTime, StringComparison.OrdinalIgnoreCase)); | |
| } | |
| /// <summary> | |
| /// Convert the given <paramref name="dateTime"/> to Pacific Standard Time. | |
| /// </summary> | |
| /// <param name="dateTime">The DateTime to convert.</param> | |
| /// <returns>A DateTime.</returns> | |
| public DateTime ConvertToPacificStandardTime(DateTime dateTime) | |
| { | |
| return TimeZoneInfo.ConvertTime( | |
| dateTime, | |
| _baseTimeZone, | |
| _pacificStandardTime); | |
| } | |
| /// <summary> | |
| /// Convert the given <paramref name="dateTime"/> to Eastern Standard Time. | |
| /// </summary> | |
| /// <param name="dateTime">The DateTime to convert.</param> | |
| /// <returns>A DateTime.</returns> | |
| public DateTime ConvertToEasternStandardTime(DateTime dateTime) | |
| { | |
| return TimeZoneInfo.ConvertTime( | |
| dateTime, | |
| _baseTimeZone, | |
| _easternStandardTime); | |
| } | |
| /// <summary> | |
| /// Convert the given <paramref name="dateTime"/> to Korea Standard Time. | |
| /// </summary> | |
| /// <param name="dateTime">The DateTime to convert.</param> | |
| /// <returns>A DateTime.</returns> | |
| public DateTime ConvertToKoreaStandardTime(DateTime dateTime) | |
| { | |
| return TimeZoneInfo.ConvertTime( | |
| dateTime, | |
| _baseTimeZone, | |
| _koreaStandardTime); | |
| } | |
| /// <summary> | |
| /// The time zone ID for Easter Standard Time. | |
| /// </summary> | |
| public const string EasternStandardTime = "Eastern Standard Time"; | |
| /// <summary> | |
| /// The time zone ID for Central America Standard Time. | |
| /// </summary> | |
| public const string CentralTime = "Central America Standard Time"; | |
| /// <summary> | |
| /// The time zone ID for Pacific Standard Time. | |
| /// </summary> | |
| public const string PacificStandardTime = "Pacific Standard Time"; | |
| /// <summary> | |
| /// The time zone ID for Korea Standard Time. | |
| /// </summary> | |
| public const string KoreaStandardTime = "Korea Standard Time"; | |
| private readonly TimeZoneInfo _baseTimeZone, | |
| _pacificStandardTime, | |
| _easternStandardTime, | |
| _koreaStandardTime; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using NUnit.Framework; | |
| namespace TimeZones.UnitTests | |
| { | |
| [TestFixture] | |
| public class WhenUsingTimeZoneConverter | |
| { | |
| [Test] | |
| public void EnsureAnInvalidBaseTimeZoneThrowsAnException() | |
| { | |
| Assert.Throws<InvalidOperationException>( | |
| () => new TimeZoneConverter("Mercury"), | |
| "You attempted to construct a TimeZoneConverter with an invalid time zone: Mercury"); | |
| } | |
| [Test] | |
| public void EnsureTimeConvertsToPacificStandardTime() | |
| { | |
| // Arrange | |
| var sut = new TimeZoneConverter(); | |
| var noonEst = new DateTime(2014, 7, 25, 12, 0, 0); | |
| var expected = new DateTime(2014, 7, 25, 9, 0, 0); | |
| // Act | |
| var actual = sut.ConvertToPacificStandardTime(noonEst); | |
| // Assert | |
| Assert.That(actual, Is.EqualTo(expected)); | |
| } | |
| [Test] | |
| public void EnsureConvertsToEasternStandardTime() | |
| { | |
| // Arrange | |
| var sut = new TimeZoneConverter(TimeZoneConverter.PacificStandardTime); | |
| var noonPst = new DateTime(2014, 7, 25, 12, 0, 0); | |
| var expected = new DateTime(2014, 7, 25, 15, 0, 0); | |
| // Act | |
| var actual = sut.ConvertToEasternStandardTime(noonPst); | |
| // Assert | |
| Assert.That(actual, Is.EqualTo(expected)); | |
| } | |
| [Test] | |
| public void EnsureTimeConvertsToKoreanStandardTime() | |
| { | |
| // Arrange | |
| var sut = new TimeZoneConverter(); | |
| var noonEst = new DateTime(2014, 7, 25, 9, 0, 0); | |
| var expected = new DateTime(2014, 7, 25, 22, 0, 0); | |
| // Act | |
| var actual = sut.ConvertToKoreaStandardTime(noonEst); | |
| // Assert | |
| Assert.That(actual, Is.EqualTo(expected)); | |
| } | |
| [Test] | |
| public void EnsureTimeConvertsFromDallasToKoreaStandardTime() | |
| { | |
| // Arrange | |
| var sut = new TimeZoneConverter(TimeZoneConverter.CentralTime); | |
| var noonCentral = new DateTime(2014, 7, 25, 8, 0, 0); | |
| var expected = new DateTime(2014, 7, 25, 23, 0, 0); | |
| // Act | |
| var actual = sut.ConvertToKoreaStandardTime(noonCentral); | |
| // Assert | |
| Assert.That(actual, Is.EqualTo(expected)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment