コード例 #1
0
        private static void TestDaylightChangesYear(TzTimeZone zone, int year, int expectedStartMonth, int expectedStartDay, int expectedStartYear, int expectedEndMonth, int expectedEndDay, int expectedEndYear)
        {
            DaylightTime daylight = zone.GetDaylightChanges(year);
            Console.WriteLine(daylight.Start);
            Console.WriteLine(daylight.End);

            AssertEqualDaylightPosition(daylight.Start, expectedStartMonth, expectedStartDay, expectedStartYear);
            AssertEqualDaylightPosition(daylight.End, expectedEndMonth, expectedEndDay, expectedEndYear);
        }
コード例 #2
0
 private static void TestToLocalTime(TzTimeZone zone)
 {
     Console.WriteLine("--------- Converting UTC to local time -------------");
     Console.WriteLine("DST on rule <= 2006 for America/Chicago: Apr Sun>=1 2:00 (2am (8:00 UTC) becomes 3am)");
     Console.WriteLine("zone.ToLocalTime('2006-04-02 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 07:00")) + "'");
     Assert.AreEqual(1, zone.ToLocalTime(new DateTime(DateTime.Parse("2006-04-02 06:00").Ticks, DateTimeKind.Utc)).Hour);
     Assert.AreEqual(2, zone.ToLocalTime(new DateTime(DateTime.Parse("2006-04-02 07:00").Ticks, DateTimeKind.Utc)).Hour);
     Console.WriteLine("zone.ToLocalTime('2006-04-02 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 07:30")) + "'");
     Assert.AreEqual(2, zone.ToLocalTime(new DateTime(DateTime.Parse("2006-04-02 07:30").Ticks, DateTimeKind.Utc)).Hour);
     Console.WriteLine("zone.ToLocalTime('2006-04-02 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 08:00")) + "'");
     Assert.AreEqual(3, zone.ToLocalTime(new DateTime(DateTime.Parse("2006-04-02 08:00").Ticks, DateTimeKind.Utc)).Hour);
     Console.WriteLine("zone.ToLocalTime('2006-04-02 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 08:30")) + "'");
     Assert.AreEqual(3, zone.ToLocalTime(new DateTime(DateTime.Parse("2006-04-02 08:30").Ticks, DateTimeKind.Utc)).Hour);
     Console.WriteLine("zone.ToLocalTime('2006-04-02 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-04-02 09:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 09:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-04-02 10:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-04-02 10:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-05-10 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-05-10 03:00")) + "'");
     Console.WriteLine("DST off rule <= 2006 for America/Chicago: Nov Sun>=1 2:00 (2am (7:00 UTC) becomes 1am)");
     Console.WriteLine("zone.ToLocalTime('2006-10-29 06:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-10-29 06:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-10-29 06:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-10-29 06:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-11-04 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-11-04 07:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-11-04 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-11-04 07:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-11-04 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-11-04 08:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-11-04 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2006-11-04 08:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-11-04 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-11-04 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2006-12-12 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2006-12-12 03:00")) + "'");
     Console.WriteLine("DST on rule >= 2007 for America/Chicago: Mar Sun>=8 2:00 (2am (8:00 UTC) becomes 3am)");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 07:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 07:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 08:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 08:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 09:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 09:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-03-11 10:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-03-11 10:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-05-10 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-05-10 03:00")) + "'");
     Console.WriteLine("DST off rule >= 2007 for America/Chicago: Nov Sun>=1 2:00 (2am (7:00 UTC) becomes 1am)");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 06:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 06:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 06:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 06:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 07:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 07:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 08:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 08:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-11-04 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-11-04 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2007-12-12 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2007-12-12 03:00")) + "'");
     Console.WriteLine("DST on rule >= 2007 for America/Chicago: Mar Sun>=8 2:00 (2am (8:00 UTC) becomes 3am)");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 07:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 07:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 08:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 08:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 09:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 09:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-03-09 10:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-03-09 10:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-05-10 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-05-10 03:00")) + "'");
     Console.WriteLine("DST off rule >= 2007 for America/Chicago: Nov Sun>=1 2:00 (2am (7:00 UTC) becomes 1am)");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 06:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 06:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 06:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 06:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 07:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 07:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 07:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 07:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 08:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 08:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 08:30'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 08:30")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-11-02 09:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-11-02 09:00")) + "'");
     Console.WriteLine("zone.ToLocalTime('2008-12-12 03:00'): '" + zone.ToLocalTime(DateTime.Parse("2008-12-12 03:00")) + "'");
 }
コード例 #3
0
 private static void AssertIsDaylightSavingsTime(TzTimeZone zone, string dt, bool expectIsDaylightSavingsTime)
 {
     Assert.AreEqual(expectIsDaylightSavingsTime, zone.IsDaylightSavingTime(DateTime.Parse(dt)));
 }
コード例 #4
0
        private static void CheckToUniversalTime(TzTimeZone zone, string year)
        {
            // 2007-3-11 @ 2am
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-10")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-11")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-12")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-11 01:00")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-11 01:59")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-11 02:00")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-03-11 02:01")));
            Console.WriteLine("DST on rule >= " + year + " for America/Chicago: Mar Sun>=8 2:00 (2am (8:00 UTC) becomes 3am)");
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 01:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 01:00")) + "'");
            Assert.AreEqual(7, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 01:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 01:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 01:30")) + "'");
            Assert.AreEqual(7, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 01:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 02:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 02:00")) + "'");
            Assert.AreEqual(7, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 02:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 02:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 02:30")) + "'");
            Assert.AreEqual(7, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 02:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 03:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 03:00")) + "'");
            Assert.AreEqual(8, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 03:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 03:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 03:30")) + "'");
            Assert.AreEqual(8, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 03:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-03-11 04:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-03-11 04:00")) + "'");
            Assert.AreEqual(9, zone.ToUniversalTime(DateTime.Parse(year + "-03-11 04:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-05-10 03:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-05-10 03:00")) + "'");
            Assert.AreEqual(8, zone.ToUniversalTime(DateTime.Parse(year + "-05-11 03:00")).Hour);

            // 2007-11-4 @ 2 am
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-5")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4 01:00")));
            Assert.IsTrue(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4 01:59")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4 02:00")));
            Assert.IsFalse(zone.IsDaylightSavingTime(DateTime.Parse(year + "-11-4 02:01")));
            Console.WriteLine("DST off rule >= " + year + " for America/Chicago: Nov Sun>=1 2:00 (2am (7:00 UTC) becomes 1am)");
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 00:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 00:00")) + "'");
            Assert.AreEqual(5, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 00:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 01:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 01:00")) + "'");
            Assert.AreEqual(6, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 01:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 01:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 01:30")) + "'");
            Assert.AreEqual(6, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 01:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 02:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 02:00")) + "'");
            Assert.AreEqual(8, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 02:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 02:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 02:30")) + "'");
            Assert.AreEqual(8, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 02:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 03:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 03:00")) + "'");
            Assert.AreEqual(9, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 03:00")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-11-04 03:30'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-11-04 03:30")) + "'");
            Assert.AreEqual(9, zone.ToUniversalTime(DateTime.Parse(year + "-11-04 03:30")).Hour);
            Console.WriteLine("zone.ToUniversalTime('" + year + "-12-12 03:00'): '" + zone.ToUniversalTime(DateTime.Parse(year + "-12-12 03:00")) + "'");
            Assert.AreEqual(9, zone.ToUniversalTime(DateTime.Parse(year + "-12-04 03:00")).Hour);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: kiszu/ForBlog
 /// <summary>
 /// Converts a datetime from fromTimeZone to UTC
 /// </summary>
 private static DateTime ConvertToUtc(DateTime datetime, TzTimeZone fromTimeZone)
 {
     return fromTimeZone.ToUniversalTime(datetime);
 }
コード例 #6
0
ファイル: Iso8601.cs プロジェクト: Yitzchok/PublicDomain
 /// <summary>
 /// Parses the specified STR.
 /// </summary>
 /// <param name="str">The STR.</param>
 /// <param name="localTimeZone">The local time zone.</param>
 /// <returns></returns>
 public static TzDateTime Parse(string str, TzTimeZone localTimeZone)
 {
     TzDateTime result;
     if (!DoParse(str, localTimeZone, out result))
     {
         ThrowInvalidFormatException(str);
     }
     return result;
 }
コード例 #7
0
ファイル: Iso8601.cs プロジェクト: Yitzchok/PublicDomain
        private static bool DoParse(string str, TzTimeZone localTimeZone, out TzDateTime result)
        {
            result = null;

            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentNullException("str");
            }

            Match m;
            int matchIndex = RegexUtilities.MatchAny(str, out m,
                FormatYear,
                FormatYearAndMonth,
                FormatComplete,
                FormatCompleteHM,
                FormatCompleteHMS,
                FormatCompleteHMSF);
            int year = 0, month = 1, day = 1, hours = 0, minutes = 0, seconds = 0, millisecond = 0;
            DateTimeKind kind = DateTimeKind.Local;
            TzTimeZone timezone = localTimeZone;

            if (matchIndex != -1)
            {
                if (matchIndex >= 0)
                {
                    year = int.Parse(RegexUtilities.GetCapture(m, 1));
                }
                if (matchIndex >= 1)
                {
                    month = int.Parse(RegexUtilities.GetCapture(m, 2));
                }
                if (matchIndex >= 2)
                {
                    day = int.Parse(RegexUtilities.GetCapture(m, 3));
                }
                if (matchIndex >= 3)
                {
                    hours = int.Parse(RegexUtilities.GetCapture(m, 4));
                    minutes = int.Parse(RegexUtilities.GetCapture(m, 5));

                    // At this level, we also expect a time zone designator
                    kind = DateTimeKind.Utc;
                    string tzd = RegexUtilities.GetLastCapture(m, 2);
                    if (tzd == UtcZuluIdentifier.ToString())
                    {
                        timezone = TzTimeZone.ZoneUTC;
                    }
                    else
                    {
                        timezone = TzTimeZone.GetTimeZoneByOffset(RegexUtilities.GetLastCapture(m, 1));
                    }
                }
                if (matchIndex >= 4)
                {
                    seconds = int.Parse(RegexUtilities.GetCapture(m, 6));
                }
                if (matchIndex >= 5)
                {
                    millisecond = int.Parse(RegexUtilities.GetCapture(m, 7));
                }
                result = new TzDateTime(year, month, day, hours, minutes, seconds, millisecond, kind, timezone);
                return true;
            }
            return false;
        }
コード例 #8
0
ファイル: Iso8601.cs プロジェクト: Yitzchok/PublicDomain
 /// <summary>
 /// Tries the parse.
 /// </summary>
 /// <param name="str">The STR.</param>
 /// <param name="localTimeZone">The local time zone.</param>
 /// <param name="dateTime">The date time.</param>
 /// <returns></returns>
 public static bool TryParse(string str, TzTimeZone localTimeZone, out TzDateTime dateTime)
 {
     return DoParse(str, localTimeZone, out dateTime);
 }
コード例 #9
0
ファイル: TzTimeZone.cs プロジェクト: numerodix/solarbeam
        /// <summary>
        /// Gets a new object representing the time zone with the
        /// name <paramref name="tzName"/> or null if none can be found.
        /// These <see cref="PublicDomain.TzTimeZone"/> instances are
        /// not cached, so constantly calling this method will continuously
        /// create new <see cref="PublicDomain.TzTimeZone"/> objects.
        /// </summary>
        /// <param name="tzName">Name of the time zone.</param>
        /// <returns>null if no time zone can by found or a TzTimeZone object</returns>
        public static TzTimeZone GetTimeZone(string tzName)
        {
            TzTimeZone result = null;
            TzZoneInfo zoneInfo;

            if (!string.IsNullOrEmpty(tzName))
            {
                try
                {
                    s_zonesLock.AcquireReaderLock(-1);

                    if (Zones.TryGetValue(tzName, out zoneInfo))
                    {
                        result = new TzTimeZone(zoneInfo);
                    }
                    else
                    {
                        LockCookie writerCookie = s_zonesLock.UpgradeToWriterLock(-1);

                        try
                        {
                            if (Zones.TryGetValue(tzName, out zoneInfo))
                            {
                                result = new TzTimeZone(zoneInfo);
                            }
                            else
                            {

                                if (zoneInfo != null)
                                {
                                    // Found the time zone
                                    result = new TzTimeZone(zoneInfo);
                                    s_zones[tzName] = zoneInfo;
                                }
                            }
                        }
                        finally
                        {
                            s_zonesLock.DowngradeFromWriterLock(ref writerCookie);
                        }
                    }
                }
                finally
                {
                    s_zonesLock.ReleaseReaderLock();
                }
            }

            return result;
        }