Ejemplo n.º 1
0
        public void Construction()
        {
            ZonedDateTime dt = Dublin.AtStrictly(new LocalDateTime(2010, 6, 9, 15, 15, 0));

            Assert.AreEqual(15, dt.Hour);
            Assert.AreEqual(2010, dt.Year);

            Instant instant = Instant.FromUtc(2010, 6, 9, 14, 15, 0);

            Assert.AreEqual(instant, dt.ToInstant());
        }
Ejemplo n.º 2
0
        public void Construction()
        {
            ZonedDateTime dt = Dublin.AtStrictly(new LocalDateTime(2010, 6, 9, 15, 15, 0));

            Assert.AreEqual(15, dt.Hour);
            Assert.AreEqual(2010, dt.Year);
            // Not 21... we're not in the Gregorian calendar!
            Assert.AreEqual(20, dt.CenturyOfEra);

            Instant instant = Instant.FromUtc(2010, 6, 9, 14, 15, 0);

            Assert.AreEqual(instant, dt.ToInstant());
        }
        public void AtStrictly_InWinter()
        {
            var when = Pacific.AtStrictly(new LocalDateTime(2009, 12, 22, 21, 39, 30));

            Assert.AreEqual(2009, when.Year);
            Assert.AreEqual(12, when.Month);
            Assert.AreEqual(22, when.Day);
            Assert.AreEqual(IsoDayOfWeek.Tuesday, when.DayOfWeek);
            Assert.AreEqual(21, when.Hour);
            Assert.AreEqual(39, when.Minute);
            Assert.AreEqual(30, when.Second);
            Assert.AreEqual(Offset.FromHours(-8), when.Offset);
        }
Ejemplo n.º 4
0
        public void Kiritimati()
        {
            DateTimeZone kiritimati = DateTimeZoneProviders.Tzdb["Pacific/Kiritimati"];
            var          offset     = kiritimati.GetUtcOffset(kiritimati.AtStrictly(new LocalDateTime(2010, 1, 1, 0, 0, 0)).ToInstant());

            Assert.AreEqual(Offset.FromHours(14), offset);
        }
Ejemplo n.º 5
0
        public void Pyongyang()
        {
            DateTimeZone pyongyang = DateTimeZoneProviders.Tzdb["Asia/Pyongyang"];
            var          offset    = pyongyang.GetUtcOffset(pyongyang.AtStrictly(new LocalDateTime(2010, 1, 1, 0, 0, 0)).ToInstant());

            Assert.AreEqual(Offset.FromHours(9), offset);
        }
Ejemplo n.º 6
0
        public void Niue()
        {
            DateTimeZone niue   = DateTimeZoneProviders.Tzdb["Pacific/Niue"];
            var          offset = niue.GetUtcOffset(niue.AtStrictly(new LocalDateTime(2010, 1, 1, 0, 0, 0)).ToInstant());

            Assert.AreEqual(Offset.FromHours(-11), offset);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Convert a given datetime from a timezone to another timezone
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static ZonedDateTime ConvertDateTimeToDifferentTimeZone(DateTime datetime, DateTimeZone from, DateTimeZone to)
        {
            var           fromLocal    = LocalDateTime.FromDateTime(datetime);
            ZonedDateTime fromDatetime = from.AtStrictly(fromLocal);

            return(fromDatetime.WithZone(to));
        }
        public void AtStrictly_InWinter()
        {
            var          when         = Pacific.AtStrictly(new LocalDateTime(2009, 12, 22, 21, 39, 30));
            Instant      instant      = when.ToInstant();
            LocalInstant localInstant = when.LocalInstant;

            Assert.AreEqual(instant, localInstant.Minus(Offset.FromHours(-8)));

            Assert.AreEqual(2009, when.Year);
            Assert.AreEqual(12, when.Month);
            Assert.AreEqual(22, when.Day);
            Assert.AreEqual(2, when.DayOfWeek);
            Assert.AreEqual(21, when.Hour);
            Assert.AreEqual(39, when.Minute);
            Assert.AreEqual(30, when.Second);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns true if the entry is in the categoryName specified.
        /// </summary>
        /// <param name="entry">The entry to check for category</param>
        /// <param name="timeZone">The time zone.</param>
        /// <param name="dateTime">The latest date and time the entry can occur.</param>
        /// <returns>
        /// A value of true indicates the entry is in the category specified or, if the categoryName
        /// is null or empty, return true if the entry is assigned no categories
        /// </returns>
        public static bool OccursBefore(Entry entry, DateTimeZone timeZone, DateTime dateTime)
        {
            var entryCreated = timeZone.AtStrictly(LocalDateTime.FromDateTime(entry.CreatedUtc)).LocalDateTime;
            var date         = LocalDateTime.FromDateTime(dateTime);

            return(entryCreated <= date);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Converts the specified time from the <paramref name="from"/> time zone to <see cref="TimeZones.Utc"/>
 /// </summary>
 /// <param name="time">The time to be converted in terms of the <paramref name="from"/> time zone</param>
 /// <param name="from">The time zone the specified <paramref name="time"/> is in</param>
 /// <param name="strict">True for strict conversion, this will throw during ambiguitities, false for lenient conversion</param>
 /// <returns>The time in terms of the to time zone</returns>
 public static DateTime ConvertToUtc(this DateTime time, DateTimeZone from, bool strict = false)
 {
     if (strict)
     {
         return(from.AtStrictly(LocalDateTime.FromDateTime(time)).ToDateTimeUtc());
     }
     return(from.AtLeniently(LocalDateTime.FromDateTime(time)).ToDateTimeUtc());
 }
Ejemplo n.º 11
0
        public static string ToString(this Instant instant, string cultureTab, DateTimeZone zone, bool withoutDaylightSaving = false, MxCultureInfo.FormatType formatType = MxCultureInfo.FormatType.DateTime, bool longFormat = false)
        {
            var rc = "[error]";

            if ((zone != null) && (String.IsNullOrEmpty(cultureTab) == false))
            {
                try
                {
                    var culture = MxCultureInfo.Instance.GetCultureInfo(cultureTab);
                    if (culture != null)
                    {
                        var local = instant.InZone(zone).LocalDateTime;
                        if (withoutDaylightSaving && zone.IsDaylightSavingsTime(instant))
                        {
                            local = local.Minus(Period.FromSeconds(zone.GetZoneInterval(instant).Savings.Seconds));
                        }

                        // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                        zone.AtStrictly(local); //throws exception if ambiguous or invalid due to daylight saving transition

                        //In all formats except Date the setting of longFormat (capital letter) causes display of hours, minutes and seconds in 24 hour clock

                        //FormatType.Date:      en-GB: D=Sunday, 29 March 2020 d=29-03-2020
                        //FormatType.Time:      en-GB: T=00:59:59 t=12:59 AM
                        //FormatType.DateTime:  en-GB: G=29-03-2020 00:59:59 g=29-03-2020 12:59 AM
                        //FormatType.Verbose:   en-GB: F=Sunday, 29 March 2020 00:59:59 f=Sunday, 29 March 2020 12:59 AM
                        //FormatType.Machine:   *:     r=2020-03-29T00:59:59.000000000(ISO) s=2020-03-29T00 59:59

                        if (formatType == MxCultureInfo.FormatType.Date)
                        {
                            rc = local.Date.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture);
                        }
                        else if (formatType == MxCultureInfo.FormatType.Time)
                        {
                            rc = local.TimeOfDay.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture);
                        }
                        else
                        {
                            rc = local.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture);
                        }
                    }
                }
                catch (SkippedTimeException)
                {
                    rc = "[error: invalid time]";
                }
                catch (AmbiguousTimeException)
                {
                    rc = "[error: ambiguous time]";
                }
                catch (Exception e)
                {
                    rc = e.Message;
                }
            }
            return(rc);
        }
Ejemplo n.º 12
0
        public static bool OccursBetween(Entry entry, DateTimeZone timeZone,
                                         DateTime startDateTime, DateTime endDateTime)
        {
            var entryCreated = timeZone.AtStrictly(LocalDateTime.FromDateTime(entry.CreatedUtc)).LocalDateTime;
            var strartDate   = LocalDateTime.FromDateTime(startDateTime);
            var endDate      = LocalDateTime.FromDateTime(endDateTime);

            return((entryCreated >= strartDate) && (entryCreated <= endDate));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Converts the specified time from the <paramref name="from"/> time zone to the <paramref name="to"/> time zone
        /// </summary>
        /// <param name="time">The time to be converted in terms of the <paramref name="from"/> time zone</param>
        /// <param name="from">The time zone the specified <paramref name="time"/> is in</param>
        /// <param name="to">The time zone to be converted to</param>
        /// <param name="strict">True for strict conversion, this will throw during ambiguitities, false for lenient conversion</param>
        /// <returns>The time in terms of the to time zone</returns>
        public static DateTime ConvertTo(this DateTime time, DateTimeZone from, DateTimeZone to, bool strict = false)
        {
            if (strict)
            {
                return(from.AtStrictly(LocalDateTime.FromDateTime(time)).WithZone(to).ToDateTimeUnspecified());
            }

            return(from.AtLeniently(LocalDateTime.FromDateTime(time)).WithZone(to).ToDateTimeUnspecified());
        }
        /// <summary>
        /// Apply DateTimeZone strictly
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dateTimeZone"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static DateTimeOffset ApplyDateTimeZoneStrictly(this DateTime dt, DateTimeZone dateTimeZone)
        {
            if (dateTimeZone == null)
            {
                throw new ArgumentNullException(nameof(dateTimeZone));
            }
            var local = dt.ToLocalDateTime();

            return(dateTimeZone.AtStrictly(local).ToDateTimeOffset());
        }
Ejemplo n.º 15
0
        public void InZone()
        {
            DateTimeZone  london     = DateTimeZoneProviders.Tzdb["Europe/London"];
            ZonedDateTime viaInstant = Instant.FromUtc(2008, 6, 10, 13, 16, 17).InZone(london);

            // London is UTC+1 in the Summer, so the above is 14:16:17 local.
            LocalDateTime local    = new LocalDateTime(2008, 6, 10, 14, 16, 17);
            ZonedDateTime expected = london.AtStrictly(local);

            Assert.AreEqual(expected, viaInstant);
        }
Ejemplo n.º 16
0
        public void Construction()
        {
            DateTimeZone  dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
            ZonedDateTime dt     = Snippet.For(dublin.AtStrictly(new LocalDateTime(2010, 6, 9, 15, 15, 0)));

            Assert.AreEqual(15, dt.Hour);
            Assert.AreEqual(2010, dt.Year);

            Instant instant = Instant.FromUtc(2010, 6, 9, 14, 15, 0);

            Assert.AreEqual(instant, dt.ToInstant());
        }
Ejemplo n.º 17
0
        public void InZoneWithCalendar()
        {
            CalendarSystem copticCalendar = CalendarSystem.Coptic;
            DateTimeZone   london         = DateTimeZoneProviders.Tzdb["Europe/London"];
            ZonedDateTime  viaInstant     = Instant.FromUtc(2004, 6, 9, 11, 10).InZone(london, copticCalendar);

            // Date taken from CopticCalendarSystemTest. Time will be 12:10 (London is UTC+1 in Summer)
            LocalDateTime local    = new LocalDateTime(1720, 10, 2, 12, 10, 0, copticCalendar);
            ZonedDateTime expected = london.AtStrictly(local);

            Assert.AreEqual(expected, viaInstant);
        }
Ejemplo n.º 18
0
        public void DSTAutumn()
        {
            DateTimeZone london = DateTimeZoneProviders.Tzdb["Europe/London"];
            // 12:45am on October 29th 2017
            LocalDateTime local  = new LocalDateTime(2017, 10, 29, 0, 50, 00);
            ZonedDateTime before = london.AtStrictly(local) + Duration.FromMinutes(10);
            ZonedDateTime after  = before + Duration.FromMinutes(60);


            Debug.WriteLine(london);
            Debug.WriteLine(before);
            Debug.WriteLine(after);
        }
Ejemplo n.º 19
0
        public void DSTSpring()
        {
            DateTimeZone london = DateTimeZoneProviders.Tzdb["Europe/London"];
            // 12:45am on March 25th 2012
            LocalDateTime local  = new LocalDateTime(2012, 3, 25, 0, 45, 00);
            ZonedDateTime before = london.AtStrictly(local);
            ZonedDateTime after  = before + Duration.FromMinutes(20);


            Debug.WriteLine(london);
            Debug.WriteLine(before);
            Debug.WriteLine(after);
        }
Ejemplo n.º 20
0
        public static DateTimeOffset ApplyDateTimeZone(this DateTime dateTime, DateTimeZone dateTimeZone)
        {
            // Validate parameters.
            if (dateTimeZone == null)
            {
                throw new ArgumentNullException(nameof(dateTimeZone));
            }

            // Create the instance, then apply the date time zone.
            LocalDateTime local = dateTime.ToLocalDateTime();

            // Apply the time zone now.
            return(dateTimeZone.AtStrictly(local).ToDateTimeOffset());
        }
Ejemplo n.º 21
0
        public void TestFromZonedDateTime()
        {
            LocalDateTime dateTime      = new LocalDateTime(2017, 1, 2, 3, 4, 5);
            DateTimeZone  myTz          = DateTimeZoneProviders.Tzdb["America/New_York"];
            ZonedDateTime zonedDateTime = myTz.AtStrictly(dateTime);
            DateTime      dfpDateTime   = DateTimeUtilities.FromDateTime(zonedDateTime);

            Assert.AreEqual(dfpDateTime.date.year, 2017);
            Assert.AreEqual(dfpDateTime.date.month, 1);
            Assert.AreEqual(dfpDateTime.date.day, 2);
            Assert.AreEqual(dfpDateTime.hour, 3);
            Assert.AreEqual(dfpDateTime.minute, 4);
            Assert.AreEqual(dfpDateTime.second, 5);
            Assert.AreEqual(dfpDateTime.timeZoneID, "America/New_York");
        }
 public ZonedDateTime Construction()
 {
     return(Pacific.AtStrictly(SampleLocal));
 }
Ejemplo n.º 23
0
 public void Construction()
 {
     Pacific.AtStrictly(SampleLocal);
 }
Ejemplo n.º 24
0
        public void AmbiguousLocalDateTime()
        {
            DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];

            Assert.Throws <AmbiguousTimeException>(() => dublin.AtStrictly(new LocalDateTime(2010, 10, 31, 1, 15, 0)));
        }
Ejemplo n.º 25
0
 public ZonedDateTime Construction() => Pacific.AtStrictly(SampleLocal);
Ejemplo n.º 26
0
        private async Task <bool> BuildSessionAsync(StringBuilder sb, int id)
        {
            var session = await _sessionRepository.Get(id);

            if (session == null)
            {
                return(false);
            }

            var dateMatch = Regex.Match(session.Name, "(\\d+\\.\\d+.\\d+)");

            if (dateMatch.Length == 0)
            {
                return(false);
            }
            if (!DateTime.TryParseExact(dateMatch.Groups[0].Value, "dd.MM.yyyy",
                                        CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
            {
                return(false);
            }

            var topics = session.Topics.Where(t => t.RoomId != null && t.SlotId != null);

            foreach (var topic in session.Topics)
            {
                var slot = session.Slots.FirstOrDefault(s => s.Id == topic.SlotId);
                var room = session.Rooms.FirstOrDefault(r => r.Id == topic.RoomId);

                if (slot == null || room == null)
                {
                    continue;
                }

                var slotTimes = Regex.Matches(slot.Time, "(\\d+\\:\\d+)");
                if (slotTimes.Count <= 1)
                {
                    continue;
                }

                if (!TimeSpan.TryParse(slotTimes[0].Groups[0].Value, out TimeSpan startTime))
                {
                    continue;
                }
                if (!TimeSpan.TryParse(slotTimes[1].Groups[0].Value, out TimeSpan endTime))
                {
                    continue;
                }

                var startDateTime = new LocalDateTime(date.Year, date.Month, date.Day, startTime.Hours, startTime.Minutes, 0);
                var endDateTime   = new LocalDateTime(date.Year, date.Month, date.Day, endTime.Hours, endTime.Minutes, 0);

                sb.AppendLine("BEGIN:VEVENT");

                sb.AppendLine("DTSTART:" + _timezone.AtStrictly(startDateTime).ToDateTimeUtc().ToString("yyyyMMddTHHmm00Z", null));
                sb.AppendLine("DTEND:" + _timezone.AtStrictly(endDateTime).ToDateTimeUtc().ToString("yyyyMMddTHHmm00Z", null));

                sb.AppendLine("SUMMARY:" + topic.Name + "");
                sb.AppendLine("LOCATION:" + room.Name + "");
                sb.AppendLine("DESCRIPTION:" + topic.Description ?? string.Empty + "");
                sb.AppendLine("BEGIN:VALARM");
                sb.AppendLine("TRIGGER:-PT15M");
                sb.AppendLine("ACTION:DISPLAY");
                sb.AppendLine("DESCRIPTION:Reminder");
                sb.AppendLine("END:VALARM");
                sb.AppendLine("END:VEVENT");
            }

            return(true);
        }
Ejemplo n.º 27
0
        public void SkippedLocalDateTime()
        {
            DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];

            Assert.Throws <SkippedTimeException>(() => dublin.AtStrictly(new LocalDateTime(2010, 3, 28, 1, 15, 0)));
        }
Ejemplo n.º 28
0
 /// <summary>
 /// https://stackoverflow.com/questions/16674008/nodatime-conversions-part-2-how-to
 /// </summary>
 /// <param name="zone"></param>
 /// <param name="localDateTime"></param>
 /// <returns></returns>
 public static DateTime ToDateTimeUtc(this DateTimeZone zone, LocalDateTime localDateTime)
 {
     //for requests
     return(zone.AtStrictly(localDateTime).ToDateTimeUtc());
 }