public override bool TryCompare(IDateTimeDiffTO dateTimeDiffTo, out string result, out string error)
        {
            result = "";
            error  = "";
            var dateTimeParser  = DateTimeConverterFactory.CreateStandardParser();
            var parsedCorreclty = DateTime.TryParseExact(dateTimeDiffTo.Input1, dateTimeDiffTo.InputFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate);

            if (parsedCorreclty)
            {
                _input1         = parsedDate;
                parsedCorreclty = DateTime.TryParseExact(dateTimeDiffTo.Input2, dateTimeDiffTo.InputFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate1);


                if (parsedCorreclty)
                {
                    _input2         = parsedDate1;
                    parsedCorreclty = OutputFormats.TryGetValue(dateTimeDiffTo.OutputType, out Func <DateTime, DateTime, double> returnedFunc);

                    if (returnedFunc != null)
                    {
                        var tmpAmount  = returnedFunc.Invoke(_input1, _input2);
                        var wholeValue = Convert.ToInt64(Math.Floor(tmpAmount));
                        result = wholeValue.ToString(CultureInfo.InvariantCulture);
                    }
                }

                return(parsedCorreclty);
            }
            error = ErrorResource.CannorParseInputDateTimeWithGivenFormat;
            return(parsedCorreclty);
        }
        public bool TryCompare(IDateTimeDiffTO dateTimeDiffTo, out string result, out string error)
        {
            //local variable declarations

            result = "";
            //Creation of parser to get the DateTime Objects
            IDateTimeParser   dateTimeParser = DateTimeConverterFactory.CreateParser();
            IDateTimeResultTO tmpRes;

            //try create the first DateTime object
            bool noErrorOccured = dateTimeParser.TryParseDateTime(dateTimeDiffTo.Input1, dateTimeDiffTo.InputFormat,
                                                                  out tmpRes, out error);

            if (noErrorOccured)
            {
                //Set the first DateTime object
                _input1 = tmpRes.ToDateTime();
                //try create the second DateTime object
                noErrorOccured = dateTimeParser.TryParseDateTime(dateTimeDiffTo.Input2, dateTimeDiffTo.InputFormat,
                                                                 out tmpRes, out error);
            }

            if (noErrorOccured)
            {
                //Set the first DateTime object
                _input2 = tmpRes.ToDateTime();

                //Try get the function according to what the OutputType is
                Func <DateTime, DateTime, double> returnedFunc;
                noErrorOccured = OutputFormats.TryGetValue(dateTimeDiffTo.OutputType, out returnedFunc);

                if (returnedFunc != null)
                {
                    //Invoke the function the return the difference
                    double tmpAmount = returnedFunc.Invoke(_input1, _input2);
                    //Splits the double that is returned into a whole number and to a string
                    long wholeValue = Convert.ToInt64(Math.Floor(tmpAmount));
                    result = wholeValue.ToString(CultureInfo.InvariantCulture);
                }
            }
            return(noErrorOccured);
        }
示例#3
0
        public override bool TryFormat(IDateTimeOperationTO dateTimeTO, out string result, out string error)
        {
            result = "";
            var dateTimeParser = DateTimeConverterFactory.CreateParser();
            var nothingDied    = true;

            dateTimeTO.InputFormat = dateTimeTO.InputFormat?.Trim();

            if (dateTimeParser.TryParseDateTime(dateTimeTO.DateTime?.Trim(), dateTimeTO.InputFormat, out IDateTimeResultTO dateTimeResultTO, out error))
            {
                var tmpDateTime = dateTimeResultTO.ToDateTime();
                if (!string.IsNullOrWhiteSpace(dateTimeTO.TimeModifierType) && TimeModifiers.TryGetValue(dateTimeTO.TimeModifierType, out Func <DateTime, int, DateTime> funcToExecute) && funcToExecute != null)
                {
                    tmpDateTime = funcToExecute(tmpDateTime, dateTimeTO.TimeModifierAmount);
                }


                if (nothingDied)
                {
                    result = ApplyDateTimeFormatParts(dateTimeTO, result, out error, dateTimeParser, out nothingDied, dateTimeResultTO, tmpDateTime);
                }
            }
示例#4
0
        /// <summary>
        ///     Converts a date from one format to another. If a valid time modifier is specified then the date is adjusted
        ///     accordingly before being returned.
        /// </summary>
        public bool TryFormat(IDateTimeOperationTO dateTimeTO, out string result, out string error)
        {
            result = "";
            IDateTimeParser dateTimeParser = DateTimeConverterFactory.CreateParser();

            bool nothingDied = true;
            IDateTimeResultTO dateTimeResultTO;

            //2013.05.06: Ashley Lewis - Bug 9300 - trim should allow null input format
            dateTimeTO.InputFormat = dateTimeTO.InputFormat != null?dateTimeTO.InputFormat.Trim() : null;

            //2013.02.12: Ashley Lewis - Bug 8725, Task 8840 - Added trim to data
            if (dateTimeParser.TryParseDateTime(dateTimeTO.DateTime.Trim(), dateTimeTO.InputFormat, out dateTimeResultTO,
                                                out error))
            {
                //
                // Parse time, if present
                //
                DateTime tmpDateTime = dateTimeResultTO.ToDateTime();
                if (!string.IsNullOrWhiteSpace(dateTimeTO.TimeModifierType))
                {
                    //2012.09.27: massimo.guerrera - Added for the new functionality for the time modification
                    Func <DateTime, int, DateTime> funcToExecute;
                    if (TimeModifiers.TryGetValue(dateTimeTO.TimeModifierType, out funcToExecute) &&
                        funcToExecute != null)
                    {
                        tmpDateTime = funcToExecute(tmpDateTime, dateTimeTO.TimeModifierAmount);
                    }
                }

                //
                // If nothing has gone wrong yet
                //
                // ReSharper disable ConditionIsAlwaysTrueOrFalse
                if (nothingDied)
                // ReSharper restore ConditionIsAlwaysTrueOrFalse
                {
                    //
                    // If there is no output format use the input format
                    //
                    string outputFormat = string.IsNullOrWhiteSpace(dateTimeTO.OutputFormat)
                        ? dateTimeTO.InputFormat
                        : dateTimeTO.OutputFormat;
                    if (string.IsNullOrWhiteSpace(outputFormat))
                    {
                        //07.03.2013: Ashley Lewis - Bug 9167 null to default


                        string shortPattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
                        string longPattern  = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
                        string finalPattern = shortPattern + " " + longPattern;
                        if (finalPattern.Contains("ss"))
                        {
                            outputFormat = finalPattern.Insert(finalPattern.IndexOf("ss", StringComparison.Ordinal) + 2, ".fff");
                            outputFormat = dateTimeParser.TranslateDotNetToDev2Format(outputFormat, out error);
                        }
                    }

                    //
                    // Format to output format
                    //
                    List <IDateTimeFormatPartTO> formatParts;

                    //
                    // Get output format parts
                    //
                    nothingDied = DateTimeParser.TryGetDateTimeFormatParts(outputFormat, out formatParts, out error);

                    if (nothingDied)
                    {
                        int count = 0;
                        while (count < formatParts.Count && nothingDied)
                        {
                            IDateTimeFormatPartTO formatPart = formatParts[count];

                            if (formatPart.Isliteral)
                            {
                                result += formatPart.Value;
                            }
                            else
                            {
                                Func <IDateTimeResultTO, DateTime, string> func;
                                if (DateTimeFormatParts.TryGetValue(formatPart.Value, out func))
                                {
                                    result += func(dateTimeResultTO, tmpDateTime);
                                }
                                else
                                {
                                    nothingDied = false;
                                    error       = string.Concat("Unrecognized format part '", formatPart.Value, "'.");
                                }
                            }

                            count++;
                        }
                    }
                }
            }
            else
            {
                nothingDied = false;
            }

            return(nothingDied);
        }