Ejemplo n.º 1
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));
            }
        }
Ejemplo n.º 2
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"));
            }
        }