Пример #1
0
        public override IValue Eval(IValue[] parameters)
        {
            var thenBranch = default(IValue);
            var elseBranch = default(IValue);

            if (parameters.Length == 2)
            {
                thenBranch = parameters[1];
                elseBranch = new EmptyValue();
            }
            else if (parameters.Length == 3)
            {
                thenBranch = parameters[1];
                elseBranch = parameters[2];
            }
            else
            {
                throw new OperandNumberException();
            }

            if (parameters[0].Type != Values.ValueType.Number)
            {
                throw new WrongOperandTypeException();
            }

            return(parameters[0].AsDouble != 0 ? thenBranch : elseBranch);
        }
Пример #2
0
        /// <summary>
        ///     Appends the INamable which match the name provided in retVal
        /// </summary>
        /// <param name="name"></param>
        /// <param name="retVal"></param>
        public void Find(string name, List <INamable> retVal)
        {
            if (!_buildingDeclaredElements)
            {
                try
                {
                    _buildingDeclaredElements = true;

                    StructureValue structureValue = Value as StructureValue;
                    if (structureValue != null)
                    {
                        structureValue.Find(name, retVal);
                    }

                    // Dereference of an empty value holds the empty value
                    EmptyValue emptyValue = Value as EmptyValue;
                    if (emptyValue != null)
                    {
                        retVal.Add(emptyValue);
                    }
                }
                finally
                {
                    _buildingDeclaredElements = false;
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object (as text).</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(string value, EmptyValue emptyValue)
 {
     _emptyValue  = emptyValue;
     _format      = null;
     _initialized = true;
     _date        = DateTime.MinValue;
     this.Text    = value;
 }
Пример #4
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object (as text).</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(string value, bool emptyIsMin)
 {
     _emptyValue  = GetEmptyValue(emptyIsMin);
     _format      = null;
     _initialized = true;
     _date        = DateTime.MinValue;
     this.Text    = value;
 }
Пример #5
0
 public SmartDate(DateTime value, bool emptyIsMin, string format)
 {
     _emptyValue  = GetEmptyValue(emptyIsMin);
     _format      = format;
     _initialized = false;
     _date        = DateTime.MinValue;
     Date         = value;
 }
Пример #6
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <remarks>
 /// SmartDate maintains the date value as a DateTime,
 /// so the provided DateTimeOffset is converted to a
 /// DateTime in this constructor. You should be aware
 /// that this can lead to a loss of precision in
 /// some cases.
 /// </remarks>
 public SmartDate(DateTimeOffset value, EmptyValue emptyValue)
 {
     _emptyValue  = emptyValue;
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     Date         = value.DateTime;
 }
Пример #7
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 /// <remarks>
 /// SmartDate maintains the date value as a DateTime,
 /// so the provided DateTimeOffset is converted to a
 /// DateTime in this constructor. You should be aware
 /// that this can lead to a loss of precision in
 /// some cases.
 /// </remarks>
 public SmartDate(DateTimeOffset value, bool emptyIsMin)
 {
     _emptyValue  = GetEmptyValue(emptyIsMin);
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     Date         = value.DateTime;
 }
Пример #8
0
 void IMobileObject.SetState(SerializationInfo info)
 {
     _date          = info.GetValue <DateTime>("SmartDate._date");
     _defaultFormat = info.GetValue <string>("SmartDate._defaultFormat");
     _emptyValue    = (EmptyValue)System.Enum.Parse(typeof(EmptyValue), info.GetValue <string>("SmartDate._emptyValue"), true);
     _format        = info.GetValue <string>("SmartDate._format");
     _initialized   = info.GetValue <bool>("SmartDate._initialized");
 }
Пример #9
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <param name="kind">One of the DateTimeKind values.</param>
 public SmartDate(DateTime value, EmptyValue emptyValue, DateTimeKind kind)
 {
     _emptyValue  = emptyValue;
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     Date         = DateTime.SpecifyKind(value, kind);
 }
Пример #10
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <remarks>
 /// The SmartDate created will use the min possible
 /// date to represent an empty date.
 /// </remarks>
 /// <param name="value">The initial value of the object.</param>
 public SmartDate(DateTime value)
 {
     _emptyValue  = Csla.SmartDate.EmptyValue.MinDate;
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     Date         = value;
 }
        /// <summary>Initializes a new instance of the <see cref="VertexAdjacencyFactory{TEdgeData}" /> class.</summary>
        /// <param name="canReplaceVertexInstanceOnModification">
        /// if set to <c>true</c> the vertex instance can be replaced after modification of the vertex adjacency;
        /// otherwise, <c>false</c> - it will always produce vertex with unlimited capacity.
        /// </param>
        public VertexAdjacencyFactory(bool canReplaceVertexInstanceOnModification = true)
        {
            var replace = _canReplaceVertexInstanceOnModification = canReplaceVertexInstanceOnModification;

            _factory = EmptyValue.IsEmptyValueType <TEdgeData>() ?
                       (ReplaceableVertexFactoryInterface <TEdgeData>)(object) new InternalEmptyValueEdgeFactory(replace) :
                       new InternalFactory(replace);
        }
Пример #12
0
        /// <summary>
        /// 判断字符串是否能转换为一个Datetime日期,并返回转换成功后的日期
        /// </summary>
        /// <param name="value">要转换的日期字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>
        /// <param name="result">返回转换成功后的日期</param>
        private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
        {
            //定义一个临时日期
            DateTime temp;

            #region 如果日期字符串为空,则转成最大或最小的日期
            if (string.IsNullOrEmpty(value))
            {
                //如果空日期的取值类型为MinDate,则转成最小日期,否则最大日期
                if (emptyValue == EmptyValue.MinDate)
                {
                    result = _minDate;
                }
                else
                {
                    result = DateTime.MaxValue;
                }

                //返回true
                return(true);
            }
            #endregion

            #region 将日期字符串转换为DateTime
            if (DateTime.TryParse(value, out temp))
            {
                //如果转换成功,则返回true
                result = temp;
                return(true);
            }
            #endregion

            #region 如果日期字符串是自定义的值,则进行转换
            //将日期字符串转成大写
            string date = value.Trim().ToLower();
            //转换成今天的日期
            if (date == SmartDateString.SmartDateT || date == SmartDateString.SmartDateToday || date == ".")
            {
                result = DateTime.Now;
                return(true);
            }
            //转换成昨天的日期
            if (date == SmartDateString.SmartDateY || date == SmartDateString.SmartDateYesterday || date == "-")
            {
                result = DateTime.Now.AddDays(-1);
                return(true);
            }
            //转换成明天的日期
            if (date == SmartDateString.SmartDateTom || date == SmartDateString.SmartDateTomorrow || date == "+")
            {
                result = DateTime.Now.AddDays(1);
                return(true);
            }
            #endregion

            //转换失败,返回false
            return(false);
        }
Пример #13
0
        public static void Execute(EmptyValue request)
        {
            using (DatabaseContext context = new DatabaseContext())
            {
                DateTime lastUpdate = DateTime.MinValue;

                if (context.AccountRatings.Count() > 0)
                {
                    lastUpdate = (from r in context.AccountRatings
                                  select r.LastModified).Max();
                }

                //get all ratings made since the last update
                var newRatings = (from r in context.TradeReviews
                                  where r.LastModified > lastUpdate
                                  select r).ToList();

                List <Guid> list = new List <Guid>();

                foreach (var item in newRatings)
                {
                    if (!list.Contains(item.BuyerGuid))
                    {
                        list.Add(item.BuyerGuid);
                    }

                    if (!list.Contains(item.SellerGuid))
                    {
                        list.Add(item.SellerGuid);
                    }
                }

                List <AccountRating> ratings = list.ConvertAll(CalculateRating);

                ratings.ForEach((rating) =>
                {
                    var existingRating = (from a in context.AccountRatings
                                          where a.AccountGuid == rating.AccountGuid
                                          select a).FirstOrDefault();

                    if (existingRating == null)
                    {
                        context.AccountRatings.Add(rating);
                    }
                    else
                    {
                        existingRating.BuyerRating       = rating.BuyerRating;
                        existingRating.BuyerRatingCount  = rating.BuyerRatingCount;
                        existingRating.LastModified      = rating.LastModified;
                        existingRating.Rating            = rating.Rating;
                        existingRating.RatingCount       = rating.RatingCount;
                        existingRating.SellerRating      = rating.SellerRating;
                        existingRating.SellerRatingCount = rating.SellerRatingCount;
                    }
                    context.SaveChanges();
                });
            }
        }
Пример #14
0
        public SmartDate(EmptyValue emptyValue)
        {
            _emptyValue  = emptyValue;
            _format      = null;
            _initialized = false;

            _date = DateTime.MinValue;
            SetEmptyDate(_emptyValue);
        }
Пример #15
0
        public static void Execute(EmptyValue request)
        {
            DateTime startTime = DateTime.UtcNow;
            DateTime pointer   = GetPointer();

            using (DatabaseContext context = new DatabaseContext())
            {
                //get expired offers
                var tradeOffers = (from o in context.TradeOffers
                                   where !o.IsCancelled && !o.IsAccepted && DateTime.UtcNow > o.Deadline &&
                                   o.Deadline > pointer
                                   select o).ToList();

                List <Notification> newNotifications = new List <Notification>();
                //convert to notifications
                foreach (var item in tradeOffers)
                {
                    Account from = (from a in context.Accounts
                                    where a.Guid == item.FromAccountGuid
                                    select a).First();

                    Account to = (from a in context.Accounts
                                  where a.Guid == item.ToAccountGuid
                                  select a).First();

                    //send to orderer
                    Notification ordererNotification = new Notification
                    {
                        Guid          = Guid.NewGuid(),
                        Created       = startTime,
                        Href          = String.Format(@"/{0}/tradeOffer/{1}", from.LanguageCode, item.Guid),
                        RecipientGuid = from.Guid,
                        SenderGuid    = Guid.Empty,
                        Type          = OfferExpiredOrdererNotification
                    };

                    newNotifications.Add(ordererNotification);

                    Notification posterNotification = new Notification
                    {
                        Guid          = Guid.NewGuid(),
                        Created       = startTime,
                        Href          = String.Format(@"/{0}/tradeOffer/{1}", from.LanguageCode, item.Guid),
                        RecipientGuid = to.Guid,
                        SenderGuid    = Guid.Empty,
                        Type          = OfferExpiredPosterNotification
                    };

                    newNotifications.Add(posterNotification);
                }

                context.Notifications.AddRange(newNotifications);
                context.SaveChanges();

                NotificationSmsDeliverer.RunPort.Post(new EmptyValue());
            }
        }
Пример #16
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || value.ToString().Equals(EmptyValue.ToString()))
            {
                return(new ValidationResult(false, Message));
            }

            return(ValidationResult.ValidResult);
        }
Пример #17
0
 public SmartDate(bool emptyIsMin, string format)
 {
     _emptyValue  = GetEmptyValue(emptyIsMin);
     _format      = format;
     _initialized = false;
     // provide a dummy value to allow real initialization
     _date = DateTime.MinValue;
     SetEmptyDate(_emptyValue);
 }
Пример #18
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(EmptyValue emptyValue)
 {
     _emptyValue  = emptyValue;
     _format      = null;
     _initialized = false;
     // provide a dummy value to allow real initialization
     _date = DateTime.MinValue;
     SetEmptyDate(_emptyValue);
 }
Пример #19
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(bool emptyIsMin)
 {
   _emptyValue = GetEmptyValue(emptyIsMin);
   _format = null;
   _initialized = false;
   // provide a dummy value to allow real initialization
   _date = DateTime.MinValue;
   SetEmptyDate(_emptyValue);
 }
Пример #20
0
        /// <summary>Creates the vertex comparer with specified equality comparer of edge data.</summary>
        /// <typeparam name="TEdgeData">The type of the edge data.</typeparam>
        /// <param name="edgeDataEqualityComparer">The equality comparer of edge data, if <c>null</c> default comparer will be used.</param>
        /// <param name="comparisonType">Type of the comparison.</param>
        /// <returns>Comparer instance.</returns>
        public static VertexAdjacencyComparer <TEdgeData> Create <TEdgeData>(
            IEqualityComparer <TEdgeData> edgeDataEqualityComparer, VertexComparisonType comparisonType)
        {
            if (edgeDataEqualityComparer == null || EmptyValue.IsEmptyValueType <TEdgeData>())
            {
                return(VertexAdjacencyComparer <TEdgeData> .GetDefault(comparisonType));
            }

            return(new VertexAdjacencyComparer <TEdgeData>(edgeDataEqualityComparer, comparisonType));
        }
Пример #21
0
        public static DateTime StringToDate(string value, EmptyValue emptyValue)
        {
            DateTime result = DateTime.MinValue;

            if (TryStringToDate(value, emptyValue, ref result))
            {
                return(result);
            }
            throw new ArgumentException(Resources.StringToDateException);
        }
Пример #22
0
 private void SetEmptyDate(EmptyValue emptyValue)
 {
     if (emptyValue == EmptyValue.MinDate)
     {
         Date = DateTime.MinValue;
     }
     else
     {
         Date = DateTime.MaxValue;
     }
 }
Пример #23
0
        public static bool TryParse(string value, EmptyValue emptyValue, ref SmartDate result)
        {
            DateTime dateResult = DateTime.MinValue;

            if (TryStringToDate(value, emptyValue, ref dateResult))
            {
                result = new SmartDate(dateResult, emptyValue);
                return(true);
            }
            return(false);
        }
Пример #24
0
        private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
        {
            // call custom parser if set...
            if (_customParser != null)
            {
                var tmpValue = _customParser.Invoke(value);
                // i f custom parser returned a value then parsing succeeded
                if (tmpValue.HasValue)
                {
                    result = tmpValue.Value;
                    return(true);
                }
            }

            DateTime tmp;

            if (String.IsNullOrEmpty(value))
            {
                result = emptyValue == EmptyValue.MinDate ? DateTime.MinValue : DateTime.MaxValue;
                return(true);
            }
            if (DateTime.TryParse(value, out tmp))
            {
                result = tmp;
                return(true);
            }


            string ldate = value.Trim().ToLower();

            if (ldate == Resources.SmartDateT ||
                ldate == Resources.SmartDateToday ||
                ldate == ".")
            {
                result = DateTime.Now;
                return(true);
            }
            if (ldate == Resources.SmartDateY ||
                ldate == Resources.SmartDateYesterday ||
                ldate == "-")
            {
                result = DateTime.Now.AddDays(-1);
                return(true);
            }
            if (ldate == Resources.SmartDateTom ||
                ldate == Resources.SmartDateTomorrow ||
                ldate == "+")
            {
                result = DateTime.Now.AddDays(1);
                return(true);
            }

            return(false);
        }
Пример #25
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(DateTime?value, EmptyValue emptyValue)
 {
     _emptyValue  = emptyValue;
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     if (value.HasValue)
     {
         Date = value.Value;
     }
 }
Пример #26
0
 /// <summary>
 /// 为空日期赋值
 /// </summary>
 /// <param name="emptyValue">空日期的类型</param>
 private void SetEmptyDate(EmptyValue emptyValue)
 {
     if (emptyValue == EmptyValue.MinDate)
     {
         this.Date = _minDate;
     }
     else
     {
         this.Date = DateTime.MaxValue;
     }
 }
Пример #27
0
 private void SetEmptyDate(EmptyValue emptyValue)
 {
     if (emptyValue == SmartDate.EmptyValue.MinDate)
     {
         this.Date = DateTime.MinValue;
     }
     else
     {
         this.Date = DateTime.MaxValue;
     }
 }
Пример #28
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(DateTime?value, bool emptyIsMin)
 {
     _emptyValue  = GetEmptyValue(emptyIsMin);
     _format      = null;
     _initialized = false;
     _date        = DateTime.MinValue;
     if (value.HasValue)
     {
         Date = value.Value;
     }
 }
Пример #29
0
        /// <summary>
        /// Converts a text date representation into a Date value.
        /// </summary>
        /// <remarks>
        /// An empty string is assumed to represent an empty date. An empty date
        /// is returned as the MinValue or MaxValue of the Date datatype depending
        /// on the EmptyIsMin parameter.
        /// </remarks>
        /// <param name="value">The text representation of the date.</param>
        /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
        /// <returns>A Date value.</returns>
        public static DateTime StringToDate(string value, EmptyValue emptyValue)
        {
            DateTime result = DateTime.MinValue;

            if (TryStringToDate(value, emptyValue, ref result))
            {
                return(result);
            }
            else
            {
                throw new ArgumentException("String value can not be converted to a date");
            }
        }
Пример #30
0
        private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
        {
            DateTime tmp;

            if (String.IsNullOrEmpty(value))
            {
                if (emptyValue == EmptyValue.MinDate)
                {
                    result = DateTime.MinValue;
                    return(true);
                }
                else
                {
                    result = DateTime.MaxValue;
                    return(true);
                }
            }
            else if (DateTime.TryParse(value, out tmp))
            {
                result = tmp;
                return(true);
            }
            else
            {
                string ldate = value.Trim().ToLower();
                if (ldate == Resources.SmartDateT ||
                    ldate == Resources.SmartDateToday ||
                    ldate == ".")
                {
                    result = DateTime.Now;
                    return(true);
                }
                if (ldate == Resources.SmartDateY ||
                    ldate == Resources.SmartDateYesterday ||
                    ldate == "-")
                {
                    result = DateTime.Now.AddDays(-1);
                    return(true);
                }
                if (ldate == Resources.SmartDateTom ||
                    ldate == Resources.SmartDateTomorrow ||
                    ldate == "+")
                {
                    result = DateTime.Now.AddDays(1);
                    return(true);
                }
            }
            return(false);
        }
Пример #31
0
        /// <summary>
        /// 将字符串转换为DateTime类型日期
        /// </summary>
        /// <param name="value">日期字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>
        public static DateTime StringToDate(string value, EmptyValue emptyValue)
        {
            //要返回的的日期
            DateTime result = _minDate;

            //如果string成功转换为DateTime,则返回
            if (TryStringToDate(value, emptyValue, ref result))
            {
                return(result);
            }
            else
            {
                //转换失败,抛出异常
                throw new ArgumentException(SmartDateString.StringToDateException);
            }
        }
Пример #32
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="emptyIsMin">空日期是否转换成最小日期</param>
        public SmartDate(bool emptyIsMin)
        {
            //将空日期的布尔标识转成枚举标识
            _emptyValue = GetEmptyValue(emptyIsMin);

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = false;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置空日期
            SetEmptyDate(_emptyValue);
        }
Пример #33
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <param name="kind">One of the DateTimeKind values.</param>
 public SmartDate(DateTime value, EmptyValue emptyValue, DateTimeKind kind)
 {
   _emptyValue = emptyValue;
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   Date = DateTime.SpecifyKind(value, kind);
 }
Пример #34
0
        /// <summary>
        /// 将DateTime类型日期转换为指定格式的字符串
        /// </summary>
        /// <param name="value">要转换的日期</param>
        /// <param name="formatString">格式字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>        
        public static string DateToString(DateTime value, string formatString, EmptyValue emptyValue)
        {
            //如果日期为最大日期或最小日期,则转换为空字符串
            if (emptyValue == EmptyValue.MinDate)
            {
                if (value == _minDate)
                {
                    return string.Empty;
                }
            }
            else
            {
                if (value == DateTime.MaxValue)
                {
                    return string.Empty;
                }
            }

            //将日期以指定格式的字符串返回
            return string.Format("{0:" + formatString + "}", value);
        }
Пример #35
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object (as text).</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(string value, EmptyValue emptyValue)
 {
   _emptyValue = emptyValue;
   _format = null;
   _initialized = true;
   _date = DateTime.MinValue;
   this.Text = value;
 }
Пример #36
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object (as text).</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(string value, bool emptyIsMin)
 {
   _emptyValue = GetEmptyValue(emptyIsMin);
   _format = null;
   _initialized = true;
   _date = DateTime.MinValue;
   this.Text = value;
 }
Пример #37
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <remarks>
 /// SmartDate maintains the date value as a DateTime,
 /// so the provided DateTimeOffset is converted to a
 /// DateTime in this constructor. You should be aware
 /// that this can lead to a loss of precision in
 /// some cases.
 /// </remarks>
 public SmartDate(DateTimeOffset value, EmptyValue emptyValue)
 {
   _emptyValue = emptyValue;
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   Date = value.DateTime;
 }
Пример #38
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 /// <remarks>
 /// SmartDate maintains the date value as a DateTime,
 /// so the provided DateTimeOffset is converted to a
 /// DateTime in this constructor. You should be aware
 /// that this can lead to a loss of precision in
 /// some cases.
 /// </remarks>
 public SmartDate(DateTimeOffset value, bool emptyIsMin)
 {
   _emptyValue = GetEmptyValue(emptyIsMin);
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   Date = value.DateTime;
 }
 private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
 {
   value = NormalizeInput(value.Trim());
   DateTime tmp;
   if (String.IsNullOrEmpty(value))
   {
     if (emptyValue == EmptyValue.MinDate)
     {
       result = DateTime.MinValue;
       return true;
     }
     else
     {
       result = DateTime.MaxValue;
       return true;
     }
   }
   else if (TryParseExtended(value, out tmp))
   {
       result = tmp;
       return true;
   }
   else if (DateTime.TryParse(value, out tmp))
   {
     result = tmp;
     return true;
   }
   else
   {
     string ldate = value.Trim().ToLower();
     if (ldate == Resources.SmartDateT ||
         ldate == Resources.SmartDateToday ||
         ldate == ".")
     {
       result = DateTime.Now;
       return true;
     }
     if (ldate == Resources.SmartDateY ||
         ldate == Resources.SmartDateYesterday ||
         ldate == "-")
     {
       result = DateTime.Now.AddDays(-1);
       return true;
     }
     if (ldate == Resources.SmartDateTom ||
         ldate == Resources.SmartDateTomorrow ||
         ldate == "+")
     {
       result = DateTime.Now.AddDays(1);
       return true;
     }
     if (ldate == "<")
     {
       result = DateTime.Now.AddMonths(-1);
       return true;
     }
     if (ldate == ">")
     {
       result = DateTime.Now.AddMonths(1);
       return true;
     }
     if (ldate.Substring(0, 1) == "+")
     {
       result = DateTime.Now.AddDays(Convert.ToInt32(ldate.Substring(1)));
       return true;
     }
     if (ldate.Substring(0, 1) == "-")
     {
       result = DateTime.Now.AddDays(-1 * Convert.ToInt32(ldate.Substring(1)));
       return true;
     }
     if (ldate.Substring(0, 1) == ".")
     {
       result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, Convert.ToInt32(ldate.Substring(1)));
       return true;
     }
     if (ldate.Substring(0, 1) == "<")
     {
       return KeepDay(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1), Convert.ToInt32(ldate.Substring(1)), out result);          
     }
     if (ldate.Substring(0, 1) == ">")
     {
       return KeepDay(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1), Convert.ToInt32(ldate.Substring(1)), out result);
     }
     if (Regex.IsMatch(ldate, @"^\d{1,2}$", RegexOptions.Compiled))
     {
       int day = Convert.ToInt32(ldate);
       if (DateTime.Now.Day - day < -16)
       {
         return KeepDay(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1), day, out result);
       }
       else if (DateTime.Now.Day - day > 16)
       {
         return KeepDay(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1), day, out result);
       }
       else
       {
         result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, day);
         return true;
       }
     }
   }
   return false;
 }
Пример #40
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyIsMin">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(DateTime? value, bool emptyIsMin)
 {
   _emptyValue = GetEmptyValue(emptyIsMin);
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   if (value.HasValue)
     Date = value.Value;
 }
Пример #41
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <remarks>
 /// The SmartDate created will use the min possible
 /// date to represent an empty date.
 /// </remarks>
 /// <param name="value">The initial value of the object.</param>
 public SmartDate(DateTime value)
 {
   _emptyValue = Csla.SmartDate.EmptyValue.MinDate;
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   Date = value;
 }
Пример #42
0
        /// <summary>
        /// 判断字符串是否能转换为一个SmartDate日期,并返回转换成功后的日期
        /// </summary>
        /// <param name="value">要转换的日期字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>
        /// <param name="result">返回转换成功后的日期</param>        
        public static bool TryParse(string value, EmptyValue emptyValue, ref SmartDate result)
        {
            //定义用于存储日期字符串转换的DateTime日期
            DateTime dateResult = _minDate;

            //将日期字符串转换为DateTime日期
            if (TryStringToDate(value, emptyValue, ref dateResult))
            {
                //如果转换成功,则返回该日期的SmartDate类型
                result = new SmartDate(dateResult, emptyValue);

                return true;
            }
            else
            {
                return false;
            }
        }
Пример #43
0
 private void SetEmptyDate(EmptyValue emptyValue)
 {
   if (emptyValue == SmartDate.EmptyValue.MinDate)
     this.Date = DateTime.MinValue;
   else
     this.Date = DateTime.MaxValue;
 }
Пример #44
0
        /// <summary>
        /// 将字符串转换为DateTime类型日期
        /// </summary>
        /// <param name="value">日期字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>        
        public static DateTime StringToDate(string value, EmptyValue emptyValue)
        {
            //要返回的的日期
            DateTime result = _minDate;

            //如果string成功转换为DateTime,则返回
            if (TryStringToDate(value, emptyValue, ref result))
            {
                return result;
            }
            else
            {
                //转换失败,抛出异常
                throw new ArgumentException(SmartDateString.StringToDateException);
            }
        }
Пример #45
0
 /// <summary>
 /// Converts a string value into a SmartDate.
 /// </summary>
 /// <param name="value">String containing the date value.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <returns>A new SmartDate containing the date value.</returns>
 public static SmartDate Parse(string value, EmptyValue emptyValue)
 {
   return new SmartDate(value, emptyValue);
 }
Пример #46
0
 /// <summary>
 /// 将字符串转换为SmartDate类型,可指定空日期的取值类型
 /// </summary>
 /// <param name="date">字符串形式的日期</param>
 /// <param name="emptyDateType">空日期的取值类型</param>        
 public static SmartDate Parse(string date, EmptyValue emptyDateType)
 {
     return new SmartDate(date, emptyDateType);
 }
Пример #47
0
 /// <summary>
 /// Converts a string value into a SmartDate.
 /// </summary>
 /// <param name="value">String containing the date value.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <param name="result">The resulting SmartDate value if the parse was successful.</param>
 /// <returns>A value indicating if the parse was successful.</returns>
 public static bool TryParse(string value, EmptyValue emptyValue, ref SmartDate result)
 {
   System.DateTime dateResult = DateTime.MinValue;
   if (TryStringToDate(value, emptyValue, ref dateResult))
   {
     result = new SmartDate(dateResult, emptyValue);
     return true;
   }
   else
   {
     return false;
   }
 }
Пример #48
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="value">要设置的日期</param>
        /// <param name="emptyValue">空日期的类型</param>
        public SmartDate(string value, EmptyValue emptyValue)
        {
            //设置空日期的类型
            _emptyValue = emptyValue;

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = true;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置日期
            this.Text = value;
        }
Пример #49
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="value">要设置的日期</param>
        /// <param name="emptyValue">空日期的类型</param>
        public SmartDate(DateTime value, EmptyValue emptyValue)
        {
            //设置空日期的类型
            _emptyValue = emptyValue;

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = false;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置日期
            Date = value;
        }
Пример #50
0
 /// <summary>
 /// Converts a text date representation into a Date value.
 /// </summary>
 /// <remarks>
 /// An empty string is assumed to represent an empty date. An empty date
 /// is returned as the MinValue or MaxValue of the Date datatype depending
 /// on the EmptyIsMin parameter.
 /// </remarks>
 /// <param name="value">The text representation of the date.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <returns>A Date value.</returns>
 public static DateTime StringToDate(string value, EmptyValue emptyValue)
 {
   DateTime result = DateTime.MinValue;
   if (TryStringToDate(value, emptyValue, ref result))
     return result;
   else
     throw new ArgumentException(Resources.StringToDateException);
 }
Пример #51
0
 void IMobileObject.SetState(SerializationInfo info)
 {
   _date = info.GetValue<DateTime>("SmartDate._date");
   _defaultFormat = info.GetValue<string>("SmartDate._defaultFormat");
   _emptyValue = (EmptyValue)System.Enum.Parse(typeof(EmptyValue), info.GetValue<string>("SmartDate._emptyValue"), true);
   _format = info.GetValue<string>("SmartDate._format");
   _initialized = info.GetValue<bool>("SmartDate._initialized");
 }
Пример #52
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="value">要设置的日期</param>
        /// <param name="emptyIsMin">空日期是否转换成最小日期</param>
        public SmartDate(string value, bool emptyIsMin)
        {
            //将空日期的布尔标识转成枚举标识
            _emptyValue = GetEmptyValue(emptyIsMin);

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = true;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置日期
            this.Text = value;
        }
Пример #53
0
    private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
    {
      // call custom parser if set...
      if (_customParser != null)
      {
        var tmpValue = _customParser.Invoke(value);
        // i f custom parser returned a value then parsing succeeded
        if (tmpValue.HasValue)
        {
          result = tmpValue.Value;
          return true;
        }
      }

      DateTime tmp;
      if (String.IsNullOrEmpty(value))
      {
        result = emptyValue == EmptyValue.MinDate ? DateTime.MinValue : DateTime.MaxValue;
        return true;
      }
      if (DateTime.TryParse(value, out tmp))
      {
        result = tmp;
        return true;
      }
      

      string ldate = value.Trim().ToLower();
      if (ldate == Resources.SmartDateT ||
          ldate == Resources.SmartDateToday ||
          ldate == ".")
      {
        result = DateTime.Now;
        return true;
      }
      if (ldate == Resources.SmartDateY ||
          ldate == Resources.SmartDateYesterday ||
          ldate == "-")
      {
        result = DateTime.Now.AddDays(-1);
        return true;
      }
      if (ldate == Resources.SmartDateTom ||
          ldate == Resources.SmartDateTomorrow ||
          ldate == "+")
      {
        result = DateTime.Now.AddDays(1);
        return true;
      }

      return false;
    }
Пример #54
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="value">要设置的日期</param>
        /// <param name="emptyIsMin">空日期是否转换成最小日期</param>
        public SmartDate(DateTime value, bool emptyIsMin)
        {
            //将空日期的布尔标识转成枚举标识
            _emptyValue = GetEmptyValue(emptyIsMin);

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = false;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置日期
            Date = value;
        }
Пример #55
0
        /// <summary>
        /// 创建SmartDate的实例
        /// </summary>
        /// <param name="emptyValue">空日期的类型</param>
        public SmartDate(EmptyValue emptyValue)
        {
            //设置空日期的类型
            _emptyValue = emptyValue;

            //设置格式字符串
            _format = null;

            //设置初始化标识
            _initialized = false;

            //将日期最小值赋给日期字段
            _date = _minDate;

            //设置空日期
            SetEmptyDate(_emptyValue);
        }
Пример #56
0
 /// <summary>
 /// 为空日期赋值
 /// </summary>
 /// <param name="emptyValue">空日期的类型</param>
 private void SetEmptyDate(EmptyValue emptyValue)
 {
     if (emptyValue == EmptyValue.MinDate)
     {
         this.Date = _minDate;
     }
     else
     {
         this.Date = DateTime.MaxValue;
     }
 }
Пример #57
0
 /// <summary>
 /// Converts a date value into a text representation.
 /// </summary>
 /// <remarks>
 /// Whether the date value is considered empty is determined by
 /// the EmptyIsMin parameter value. If the date is empty, this
 /// method returns an empty string. Otherwise it returns the date
 /// value formatted based on the FormatString parameter.
 /// </remarks>
 /// <param name="value">The date value to convert.</param>
 /// <param name="formatString">The format string used to format the date into text.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 /// <returns>Text representation of the date value.</returns>
 public static string DateToString(
   DateTime value, string formatString, EmptyValue emptyValue)
 {
   if (emptyValue == EmptyValue.MinDate)
   {
     if (value == DateTime.MinValue)
       return string.Empty;
   }
   else
   {
     if (value == DateTime.MaxValue)
       return string.Empty;
   }
   return string.Format("{0:" + formatString + "}", value);
 }
Пример #58
0
 /// <summary>
 /// Creates a new SmartDate object.
 /// </summary>
 /// <param name="value">The initial value of the object.</param>
 /// <param name="emptyValue">Indicates whether an empty date is the min or max date value.</param>
 public SmartDate(DateTime? value, EmptyValue emptyValue)
 {
   _emptyValue = emptyValue;
   _format = null;
   _initialized = false;
   _date = DateTime.MinValue;
   if (value.HasValue)
     Date = value.Value;
 }
Пример #59
0
        /// <summary>
        /// 判断字符串是否能转换为一个Datetime日期,并返回转换成功后的日期
        /// </summary>
        /// <param name="value">要转换的日期字符串</param>
        /// <param name="emptyValue">空日期的取值类型</param>
        /// <param name="result">返回转换成功后的日期</param>        
        private static bool TryStringToDate(string value, EmptyValue emptyValue, ref DateTime result)
        {
            //定义一个临时日期
            DateTime temp;

            #region 如果日期字符串为空,则转成最大或最小的日期
            if (string.IsNullOrEmpty(value))
            {
                //如果空日期的取值类型为MinDate,则转成最小日期,否则最大日期
                if (emptyValue == EmptyValue.MinDate)
                {
                    result = _minDate;
                }
                else
                {
                    result = DateTime.MaxValue;
                }

                //返回true
                return true;
            }
            #endregion

            #region 将日期字符串转换为DateTime
            if (DateTime.TryParse(value, out temp))
            {
                //如果转换成功,则返回true
                result = temp;
                return true;
            }
            #endregion

            #region 如果日期字符串是自定义的值,则进行转换
            //将日期字符串转成大写
            string date = value.Trim().ToLower();
            //转换成今天的日期
            if (date == SmartDateString.SmartDateT || date == SmartDateString.SmartDateToday || date == ".")
            {
                result = DateTime.Now;
                return true;
            }
            //转换成昨天的日期
            if (date == SmartDateString.SmartDateY || date == SmartDateString.SmartDateYesterday || date == "-")
            {
                result = DateTime.Now.AddDays(-1);
                return true;
            }
            //转换成明天的日期
            if (date == SmartDateString.SmartDateTom || date == SmartDateString.SmartDateTomorrow || date == "+")
            {
                result = DateTime.Now.AddDays(1);
                return true;
            }
            #endregion

            //转换失败,返回false
            return false;
        }