Пример #1
0
        public void TestTimeZone_ShortTimeFormat()
        {
            TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Pacific Standard Time");

            CultureInfo culture = new CultureInfo("en-US");

            var formatter = new NumberDateFormat(DateFormat.LONG, DateFormat.SHORT, culture) // Short time = no time zone info
            {
                TimeZone = timeZone
            };

            // Convert from Unix epoch to time zone.
            DateTime dateToParse = TimeZoneInfo.ConvertTimeFromUtc(J2N.Time.UnixEpoch, timeZone);

            // Get the difference since the Unix epoch in milliseconds.
            long dateAsLong = dateToParse.GetMillisecondsSinceUnixEpoch();

            string actual = formatter.Format(dateAsLong);

            Console.WriteLine("Output of formatter.Format():");
            Console.WriteLine($"\"{actual}\"");


            // Convert the parsed result back to a long
            long parsedLong = Convert.ToInt64(formatter.Parse(actual));

            // Make sure round trip results in the same number
            Assert.AreEqual(dateAsLong, parsedLong);
        }
Пример #2
0
        public void TestTimeZone_ShortTimeFormat_CentralAfricaTime()
        {
            TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Africa/Gaborone");

            CultureInfo culture = new CultureInfo("en-US");

            var formatter = new NumberDateFormat(DateFormat.LONG, DateFormat.MEDIUM, culture) // Medium time = no time zone info, but contains seconds
            {
                TimeZone = timeZone
            };

            const long Oct_31_1871_Ticks = 590376582130000000L; // Oct 31, 1871 05:30:13 UTC

            DateTime dateToParse = TimeZoneInfo.ConvertTime(new DateTime(Oct_31_1871_Ticks, DateTimeKind.Utc), timeZone);

            // Get the difference since the Unix epoch in milliseconds.
            long dateAsLong = dateToParse.GetMillisecondsSinceUnixEpoch();

            string actual = formatter.Format(dateAsLong);

            Console.WriteLine("Output of formatter.Format():");
            Console.WriteLine($"\"{actual}\"");

            // Convert the parsed result back to a long
            long parsedLong = Convert.ToInt64(formatter.Parse(actual));

            // Make sure round trip results in the same number
            Assert.AreEqual(dateAsLong, parsedLong);
        }
Пример #3
0
        public void TestTimeZone_PacificTime()
        {
            TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Pacific Standard Time");

            CultureInfo culture = new CultureInfo("en-US");

            var formatter = new NumberDateFormat(DateFormat.LONG, DateFormat.LONG, culture)
            {
                TimeZone = timeZone
            };

            // Convert from Unix epoch to time zone.
            DateTime dateToParse = TimeZoneInfo.ConvertTimeFromUtc(J2N.Time.UnixEpoch, timeZone);

            // Get the difference since the Unix epoch in milliseconds.
            long dateAsLong = dateToParse.GetMillisecondsSinceUnixEpoch();

            string actual = formatter.Format(dateAsLong);

            Console.WriteLine("Output of formatter.Format():");
            Console.WriteLine($"\"{actual}\"");


            // Make sure time zone is correct in the string for PST, including DST.
            if (timeZone.IsDaylightSavingTime(dateToParse))
            {
                Assert.IsTrue(Regex.IsMatch(actual, @"\-\s?0?7"));
            }
            else
            {
                Assert.IsTrue(Regex.IsMatch(actual, @"\-\s?0?8"));
            }

            // Convert the parsed result back to a long
            long parsedLong = Convert.ToInt64(formatter.Parse(actual));

            // Make sure round trip results in the same number
            Assert.AreEqual(dateAsLong, parsedLong);
        }