コード例 #1
0
        public bool IsDateTimeAllowed(ZonedDateTime date, TimeRestrictionModel model)
        {
            if (!model.RestrictionsEnabled)
            {
                return(true);
            }

            LocalDateTime d = date.LocalDateTime;

            LocalDateTime startDateLocal, endDateLocal;

            int hour, minute;

            getHoursMinutes(model.EnabledThrough[0], out hour, out minute);
            if (hour == 24)
            {
                startDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999);
            }
            else
            {
                startDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, hour, minute);
            }

            getHoursMinutes(model.EnabledThrough[1], out hour, out minute);
            if (hour == 24)
            {
                endDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999);
            }
            else
            {
                endDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, hour, minute);
            }

            return(d.CompareTo(startDateLocal) >= 0 && d.CompareTo(endDateLocal) <= 0);
        }
コード例 #2
0
        public void CompareTo_ObjectTestStruct_0()
        {
            object other = TestStruct;

            var exp = 0;
            var act = TestStruct.CompareTo(other);

            Assert.AreEqual(exp, act);
        }
コード例 #3
0
        public void CompareTo_SameCalendar()
        {
            LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime value2 = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime value3 = new LocalDateTime(2011, 1, 2, 10, 45);

            Assert.That(value1.CompareTo(value2), Is.EqualTo(0));
            Assert.That(value1.CompareTo(value3), Is.LessThan(0));
            Assert.That(value3.CompareTo(value2), Is.GreaterThan(0));
        }
コード例 #4
0
        public void CompareTo_DifferentCalendars_OnlyLocalInstantMatters()
        {
            CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical);
            LocalDateTime  value1  = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime  value2  = new LocalDateTime(1500, 1, 1, 10, 30, islamic);
            LocalDateTime  value3  = value1.WithCalendar(islamic);

            Assert.That(value1.CompareTo(value2), Is.LessThan(0));
            Assert.That(value2.CompareTo(value1), Is.GreaterThan(0));
            Assert.That(value1.CompareTo(value3), Is.EqualTo(0));
        }
コード例 #5
0
ファイル: ShiftInstance.cs プロジェクト: yoozeer/ShiftSharp
 /// <summary>
 /// Determine if this time falls within the shift instance period
 /// </summary>
 /// <param name="ldt">Date and time to check</param>
 /// <returns>True if the specified time is in this shift instance</returns>
 public Boolean IsInShiftInstance(LocalDateTime ldt)
 {
     if (ldt.CompareTo(StartDateTime) >= 0 && ldt.CompareTo(GetEndTime()) <= 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #6
0
        /// <summary>
        /// Checks if starting date and ending date LocalDateTimes are valid
        /// </summary>
        /// <param name="startingDate">starting date of the time period</param>
        /// <param name="endingDate">ending date of the time period</param>
        private void checkLocalDateTimes(LocalDateTime startingDate, LocalDateTime endingDate)
        {
            if (startingDate.CompareTo(endingDate) == 0)
            {
                throw new ArgumentException(STARTING_DATE_SAME_AS_ENDING_DATE);
            }

            if (startingDate.CompareTo(endingDate) > 0)
            {
                throw new ArgumentException(STARTING_DATE_LATER_THAN_ENDING_DATE);
            }
        }
コード例 #7
0
ファイル: RaidTime.cs プロジェクト: AntiTcb/AlphaBetaBot
        public static RaidTime Parse(string value)
        {
            foreach (string format in Formats)
            {
                var parse = LocalDateTimePattern.CreateWithInvariantCulture(format).Parse(value);

                if (!parse.Success)
                {
                    continue;
                }

                var localDt    = new LocalDateTime(DateTimeOffset.Now.Year, parse.Value.Month, parse.Value.Day, parse.Value.Hour, parse.Value.Minute);
                var nowLocalDt = new LocalDateTime(DateTimeOffset.Now.Year, DateTimeOffset.Now.Month, DateTimeOffset.Now.Day, DateTimeOffset.Now.Hour, DateTimeOffset.Now.Minute);

                if (localDt.CompareTo(nowLocalDt) <= 0)
                {
                    localDt = localDt.PlusYears(1);
                }

                int offset = -8;
                if (TimeZoneInfo.Local.IsDaylightSavingTime(localDt.ToDateTimeUnspecified()))
                {
                    offset = -7;
                }

                var offsetDt = new OffsetDateTime(localDt, Offset.FromHours(offset));

                return(new RaidTime(offsetDt));
            }

            throw new FormatException("Couldn't parse value to any format");
        }
コード例 #8
0
        public void ShouldReportLargerOnCompareToNull()
        {
            var dateTime1 = new LocalDateTime(1947, 12, 17, 0, 0, 0, 0);

            var comp = dateTime1.CompareTo(null);

            comp.Should().BeGreaterThan(0);
        }
コード例 #9
0
        public void ShouldThrowOnCompareToOtherType()
        {
            var dateTime1 = new LocalDateTime(1947, 12, 17, 0, 0, 0, 0);

            var ex = Record.Exception(() => dateTime1.CompareTo(new DateTime(1947, 12, 17)));

            ex.Should().NotBeNull().And.BeOfType <ArgumentException>();
        }
コード例 #10
0
        public void CompareTo_DifferentCalendars_Throws()
        {
            CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical);
            LocalDateTime  value1  = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime  value2  = new LocalDateTime(1500, 1, 1, 10, 30, islamic);

            Assert.Throws <ArgumentException>(() => value1.CompareTo(value2));
            Assert.Throws <ArgumentException>(() => ((IComparable)value1).CompareTo(value2));
        }
コード例 #11
0
        public void ShouldReportSmallerOnCompareTo()
        {
            var dateTime1 = new LocalDateTime(1947, 12, 16, 23, 59, 59, 999999999);
            var dateTime2 = new LocalDateTime(1947, 12, 17, 0, 59, 59, 999999999);

            var comp = dateTime1.CompareTo(dateTime2);

            comp.Should().BeLessThan(0);
        }
コード例 #12
0
        public void ShouldReportEqualOnCompareTo()
        {
            var dateTime1 = new LocalDateTime(1947, 12, 16, 23, 59, 59, 999999999);
            var dateTime2 = new LocalDateTime(1947, 12, 16, 23, 59, 59, 999999999);

            var comp = dateTime1.CompareTo(dateTime2);

            comp.Should().Be(0);
        }
コード例 #13
0
        /// <summary>
        /// Calculate the schedule working time between the specified dates and times
        /// </summary>
        /// <param name="from">Starting date and time</param>
        /// <param name="to">Ending date and time</param>
        /// <returns>Duration</returns>
        public Duration CalculateWorkingTime(LocalDateTime from, LocalDateTime to)
        {
            if (from.CompareTo(to) > 0)
            {
                string msg = string.Format(WorkSchedule.GetMessage("end.earlier.than.start"), to, from);
                throw new Exception(msg);
            }

            Duration sum = Duration.Zero;

            LocalDate thisDate = from.Date;
            LocalTime thisTime = from.TimeOfDay;
            LocalDate toDate   = to.Date;
            LocalTime toTime   = to.TimeOfDay;
            int       dayCount = Rotation.GetDayCount();

            // get the working shift from yesterday
            Shift lastShift = null;

            LocalDate     yesterday         = thisDate.PlusDays(-1);
            ShiftInstance yesterdayInstance = GetShiftInstanceForDay(yesterday);

            if (yesterdayInstance != null)
            {
                lastShift = yesterdayInstance.Shift;
            }

            // step through each day until done
            while (thisDate.CompareTo(toDate) < 1)
            {
                if (lastShift != null && lastShift.SpansMidnight())
                {
                    // check for days in the middle of the time period
                    bool lastDay = thisDate.CompareTo(toDate) == 0 ? true : false;

                    if (!lastDay || (lastDay && !toTime.Equals(LocalTime.Midnight)))
                    {
                        // add time after midnight in this day
                        int afterMidnightSecond = TimePeriod.SecondOfDay(lastShift.GetEnd());
                        int fromSecond          = TimePeriod.SecondOfDay(thisTime);

                        if (afterMidnightSecond > fromSecond)
                        {
                            Duration seconds = Duration.FromSeconds(afterMidnightSecond - fromSecond);
                            sum = sum.Plus(seconds);
                        }
                    }
                }

                // today's shift
                ShiftInstance instance = GetShiftInstanceForDay(thisDate);

                Duration duration;

                if (instance != null)
                {
                    lastShift = instance.Shift;
                    // check for last date
                    if (thisDate.CompareTo(toDate) == 0)
                    {
                        duration = lastShift.CalculateWorkingTime(thisTime, toTime, true);
                    }
                    else
                    {
                        duration = lastShift.CalculateWorkingTime(thisTime, LocalTime.MaxValue, true);
                    }
                    sum = sum.Plus(duration);
                }
                else
                {
                    lastShift = null;
                }

                int n = 1;
                if (GetDayInRotation(thisDate) == dayCount)
                {
                    // move ahead by the rotation count if possible
                    LocalDate rotationEndDate = thisDate.PlusDays(dayCount);

                    if (rotationEndDate.CompareTo(toDate) < 0)
                    {
                        n   = dayCount;
                        sum = sum.Plus(Rotation.GetWorkingTime());
                    }
                }

                // move ahead n days starting at midnight
                thisDate = thisDate.PlusDays(n);
                thisTime = LocalTime.Midnight;
            }             // end day loop

            return(sum);
        }
コード例 #14
0
        public void CompareTo_DifferentCalendars_Throws()
        {
            CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical);
            LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime value2 = new LocalDateTime(1500, 1, 1, 10, 30, islamic);

            Assert.Throws<ArgumentException>(() => value1.CompareTo(value2));
            Assert.Throws<ArgumentException>(() => ((IComparable)value1).CompareTo(value2));
        }
コード例 #15
0
        public void CompareTo_SameCalendar()
        {
            LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime value2 = new LocalDateTime(2011, 1, 2, 10, 30);
            LocalDateTime value3 = new LocalDateTime(2011, 1, 2, 10, 45);

            Assert.That(value1.CompareTo(value2), Is.EqualTo(0));
            Assert.That(value1.CompareTo(value3), Is.LessThan(0));
            Assert.That(value3.CompareTo(value2), Is.GreaterThan(0));
        }