示例#1
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);
        }