/// <summary>
        /// Converts an input value to a DateTimeOffset.
        /// </summary>
        /// <param name="input">The date.</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private static DateTimeOffset?GetDateTimeOffsetFromInputParameter(object input, DateTimeOffset?defaultValue)
        {
            if (input is String inputString)
            {
                if (inputString.Trim().ToLower() == "now")
                {
                    return(LavaDateTime.NowOffset);
                }
                else
                {
                    return(LavaDateTime.ParseToOffset(inputString, defaultValue));
                }
            }
            else if (input is DateTime dt)
            {
                return(LavaDateTime.ConvertToDateTimeOffset(dt));
            }
            else if (input is DateTimeOffset inputDateTimeOffset)
            {
                return(inputDateTimeOffset);
            }

            return(defaultValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process the specified input template and verify the output against an expected DateTime result.
        /// </summary>
        /// <param name="expectedDateTime"></param>
        /// <param name="inputTemplate"></param>
        /// <param name="maximumDelta"></param>
        public void AssertTemplateOutputDate(ILavaEngine engine, DateTimeOffset?expectedDateTime, string inputTemplate, TimeSpan?maximumDelta = null)
        {
            var outputString = GetTemplateOutput(engine, inputTemplate);

            var outputDateUtc = LavaDateTime.ParseToOffset(outputString, null);

            WriteOutputToDebug(engine, outputString);

            Assert.That.IsNotNull(outputDateUtc, $"Template Output does not represent a valid DateTime. [Output=\"{ outputString }\"]");

            try
            {
                if (maximumDelta != null)
                {
                    DateTimeAssert.AreEqual(expectedDateTime, outputDateUtc, maximumDelta.Value);
                }
                else
                {
                    DateTimeAssert.AreEqual(expectedDateTime, outputDateUtc);
                }
            }
            catch (Exception ex)
            {
                var info = $@"
Test Environment:
LavaEngine = { engine.EngineName },
LocalDateTime = {DateTimeOffset.Now},
LocalTimeZoneName = {TimeZoneInfo.Local.DisplayName},
LocalTimeZoneOffset = { TimeZoneInfo.Local.BaseUtcOffset }
RockDateTime = { LavaDateTime.NowOffset },
RockTimeZoneName = { RockDateTime.OrgTimeZoneInfo.DisplayName },
RockTimeZoneOffset = { RockDateTime.OrgTimeZoneInfo.BaseUtcOffset }
";
                throw new Exception($"Lava Date/Time test failed.\n{ info }", ex);
            }
        }
        /* [2021-07-31] DL
         *
         * Lava Date filters may return DateTime, DateTimeOffset, or string values according to their purpose.
         * Where possible, a filter should return a DateTime value specified in UTC, or a DateTimeOffset.
         * Local DateTime values may give unexpected results if the Rock timezone setting is different from the server timezone.
         * Where a date string is accepted as an input parameter, the Rock timezone is implied unless a timezone is specified.
         */

        /// <summary>
        /// Formats a date using a .NET date format string
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Date(object input, string format = null)
        {
            if (input == null)
            {
                return(null);
            }

            string output;

            // If input is "now", use the current Rock date/time as the input value.
            if (input.ToString().ToLower() == "now")
            {
                // To correctly include the Rock configured timezone, we need to use a DateTimeOffset.
                // The DateTime object can only represent local server time or UTC time.
                input = LavaDateTime.NowOffset;
            }

            // Use the General Short Date/Long Time format by default.
            if (string.IsNullOrWhiteSpace(format))
            {
                format = "G";
            }
            // Consider special 'Standard Date' and 'Standard Time' formats.
            else if (format == "sd")
            {
                format = "d";
            }
            else if (format == "st")
            {
                format = "t";
            }
            // If the format string is a single character, add a space to produce a valid custom format string.
            // (refer http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#UsingSingleSpecifiers)
            else if (format.Length == 1)
            {
                format = " " + format;
            }

            if (input is DateTimeOffset inputDateTimeOffset)
            {
                // Preserve the value of the specified offset and return the formatted datetime value.
                output = inputDateTimeOffset.ToString(format).Trim();
            }
            else
            {
                // Convert the input to a valid Rock DateTimeOffset if possible.
                DateTimeOffset?inputDateTime;

                if (input is DateTime dt)
                {
                    inputDateTime = LavaDateTime.ConvertToRockOffset(dt);
                }
                else
                {
                    inputDateTime = LavaDateTime.ParseToOffset(input.ToString(), null);
                }

                if (!inputDateTime.HasValue)
                {
                    // Not a valid date, so return the input unformatted.
                    output = input.ToString().Trim();
                }
                else
                {
                    output = LavaDateTime.ToString(inputDateTime.Value, format).Trim();
                }
            }
            return(output);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Process the specified input template and verify the output against an expected DateTime result.
 /// </summary>
 /// <param name="expectedOutput"></param>
 /// <param name="inputTemplate"></param>
 /// <param name="maximumDelta"></param>
 public void AssertTemplateOutputDate(string expectedRockDateTimeOutput, string inputTemplate, TimeSpan?maximumDelta = null)
 {
     AssertTemplateOutputDate(LavaDateTime.ParseToOffset(expectedRockDateTimeOutput), inputTemplate, maximumDelta);
 }