Exemple #1
0
        public override object InternalEvaluate(TagModel model)
        {
            string      dateStr = GetAutoValueAsString("Value", model);
            CultureInfo culture = ParseLocale != null
                                      ? new CultureInfo(GetAsString(ParseLocale, model))
                                      : (CultureInfo)model[FormatConstants.LOCALE];
            var format =
                (DateTimeFormatInfo)DateTimeFormatInfo.GetInstance(culture.DateTimeFormat).Clone();
            DateTime?result = null;

            if (string.IsNullOrEmpty(dateStr))
            {
                dateStr = GetAutoValueAsString(nameof(DefaultValue), model);
            }

            if (!String.IsNullOrEmpty(dateStr))
            {
                if (GetAutoValueAsBool("Exact", model))
                {
                    string pattern = GetAsString(Pattern, model) ?? GetPattern(model, format);
                    try
                    {
                        result = DateTime.ParseExact(dateStr, pattern, format);
                    } catch (FormatException)
                    {
                        throw TagException.ParseException(dateStr, "Date").Decorate(Context);
                    }
                }
                else
                {
                    result = DateTime.Parse(dateStr, format);
                }
            }
            return(result);
        }
Exemple #2
0
        public void TestSimpleParse_ExplicitExact_WrongFormat()
        {
            var date = new ParseDate();

            date.Value = new MockAttribute(new Constant("1/6/2008 12:00:00"));
            date.Exact = new MockAttribute(new Constant("true"));
            try
            {
                date.Evaluate(_model);
                Assert.Fail("Should fail");
            }
            catch (TagException Te)
            {
                Assert.That(Te.Message, Is.EqualTo(TagException.ParseException("1/6/2008 12:00:00", "Date").Message));
            }
        }
        public void TestFromValueDecimalToVarWithDecimalSeperatorNotAllowedInStyle()
        {
            var tag = new ParseNumber();

            tag.Value  = new MockAttribute(new StringConstant("1,345.67"));
            tag.Var    = new MockAttribute(new StringConstant("Number"));
            tag.Styles = new MockAttribute(new StringConstant(NumberStyles.AllowDecimalPoint.ToString()));
            try
            {
                tag.Evaluate(_model);
                Assert.Fail("Should be in incorrect format");
            }
            catch (TagException Te)
            {
                Assert.That(Te.Message,
                            Is.EqualTo(TagException.ParseException("1,345.67", "Number").Message));
            }
        }
 private decimal Parse(TagModel model, string number, NumberType type)
 {
     try
     {
         decimal     result;
         CultureInfo culture = ParseLocale != null
                                   ? new CultureInfo(GetAsString(ParseLocale, model))
                                   : (CultureInfo)model[FormatConstants.LOCALE];
         var  format      = (NumberFormatInfo)NumberFormatInfo.GetInstance(culture.NumberFormat).Clone();
         bool integerOnly = INTEGER_ONLY;
         if (IntegerOnly != null)
         {
             integerOnly = GetAutoValueAsBool("IntegerOnly", model);
         }
         NumberStyles style = FormatConstants.NUMBERSTYLES[type];
         style  = GetAsNumberStyle(Styles, model, style);
         result = InternalParse(format, number, style);
         return(integerOnly ? Decimal.Floor(result) : result);
     } catch (FormatException)
     {
         throw TagException.ParseException(number, type.ToString()).Decorate(Context);
     }
 }