public static bool IsDate(this string payload)
        {
            bool result = false;

            if (string.IsNullOrEmpty(payload))
            {
                return false;
            }

            var acceptedDateFormats = new List<string>
            {
                "yyyymmdd",
                "mmddyyyy",
                "yyyyddmm",
                "ddmmyyyy",
                "yyyy/mm/dd",
                "dd/mm/yyyy",
                "yyyy/dd/mm",
                "mm/dd/yyyy",
                "yyyy-mm-dd",
                "dd-mm-yyyy",
                "mm-dd-yyyy",
                "yyyy-dd-mm",
                @"dd\mm\yyyy",
                @"yyyy\mm\dd",
                @"yyyy\dd\mm",
                @"mm\dd\yyyy",
                "dd mm yyyy",
                "mm dd yyyy",
                "yyyy mm dd",
                "yyyy dd mm",
                "yyyy mm dd",
                "dd.mm.yyyy",
                "mm.dd.yyyy",
                "yyyy.mm.dd",
                "yyyy.dd.mm"
            };
            var d = new DateTimeParser();
            int count = 0;
            while (result == false && count < acceptedDateFormats.Count)
            {
                string errorMsg;
                IDateTimeResultTO to;
                result = d.TryParseDateTime(payload, acceptedDateFormats[count], out to, out errorMsg);
                count++;
            }
            return result;
        }
Example #2
0
        public void TranslateFormatFromDotNetWhereFormatForFullLongTimeExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "dddd', 'dd MMMM yyyy HH:mm:ss";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "DW', 'dd' 'MM' 'yyyy' '24h':'min':'ss");
        }
Example #3
0
        public void TranslateFormatFromDotNetWhereFormatForEraExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "g, gg";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "g', 'gg");
        }
Example #4
0
        public void TranslateFormatFromDotNetWhereFormatForRoundTripExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "yyyy'-'mm'-'dd'T'24h':'min':'ss'.'fffffffK");
        }
Example #5
0
        public void TranslateFormatFromDotNetWhereFormatForTimeZonesExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "'Hours offset from UTC: 'z', Hours offset from UTC, with a leading zero: 'zz', Offset with minutes: 'zzz";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "'Hours offset from UTC: 'z', Hours offset from UTC, with a leading zero: 'zz', Offset with minutes: 'zzz");
        }
Example #6
0
        public void TranslateFormatFromDotNetWhereFormatForLongTimeExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "HH:mm:ss.fffffff";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "24h':'min':'ss'.'fffffff");
        }
Example #7
0
        public void TranslateFormatFromDotNetWhereFormatForNonZeroSplitSecondsExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "F- FF- FFF- FFFF- FFFFFF- FFFFFFF-";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "F'- 'FF'- 'FFF'- 'FFFF'- 'FFFFFF'- 'FFFFFFF'-'");
        }
Example #8
0
        public void TranslateFormatFromDotNetWhereFormatForShortDateExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "MM/dd/yyyy";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "mm'/'dd'/'yyyy");
        }
Example #9
0
        public void TranslateFormatFromDotNetWhereFormatOfDayoftheMonthExpectedInDev2Format()
        {
            //initialize
            IDateTimeParser translatingParser = new DateTimeParser();
            string inputFormat = "d, dd, ddd, dddd";
            string error;

            //execute
            inputFormat = translatingParser.TranslateDotNetToDev2Format(inputFormat, out error);
            Assert.IsTrue(string.IsNullOrEmpty(error));
            Assert.AreEqual(inputFormat, "d', 'dd', 'dW', 'DW");
        }
Example #10
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);
        }
Example #11
0
 private static string Format_dw(IDateTimeResultTO dateTimeResultTO, DateTime dateTime)
 {
     return(DateTimeParser.GetDayOfWeekInt(dateTime.DayOfWeek).ToString(CultureInfo.InvariantCulture));
 }
 public string GetDateTimeFormatInformation()
 {
     var dateTimeParser = new DateTimeParser();
     string error;
     var translatedDateTimeFormat = dateTimeParser.TranslateDotNetToDev2Format(CultureInfo.CurrentUICulture.DateTimeFormat.FullDateTimePattern, out error);
     return translatedDateTimeFormat;
 }