public void PlusSeconds_Simple()
        {
            LocalDateTime start            = new LocalDateTime(2011, 4, 2, 12, 15, 8);
            LocalDateTime expectedForward  = new LocalDateTime(2011, 4, 2, 12, 15, 18);
            LocalDateTime expectedBackward = new LocalDateTime(2011, 4, 2, 12, 14, 58);

            Assert.AreEqual(expectedForward, start.PlusSeconds(10));
            Assert.AreEqual(expectedBackward, start.PlusSeconds(-10));
        }
Ejemplo n.º 2
0
        public IEnumerable <Tuple <LocalDate, Duration> > GetDurationForEachDate(ZonedDateTime first, ZonedDateTime last)
        {
            var returnList = new List <Tuple <LocalDate, Duration> >();
            var firstKey   = new LocalDate(first.Year, first.Month, first.Day);
            var lastKey    = new LocalDate(last.Year, last.Month, last.Day);

            var localFirst = first.LocalDateTime;
            var localLast  = last.LocalDateTime;

            LocalDateTime endOfFirst      = new LocalDateTime(first.Year, first.Month, first.Day, 23, 59, 59);
            LocalDateTime beginningOfLast = new LocalDateTime(last.Year, last.Month, last.Day, 0, 0, 0);

            returnList.Add(new Tuple <LocalDate, Duration>(firstKey, endOfFirst.Minus(localFirst).ToDuration()));

            var dateIterator = endOfFirst.PlusSeconds(1);

            HashSet <LocalDate> datesSeen = new HashSet <LocalDate>();

            datesSeen.Add(firstKey);

            while (dateIterator < beginningOfLast)
            {
                var date = new LocalDate(dateIterator.Year, dateIterator.Month, dateIterator.Day);
                datesSeen.Add(date);

                returnList.Add(new Tuple <LocalDate, Duration>(date, Duration.FromDays(1)));
                dateIterator = dateIterator.PlusHours(24);
            }

            returnList.Add(new Tuple <LocalDate, Duration>(lastKey, localLast.Minus(beginningOfLast).ToDuration()));

            return(returnList);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the start or end date time in a time zone for the specified date range type.
        /// </summary>
        /// <param name="nowInTimeZone">Now in time zone</param>
        /// <param name="dateRangeType">The date range type (today, current week etc.)</param>
        /// <param name="isStart">True for start and false for end of date range</param>
        /// <param name="useEndOfCurrentDay">Flag to indicate if end of current periods are now or the end of the current day</param>
        /// <returns>The start or end date time for the range in the time zone</returns>
        public static LocalDateTime LocalDateTimeForDateRangeType(this LocalDateTime nowInTimeZone, DateRangeType dateRangeType, bool isStart, bool useEndOfCurrentDay = false)
        {
            LocalDateTime startToday = nowInTimeZone.PlusTicks(-nowInTimeZone.TickOfDay);
            LocalDateTime endToday   = startToday.PlusDays(1).PlusSeconds(-1);
            var           currentEnd = useEndOfCurrentDay ? endToday : nowInTimeZone;

            var startThisWeek  = startToday.With(DateAdjusters.PreviousOrSame(IsoDayOfWeek.Monday));
            var startThisMonth = startToday.With(DateAdjusters.StartOfMonth);

            LocalDateTime dateTimeInTimeZone = nowInTimeZone;

            switch (dateRangeType)
            {
            case DateRangeType.Today:
                dateTimeInTimeZone = isStart ? startToday : currentEnd;
                break;

            case DateRangeType.Yesterday:
            case DateRangeType.PriorToYesterday:
                dateTimeInTimeZone = isStart ? startToday.PlusDays(-1) : startToday.PlusSeconds(-1);
                if (dateRangeType == DateRangeType.PriorToYesterday)
                {
                    dateTimeInTimeZone = dateTimeInTimeZone.PlusDays(-1);
                }
                break;

            case DateRangeType.CurrentWeek:
                dateTimeInTimeZone = isStart ? startThisWeek : currentEnd;
                break;

            case DateRangeType.PreviousWeek:
            case DateRangeType.PriorToPreviousWeek:
                dateTimeInTimeZone = isStart ? startThisWeek.PlusDays(-7) : startThisWeek.PlusSeconds(-1);
                if (dateRangeType == DateRangeType.PriorToPreviousWeek)
                {
                    dateTimeInTimeZone = dateTimeInTimeZone.PlusDays(-7);
                }
                break;

            case DateRangeType.CurrentMonth:
                dateTimeInTimeZone = isStart ? startThisMonth : currentEnd;
                break;

            case DateRangeType.PreviousMonth:
            case DateRangeType.PriorToPreviousMonth:
                dateTimeInTimeZone = isStart ? startThisMonth.PlusMonths(-1) : startThisMonth.PlusSeconds(-1);
                if (dateRangeType == DateRangeType.PriorToPreviousMonth)
                {
                    dateTimeInTimeZone = dateTimeInTimeZone.PlusMonths(-1);
                }
                break;

            case DateRangeType.ProjectExtents:
            case DateRangeType.Custom:
                //do nothing
                break;
            }
            return(dateTimeInTimeZone);
        }
Ejemplo n.º 4
0
        public void Addition_Duration()
        {
            const int  minutes      = 23;
            const int  hours        = 3;
            const int  milliseconds = 40000;
            const long seconds      = 321;
            const long nanoseconds  = 12345;
            const long ticks        = 5432112345;

            // Test all three approaches... not bothering to check a different calendar,
            // but we'll use two different offsets.
            OffsetDateTime start    = new LocalDateTime(2014, 08, 14, 6, 51).WithOffset(Offset.FromHours(1));
            Duration       duration = Duration.FromHours(8) + Duration.FromMinutes(9);
            OffsetDateTime expected = new LocalDateTime(2014, 08, 14, 15, 0).WithOffset(Offset.FromHours(1));

            Assert.AreEqual(expected, start + duration);
            Assert.AreEqual(expected, start.Plus(duration));
            Assert.AreEqual(expected, OffsetDateTime.Add(start, duration));

            Assert.AreEqual(start + Duration.FromHours(hours), start.PlusHours(hours));
            Assert.AreEqual(start + Duration.FromHours(-hours), start.PlusHours(-hours));

            Assert.AreEqual(start + Duration.FromMinutes(minutes), start.PlusMinutes(minutes));
            Assert.AreEqual(start + Duration.FromMinutes(-minutes), start.PlusMinutes(-minutes));

            Assert.AreEqual(start + Duration.FromSeconds(seconds), start.PlusSeconds(seconds));
            Assert.AreEqual(start + Duration.FromSeconds(-seconds), start.PlusSeconds(-seconds));

            Assert.AreEqual(start + Duration.FromMilliseconds(milliseconds), start.PlusMilliseconds(milliseconds));
            Assert.AreEqual(start + Duration.FromMilliseconds(-milliseconds), start.PlusMilliseconds(-milliseconds));

            Assert.AreEqual(start + Duration.FromTicks(ticks), start.PlusTicks(ticks));
            Assert.AreEqual(start + Duration.FromTicks(-ticks), start.PlusTicks(-ticks));

            Assert.AreEqual(start + Duration.FromNanoseconds(nanoseconds), start.PlusNanoseconds(nanoseconds));
            Assert.AreEqual(start + Duration.FromNanoseconds(-nanoseconds), start.PlusNanoseconds(-nanoseconds));
        }
Ejemplo n.º 5
0
        public void CuriousSubtraction()
        {
            var shanghai      = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
            var localBefore   = new LocalDateTime(1927, 12, 31, 23, 54, 02);
            var localAfter    = localBefore.PlusSeconds(1);
            var instantBefore = localBefore.InZoneLeniently(shanghai).ToInstant();
            var instantAfter  = localAfter.InZoneLeniently(shanghai).ToInstant();

            Assert.AreEqual(Duration.FromSeconds(358), instantAfter - instantBefore);

            // Now let's resolve them differently...
            var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, Resolvers.ThrowWhenSkipped);

            instantBefore = localBefore.InZone(shanghai, resolver).ToInstant();
            instantAfter  = localAfter.InZone(shanghai, resolver).ToInstant();
            Assert.AreEqual(Duration.FromSeconds(1), instantAfter - instantBefore);
        }
Ejemplo n.º 6
0
        public void CuriousSubtraction()
        {
            var shanghai = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
            var localBefore = new LocalDateTime(1900, 12, 31, 23, 54, 16);
            var localAfter = localBefore.PlusSeconds(1);

            // Note: The behavior of NodaTime's Lenient resolver changed in 2.0, which deviates from the problem described in the original post.
            var oldLenientResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter);
            var instantBefore = localBefore.InZone(shanghai, oldLenientResolver).ToInstant();
            var instantAfter = localAfter.InZone(shanghai, oldLenientResolver).ToInstant();

            Assert.AreEqual(Duration.FromSeconds(344), instantAfter - instantBefore);

            // Now let's resolve them differently...
            var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, Resolvers.ThrowWhenSkipped);
            instantBefore = localBefore.InZone(shanghai, resolver).ToInstant();
            instantAfter = localAfter.InZone(shanghai, resolver).ToInstant();
            Assert.AreEqual(Duration.FromSeconds(1), instantAfter - instantBefore);
        }
Ejemplo n.º 7
0
        public void CuriousSubtraction()
        {
            var shanghai    = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
            var localBefore = new LocalDateTime(1900, 12, 31, 23, 54, 16);
            var localAfter  = localBefore.PlusSeconds(1);

            // Note: The behavior of NodaTime's Lenient resolver changed in 2.0, which deviates from the problem described in the original post.
            var oldLenientResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter);
            var instantBefore      = localBefore.InZone(shanghai, oldLenientResolver).ToInstant();
            var instantAfter       = localAfter.InZone(shanghai, oldLenientResolver).ToInstant();

            Assert.AreEqual(Duration.FromSeconds(344), instantAfter - instantBefore);

            // Now let's resolve them differently...
            var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, Resolvers.ThrowWhenSkipped);

            instantBefore = localBefore.InZone(shanghai, resolver).ToInstant();
            instantAfter  = localAfter.InZone(shanghai, resolver).ToInstant();
            Assert.AreEqual(Duration.FromSeconds(1), instantAfter - instantBefore);
        }
Ejemplo n.º 8
0
        public static string getWaitingTimeMessage(double x, double y)
        {
            if (x != null && x != 0)
            {
                double duration = (y / x) * 24 * 60 * 60;

                LocalDateTime d1      = new LocalDateTime();
                LocalDateTime d2      = d1.PlusSeconds((long)duration);
                Period        period  = Period.Between(d1, d2);
                int           years   = period.Years;
                int           months  = period.Months;
                int           days    = period.Days;
                long          hours   = period.Hours;
                long          minutes = period.Minutes;
                long          seconds = period.Seconds;
                string        result  = "Your average waiting time is " + (years > 0 ? years + " years " : "") +
                                        (months > 0 ? months + " months " : "") +
                                        (days > 0 ? days + " days " : "");
                //(hours > 0 ? hours + " hours " : "") +
                // (minutes > 0 ? minutes + " minutes " : "");
                return(result);
            }
            return("");
        }
Ejemplo n.º 9
0
 public void PlusSeconds()
 {
     SampleStartDateTime.PlusSeconds(3);
 }
Ejemplo n.º 10
0
 public LocalDateTime PlusSeconds() => Sample.PlusSeconds(3);
 public void PlusSeconds()
 {
     Sample.PlusSeconds(3).Consume();
 }
Ejemplo n.º 12
0
        public void Addition_Duration()
        {
            const int minutes = 23;
            const int hours = 3;
            const int milliseconds = 40000;
            const long seconds = 321;
            const long nanoseconds = 12345;
            const long ticks = 5432112345;

            // Test all three approaches... not bothering to check a different calendar,
            // but we'll use two different offsets.
            OffsetDateTime start = new LocalDateTime(2014, 08, 14, 6, 51).WithOffset(Offset.FromHours(1));
            Duration duration = Duration.FromHours(8) + Duration.FromMinutes(9);
            OffsetDateTime expected = new LocalDateTime(2014, 08, 14, 15, 0).WithOffset(Offset.FromHours(1));
            Assert.AreEqual(expected, start + duration);
            Assert.AreEqual(expected, start.Plus(duration));
            Assert.AreEqual(expected, OffsetDateTime.Add(start, duration));

            Assert.AreEqual(start + Duration.FromHours(hours), start.PlusHours(hours));
            Assert.AreEqual(start + Duration.FromHours(-hours), start.PlusHours(-hours));

            Assert.AreEqual(start + Duration.FromMinutes(minutes), start.PlusMinutes(minutes));
            Assert.AreEqual(start + Duration.FromMinutes(-minutes), start.PlusMinutes(-minutes));

            Assert.AreEqual(start + Duration.FromSeconds(seconds), start.PlusSeconds(seconds));
            Assert.AreEqual(start + Duration.FromSeconds(-seconds), start.PlusSeconds(-seconds));

            Assert.AreEqual(start + Duration.FromMilliseconds(milliseconds), start.PlusMilliseconds(milliseconds));
            Assert.AreEqual(start + Duration.FromMilliseconds(-milliseconds), start.PlusMilliseconds(-milliseconds));

            Assert.AreEqual(start + Duration.FromTicks(ticks), start.PlusTicks(ticks));
            Assert.AreEqual(start + Duration.FromTicks(-ticks), start.PlusTicks(-ticks));

            Assert.AreEqual(start + Duration.FromNanoseconds(nanoseconds), start.PlusNanoseconds(nanoseconds));
            Assert.AreEqual(start + Duration.FromNanoseconds(-nanoseconds), start.PlusNanoseconds(-nanoseconds));
        }
 public void PlusSeconds_Simple()
 {
     LocalDateTime start = new LocalDateTime(2011, 4, 2, 12, 15, 8);
     LocalDateTime expectedForward = new LocalDateTime(2011, 4, 2, 12, 15, 18);
     LocalDateTime expectedBackward = new LocalDateTime(2011, 4, 2, 12, 14, 58);
     Assert.AreEqual(expectedForward, start.PlusSeconds(10));
     Assert.AreEqual(expectedBackward, start.PlusSeconds(-10));
 }
Ejemplo n.º 14
0
 public LocalDateTime PlusSeconds()
 {
     return(Sample.PlusSeconds(3));
 }
Ejemplo n.º 15
0
 public IDateTime PlusSeconds(int seconds)
 {
     return(new DateTime(_localDateTime.PlusSeconds(seconds)));
 }
 /// <summary>
 /// Add seconds
 /// </summary>
 /// <param name="lt"></param>
 /// <param name="seconds"></param>
 /// <returns></returns>
 public static LocalDateTime AddSeconds(this LocalDateTime lt, long seconds) => lt.PlusSeconds(seconds);