public void DateTimeResultTO_Constructor_Sets_TimeZone()
        {
            var dateTimeResultTO = new DateTimeResultTO();

            Assert.IsTrue("South Africa Standard Time" == dateTimeResultTO.TimeZone.Name || "GMT Standard Time" == dateTimeResultTO.TimeZone.Name, "DateTimeResultTO did not get the correct timezone for South Africa or Ireland. Got: " + dateTimeResultTO.TimeZone.Name);
            Assert.IsTrue("(UTC+02:00) Harare, Pretoria" == dateTimeResultTO.TimeZone.LongName || "(UTC+00:00) Dublin, Edinburgh, Lisbon, London" == dateTimeResultTO.TimeZone.LongName, "DateTimeResultTO did not get the correct long format timezone for South Africa or Ireland. Got: " + dateTimeResultTO.TimeZone.LongName);
            Assert.IsTrue("South Africa Standard Time" == dateTimeResultTO.TimeZone.ShortName || "GMT Standard Time" == dateTimeResultTO.TimeZone.ShortName, "DateTimeResultTO did not get the correct short format timezone for South Africa or Ireland. Got: " + dateTimeResultTO.TimeZone.ShortName);
        }
        public void DateTimeResultTO_SetProperties_SetEra()
        {
            var dateTimeResultTO = new DateTimeResultTO();
            var value            = "A.D";

            dateTimeResultTO.Era = value.ToString(CultureInfo.InvariantCulture);
            Assert.AreEqual(value, dateTimeResultTO.Era);
        }
        public void DateTimeResultTO_Constructor_Sets_TimeZone()
        {
            var dateTimeResultTO = new DateTimeResultTO();

            Assert.AreEqual("South Africa Standard Time", dateTimeResultTO.TimeZone.Name);
            Assert.AreEqual("(UTC+02:00) Harare, Pretoria", dateTimeResultTO.TimeZone.LongName);
            Assert.AreEqual("South Africa Standard Time", dateTimeResultTO.TimeZone.ShortName);
        }
        public void DateTimeResultTO_NormalizeHours_AmPm_IsEqual_12am()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Hours = 12,
                AmPm  = DateTimeAmPm.am
            };

            dateTimeResultTO.NormalizeHours();
            Assert.AreEqual(0, dateTimeResultTO.Hours);
        }
        public void DateTimeResultTO_NormalizeHours_AmPm_IsEqual_pm()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Hours = 6,
                AmPm  = DateTimeAmPm.pm
            };

            dateTimeResultTO.NormalizeHours();
            Assert.AreEqual(18, dateTimeResultTO.Hours);
        }
        public void DateTimeResultTO_NormalizeHours_Is24HisFalse_HoursGreaterThan12()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Is24H = false,
                Hours = 16
            };

            dateTimeResultTO.NormalizeHours();
            Assert.AreEqual(DateTimeAmPm.pm, dateTimeResultTO.AmPm);
        }
Exemple #7
0
        bool TryParse(string data, string inputFormat, bool parseAsTime, out IDateTimeResultTO result,
                      out string error)
        {
            var nothingDied = true;

            result = new DateTimeResultTO();
            error  = "";
            var       originalInputFormat = inputFormat;
            var       originalData        = data;
            var       culturesTried       = 0;
            const int MaxAttempts         = 8;

            if (string.IsNullOrWhiteSpace(data))
            {
                originalData = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
            }

            if (string.IsNullOrWhiteSpace(inputFormat))
            {
                originalInputFormat =
                    TranslateDotNetToDev2Format(
                        GlobalConstants.Dev2DotNetDefaultDateTimeFormat.Replace("ss", "ss.fff"), out error);
            }
            else
            {
                culturesTried = MaxAttempts;
            }
            while (culturesTried <= MaxAttempts)
            {
                var dateTimeArray = originalData.ToArray();
                var position      = 0;
                // TODO: remove this allocation from the while loop
                var getDateTimeFormatParts = new GetDateTimeFormatPartsProcess(originalInputFormat, _dateTimeFormatForwardLookups, _dateTimeFormatPartOptions);
                nothingDied = getDateTimeFormatParts.Execute();
                error       = getDateTimeFormatParts.Error;
                if (!string.IsNullOrEmpty(error))
                {
                    return(false);
                }
                if (nothingDied)
                {
                    TryCulture(parseAsTime, ref result, ref error, ref nothingDied, ref originalInputFormat, ref culturesTried, MaxAttempts, dateTimeArray, ref position, getDateTimeFormatParts.Result);
                }
                else
                {
                    culturesTried++;
                }
            }

            return(nothingDied);
        }
        public void DateTimeResultTO_SetProperties_Months_Equal_0_DaysOfYear_NotEqual_0()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Years      = 0,
                Months     = 0,
                DaysOfYear = 20
            };

            dateTimeResultTO.ToDateTime();

            Assert.AreEqual(1, dateTimeResultTO.Years);
            Assert.AreEqual(1, dateTimeResultTO.Months);
            Assert.AreEqual(20, dateTimeResultTO.Days);
        }
        public void DateTimeResultTO_SetProperties_Months_Equal_0_Weeks_NotEqual_0()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Years  = 0,
                Months = 0,
                Weeks  = 20
            };

            dateTimeResultTO.ToDateTime();
            var tmpDate = CultureInfo.CurrentCulture.Calendar.AddWeeks(new System.DateTime(1, 1, 1), 20);
            var Months  = tmpDate.Month;
            var Days    = tmpDate.Day;

            Assert.AreEqual(1, dateTimeResultTO.Years);
            Assert.AreEqual(Months, dateTimeResultTO.Months);
            Assert.AreEqual(Days, dateTimeResultTO.Days);
        }
        public void DateTimeResultTO_SetProperties_Everything_Equal_0()
        {
            var dateTimeResultTO = new DateTimeResultTO
            {
                Years        = 0,
                Months       = 0,
                Days         = 0,
                Hours        = 0,
                Seconds      = 0,
                Milliseconds = 0,
                DaysOfYear   = 0,
                Weeks        = 0
            };

            dateTimeResultTO.ToDateTime();

            Assert.AreEqual(1, dateTimeResultTO.Years);
            Assert.AreEqual(1, dateTimeResultTO.Months);
            Assert.AreEqual(1, dateTimeResultTO.Days);
            Assert.AreEqual(0, dateTimeResultTO.Hours);
            Assert.AreEqual(0, dateTimeResultTO.Minutes);
            Assert.AreEqual(0, dateTimeResultTO.Seconds);
            Assert.AreEqual(0, dateTimeResultTO.Milliseconds);
        }
        /// <summary>
        ///     Parses the given data using the specified format
        /// </summary>
        private bool TryParse(string data, string inputFormat, bool parseAsTime, out IDateTimeResultTO result,
                              out string error)
        {
            bool nothingDied = true;

            result = new DateTimeResultTO();
            error  = "";

            int       culturesTried = 0;
            const int MaxAttempts   = 8;

            if (string.IsNullOrWhiteSpace(data))
            {
                data = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
            }

            if (string.IsNullOrWhiteSpace(inputFormat))
            {
                inputFormat =
                    TranslateDotNetToDev2Format(
                        GlobalConstants.Dev2DotNetDefaultDateTimeFormat.Replace("ss", "ss.fff"), out error);
            }
            else
            {
                culturesTried = MaxAttempts;
            }
            while (culturesTried <= MaxAttempts)
            {
                char[] dateTimeArray = data.ToArray();
                List <IDateTimeFormatPartTO> formatParts;
                int position = 0;


                nothingDied = TryGetDateTimeFormatParts(inputFormat, _dateTimeFormatForwardLookups, _dateTimeFormatPartOptions, out formatParts, out error);
                if (!string.IsNullOrEmpty(error))
                {
                    return(false);
                }
                if (nothingDied)
                {
                    int count = 0;
                    while (count < formatParts.Count && nothingDied && position < dateTimeArray.Length)
                    {
                        IDateTimeFormatPartTO formatPart = formatParts[count];

                        int resultLength;
                        if (TryGetDataFromDateTime(dateTimeArray, position, formatPart, result, parseAsTime,
                                                   out resultLength, out error))
                        {
                            position += resultLength;
                        }
                        else
                        {
                            //clear invalid result!
                            result      = new DateTimeResultTO();
                            nothingDied = false;
                        }

                        count++;
                    }
                    if (!nothingDied)
                    {
                        inputFormat = MatchInputFormatToCulture(ref error, culturesTried);

                        if (culturesTried >= MaxAttempts)
                        {
                            if (!IsBlankResult(result))
                            {
                                //Return the result if it isn't blank
                                nothingDied = true;
                            }
                            else
                            {
                                //no result, throw error
                                error = string.Format(ErrorResource.CannorParseInputDateTimeWithGivenFormat, error);
                            }
                        }
                        else
                        {
                            nothingDied = true;
                        }

                        culturesTried++;
                    }
                    else
                    {
                        //Stop trying different formats
                        culturesTried = MaxAttempts + 1;
                    }
                }
                else
                {
                    culturesTried++;
                }
            }

            return(nothingDied);
        }