Exemplo n.º 1
0
        private static (string, string) EvalConvertFromUTC(object utcTimestamp, string timezone, string format, CultureInfo locale)
        {
            string error             = null;
            string result            = null;
            var    utcDt             = DateTime.UtcNow;
            object parsed            = null;
            object convertedTimeZone = null;

            (parsed, error) = FunctionUtils.NormalizeToDateTime(utcTimestamp);
            if (error == null)
            {
                utcDt = ((DateTime)parsed).ToUniversalTime();
            }

            if (error == null)
            {
                (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);

                if (error == null)
                {
                    var convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDt, (TimeZoneInfo)convertedTimeZone);
                    (result, error) = FunctionUtils.ReturnFormatTimeStampStr(convertedDateTime, format, locale);
                }
            }

            return(result, error);
        }
Exemplo n.º 2
0
        private static (string, string) EvalConvertToUTC(object sourceTimestamp, string sourceTimezone, string format)
        {
            string error  = null;
            string result = null;
            var    srcDt  = DateTime.UtcNow;

            try
            {
                if (sourceTimestamp is string st)
                {
                    srcDt = DateTime.Parse(st);
                }
                else
                {
                    srcDt = (DateTime)sourceTimestamp;
                }
            }
            catch
            {
                error = $"illegal time-stamp representation {sourceTimestamp}";
            }

            if (error == null)
            {
                object convertedTimeZone;
                (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(sourceTimezone);
                if (error == null)
                {
                    var convertedDateTime = TimeZoneInfo.ConvertTimeToUtc(srcDt, (TimeZoneInfo)convertedTimeZone);
                    (result, error) = FunctionUtils.ReturnFormatTimeStampStr(convertedDateTime, format);
                }
            }

            return(result, error);
        }
Exemplo n.º 3
0
        private static (string, string) EvalConvertToUtc(object sourceTimestamp, string sourceTimezone, string format)
        {
            string error  = null;
            string result = null;
            var    srcDt  = DateTime.UtcNow;

            try
            {
                if (sourceTimestamp is string st)
                {
                    srcDt = DateTime.Parse(st, CultureInfo.InvariantCulture);
                }
                else
                {
                    srcDt = (DateTime)sourceTimestamp;
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types (we should probably do something about this but ignoring it for not)
            catch
#pragma warning restore CA1031 // Do not catch general exception types
            {
                error = $"illegal time-stamp representation {sourceTimestamp}";
            }

            if (error == null)
            {
                object convertedTimeZone;
                (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(sourceTimezone);
                if (error == null)
                {
                    var convertedDateTime = TimeZoneInfo.ConvertTimeToUtc(srcDt, (TimeZoneInfo)convertedTimeZone);
                    (result, error) = FunctionUtils.ReturnFormatTimeStampStr(convertedDateTime, format);
                }
            }

            return(result, error);
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty parsed = null;
            string        result = null;
            string        error  = null;

            var(validYear, validMonth, validDay) = (0, 0, 0);
            var currentUtcTime    = DateTime.UtcNow;
            var convertedDateTime = currentUtcTime;
            IReadOnlyList <object> args;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty(args[0]);
            }

            if (error == null)
            {
                if (parsed.Year != null || parsed.Month == null || parsed.DayOfMonth == null)
                {
                    error = $"{args[0]} must be a timex string which only contains month and day-of-month, for example: 'XXXX-10-31'.";
                }
            }

            if (error == null)
            {
                if (args.Count == 2 && args[1] is string timezone)
                {
                    object convertedTimeZone = null;
                    (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);
                    if (error == null)
                    {
                        convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, (TimeZoneInfo)convertedTimeZone);
                    }
                }
                else
                {
                    convertedDateTime = currentUtcTime.ToLocalTime();
                }
            }

            if (error == null)
            {
                var(year, month, day) = (convertedDateTime.Year, convertedDateTime.Month, convertedDateTime.Day);
                if (parsed.Month <= month || (parsed.Month == month && parsed.DayOfMonth < day))
                {
                    validYear = year;
                }
                else
                {
                    validYear = year - 1;
                }

                validMonth = parsed.Month ?? 0;
                validDay   = parsed.DayOfMonth ?? 0;
                if (validMonth == 2 && validDay == 29)
                {
                    while (!DateTime.IsLeapYear(validYear))
                    {
                        validYear -= 1;
                    }
                }

                result = TimexProperty.FromDate(new DateTime(validYear, validMonth, validDay)).TimexValue;
            }

            return(result, error);
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty parsed = null;
            string        result = null;
            string        error  = null;

            var(validHour, validMinute, validSecond) = (0, 0, 0);
            IReadOnlyList <object> args;
            var formatRegex       = new Regex("TXX:[0-5][0-9]:[0-5][0-9]");
            var currentUtcTime    = DateTime.UtcNow;
            var convertedDateTime = currentUtcTime;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                if (!formatRegex.IsMatch(args[0] as string))
                {
                    error = $"{args[0]}  must be a timex string which only contains minutes and seconds, for example: 'TXX:15:28'";
                }
            }

            if (error == null)
            {
                if (args.Count == 2 && args[1] is string timezone)
                {
                    object convertedTimeZone = null;
                    (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);
                    if (error == null)
                    {
                        convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, (TimeZoneInfo)convertedTimeZone);
                    }
                }
                else
                {
                    convertedDateTime = currentUtcTime.ToLocalTime();
                }
            }

            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty((args[0] as string).Replace("XX", "00"));
            }

            if (error == null)
            {
                var(hour, minute, second) = (convertedDateTime.Hour, convertedDateTime.Minute, convertedDateTime.Second);
                if (parsed.Minute < minute || (parsed.Minute == minute && parsed.Second < second))
                {
                    validHour = hour;
                }
                else
                {
                    validHour = hour - 1;
                }

                if (validHour < 0)
                {
                    validHour += 24;
                }

                validMinute = parsed.Minute ?? 0;
                validSecond = parsed.Second ?? 0;
                result      = TimexProperty.FromTime(new Time(validHour, validMinute, validSecond)).TimexValue;
            }

            return(result, error);
        }