Esempio n. 1
0
        // https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-text
        public static FormulaValue Text(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args)
        {
            // only DateValue and DateTimeValue are supported for now with custom format strings.
            if (args.Length > 1 && args[0] is StringValue)
            {
                return(CommonErrors.NotYetImplementedError(irContext, "Text() doesn't support format args for type StringValue"));
            }

            string resultString = null;
            string formatString = null;

            if (args.Length > 1 && args[1] is StringValue fs)
            {
                formatString = fs.Value;
            }


            CultureInfo suppliedCulture = null;

            if (args.Length > 2 && args[2] is StringValue locale)
            {
                suppliedCulture = new CultureInfo(locale.Value);
            }

            switch (args[0])
            {
            case NumberValue num:
                resultString = num.Value.ToString(formatString ?? "g", suppliedCulture ?? runner.CultureInfo);
                break;

            case StringValue s:
                resultString = s.Value;
                break;

            case DateValue d:
                formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo);
                resultString = d.Value.ToString(formatString ?? "M/d/yyyy", suppliedCulture ?? runner.CultureInfo);
                break;

            case DateTimeValue dt:
                formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo);
                resultString = dt.Value.ToString(formatString ?? "g", suppliedCulture ?? runner.CultureInfo);
                break;

            case TimeValue t:
                formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo);
                resultString = _epoch.Add(t.Value).ToString(formatString ?? "t", suppliedCulture ?? runner.CultureInfo);
                break;

            default:
                break;
            }

            if (resultString != null)
            {
                return(new StringValue(irContext, resultString));
            }

            return(CommonErrors.NotYetImplementedError(irContext, $"Text format for {args[0]?.GetType().Name}"));
        }
Esempio n. 2
0
        // https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/show-text-dates-times
        // https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-dateadd-datediff
        public static FormulaValue DateAdd(IRContext irContext, FormulaValue[] args)
        {
            DateTime datetime;

            switch (args[0])
            {
            case DateTimeValue dtv:
                datetime = dtv.Value;
                break;

            case DateValue dv:
                datetime = dv.Value;
                break;

            default:
                return(CommonErrors.RuntimeTypeMismatch(irContext));
            }

            var delta = (NumberValue)args[1];
            var units = (StringValue)args[2];

            try
            {
                DateTime newDate;
                switch (units.Value.ToLower())
                {
                case "milliseconds":
                    newDate = datetime.AddMilliseconds(delta.Value);
                    break;

                case "seconds":
                    newDate = datetime.AddSeconds(delta.Value);
                    break;

                case "minutes":
                    newDate = datetime.AddMinutes(delta.Value);
                    break;

                case "hours":
                    newDate = datetime.AddHours(delta.Value);
                    break;

                case "days":
                    newDate = datetime.AddDays(delta.Value);
                    break;

                case "months":
                    newDate = datetime.AddMonths((int)delta.Value);
                    break;

                case "quarters":
                    newDate = datetime.AddMonths((int)delta.Value * 3);
                    break;

                case "years":
                    newDate = datetime.AddYears((int)delta.Value);
                    break;

                default:
                    // TODO: Task 10723372: Implement Unit Functionality in DateAdd, DateDiff Functions
                    return(CommonErrors.NotYetImplementedError(irContext, "DateAdd Only supports Days for the unit field"));
                }

                if (args[0] is DateTimeValue)
                {
                    return(new DateTimeValue(irContext, newDate));
                }
                else
                {
                    return(new DateValue(irContext, newDate.Date));
                }
            }
            catch
            {
                return(CommonErrors.ArgumentOutOfRange(irContext));
            }
        }
Esempio n. 3
0
        public static FormulaValue DateDiff(IRContext irContext, FormulaValue[] args)
        {
            DateTime start;

            switch (args[0])
            {
            case DateTimeValue dtv:
                start = dtv.Value;
                break;

            case DateValue dv:
                start = dv.Value;
                break;

            default:
                return(CommonErrors.RuntimeTypeMismatch(irContext));
            }

            DateTime end;

            switch (args[1])
            {
            case DateTimeValue dtv:
                end = dtv.Value;
                break;

            case DateValue dv:
                end = dv.Value;
                break;

            default:
                return(CommonErrors.RuntimeTypeMismatch(irContext));
            }

            var units = (StringValue)args[2];

            TimeSpan diff = end - start;

            // The function DateDiff only returns a whole number of the units being subtracted, and the precision is given in the unit specified.
            switch (units.Value.ToLower())
            {
            case "milliseconds":
                double milliseconds = Math.Floor(diff.TotalMilliseconds);
                return(new NumberValue(irContext, milliseconds));

            case "seconds":
                double seconds = Math.Floor(diff.TotalSeconds);
                return(new NumberValue(irContext, seconds));

            case "minutes":
                double minutes = Math.Floor(diff.TotalMinutes);
                return(new NumberValue(irContext, minutes));

            case "hours":
                double hours = Math.Floor(diff.TotalHours);
                return(new NumberValue(irContext, hours));

            case "days":
                double days = Math.Floor(diff.TotalDays);
                return(new NumberValue(irContext, days));

            case "months":
                double months = (end.Year - start.Year) * 12 + end.Month - start.Month;
                return(new NumberValue(irContext, months));

            case "quarters":
                double quarters = (end.Year - start.Year) * 4 + Math.Floor(end.Month / 3.0) - Math.Floor(start.Month / 3.0);
                return(new NumberValue(irContext, quarters));

            case "years":
                double years = end.Year - start.Year;
                return(new NumberValue(irContext, years));

            default:
                // TODO: Task 10723372: Implement Unit Functionality in DateAdd, DateDiff Functions
                return(CommonErrors.NotYetImplementedError(irContext, "DateDiff Only supports Days for the unit field"));
            }
        }