Esempio n. 1
0
            public void NotANumber()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate("a", "b", "c", out result);

                Assert.IsFalse(success);
            }
Esempio n. 2
0
            public void InvalidDate()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate("2000", "2", "31", out result);

                Assert.IsFalse(success);
            }
Esempio n. 3
0
            public void AllNull()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate(null, null, null, out result);

                Assert.IsFalse(success);
            }
Esempio n. 4
0
            public void OneNull()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate("2000", "07", null, out result);

                Assert.IsFalse(success);
            }
Esempio n. 5
0
            public void AllEmptyStrings()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate("", "", "", out result);

                Assert.IsFalse(success);
            }
Esempio n. 6
0
            public void OneEmptyString()
            {
                DateTime result;
                bool     success = DateTimeFunctions.TryCreate("2000", "07", "", out result);

                Assert.IsFalse(success);
            }
Esempio n. 7
0
            public void Default()
            {
                DateTime result;

                DateTimeFunctions.TryCreate("2000", "07", "05", out result);

                Assert.AreEqual(new DateTime(2000, 7, 5), result);
            }
Esempio n. 8
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            // Html.ValidationMessageFor() requires an errorMessage (over an exception) to be set when adding errors to the ModelState

            DateTime result;

            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }

            // datetime split over 3 inputs
            if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName + ".Year") &&
                bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName + ".Month") &&
                bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName + ".Day"))
            {
                string year, month, day;

                if (bindingContext.TryGetValue(".Year", out year) &&
                    bindingContext.TryGetValue(".Month", out month) &&
                    bindingContext.TryGetValue(".Day", out day))
                {
                    if (DateTimeFunctions.TryCreate(year, month, day, out result))
                    {
                        return(result);
                    }

                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format(@"""{0}/{1}/{2}"" is not a valid date", year, month, day));
                    return(null);
                }
            }

            // normal datetime
            if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {
                string date;

                if (bindingContext.TryGetValue(out date))
                {
                    if (DateTime.TryParse(date, out result))
                    {
                        return(result);
                    }

                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format(@"""{0}"" is not a valid date", date));
                    return(null);
                }
            }

            bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Unable to bind the date/time");
            return(null);
        }
Esempio n. 9
0
            public void LocalizationGB()
            {
                CultureInfo currentCultureInfo = Thread.CurrentThread.CurrentCulture;

                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

                DateTime result;

                DateTimeFunctions.TryCreate("2000", "07", "05", out result);

                Assert.AreEqual(new DateTime(2000, 7, 5), result);

                Thread.CurrentThread.CurrentCulture = currentCultureInfo;
            }