/// <summary>
        /// Returns the difference between two date/time values, measured in the specified interval.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="interval">The interval in which the difference is measured.</param>
        /// <returns></returns>
        public static Int64?DateDiff(object startDate, object endDate, string interval)
        {
            var startDto = GetDateTimeOffsetFromInputParameter(startDate, null);
            var endDto   = GetDateTimeOffsetFromInputParameter(endDate, null);

            if (startDto != null && endDto != null)
            {
                var difference = endDto.Value - startDto.Value;

                switch (interval)
                {
                case "d":
                    return(( Int64 )difference.TotalDays);

                case "h":
                    return(( Int64 )difference.TotalHours);

                case "m":
                    return(( Int64 )difference.TotalMinutes);

                case "M":
                    return(( Int64 )GetMonthsBetween(startDto.Value, endDto.Value));

                case "Y":
                    // Return the difference between the dates as the number of whole years.
                    var years = LavaDateTime.ConvertToRockDateTime(endDto.Value).TotalYears(LavaDateTime.ConvertToRockDateTime(startDto.Value));
                    return(years);

                case "s":
                    return(( Int64 )difference.TotalSeconds);

                default:
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        public void EventScheduledInstanceCommand_ForSampleDataKnownEvents_ReturnsExpectedEventData()
        {
            var input = @"
{% eventscheduledinstance eventid:'Customs & Classics Car Show' startdate:'2018-4-1' maxoccurrences:1 %}
    {% for item in EventScheduledInstances %}
        Name={{ item.Name }}<br>
        Date={{item.Date }}<br>
        Time={{ item.Time }}<br>
        DateTime={{ item.DateTime | Date:'yyyy-MM-ddTHH:mm:sszzz' }}
    {% endfor %}
{% endeventscheduledinstance %}
";

            var rockTimeOffset = LavaDateTime.ConvertToRockDateTime(new DateTime(2021, 9, 1, 0, 0, 0, DateTimeKind.Unspecified)).ToString("zzz");

            var expectedOutput = @"
Name=Customs & Classics Car Show<br>Date=16/04/2018<br>Time=9:24 AM<br>DateTime=2018-04-16T09:24:13<offset>
";

            expectedOutput = expectedOutput.Replace("<offset>", rockTimeOffset);

            TestHelper.AssertTemplateOutput(expectedOutput, input);
        }
        public void CalendarEventsCommand_ForSampleDataKnownEvents_ReturnsExpectedEventData()
        {
            var input          = @"
{% calendarevents calendarid:'Internal' startdate:'2021-1-1' maxoccurrences:2 %}
    {% for item in EventScheduledInstances %}
        Name={{ item.Name }}<br>
        Date={{item.Date }}<br>
        Time={{ item.Time }}<br>
        DateTime={{ item.DateTime | Date:'yyyy-MM-ddTHH:mm:sszzz' }}
        <hr>
    {% endfor %}
{% endcalendarevents %}
";
            var rockTimeOffset = LavaDateTime.ConvertToRockDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)).ToString("zzz");

            var expectedOutput = @"
Name=Rock Solid Finances Class<br>Date=2/01/2021<br>Time=4:30 PM<br>DateTime=2021-01-02T16:30:00<offset><hr>
Name=Rock Solid Finances Class<br>Date=3/01/2021<br>Time=12:00 PM<br>DateTime=2021-01-03T12:00:00<offset><hr>
";

            expectedOutput = expectedOutput.Replace("<offset>", rockTimeOffset);

            TestHelper.AssertTemplateOutput(expectedOutput, input);
        }