Esempio n. 1
0
 /// <summary>
 /// DateOffset as Offset with Unit, i.e. "1 день"
 /// </summary>
 public static string GetOffsetUnitString(DateOffset d)
 {
     if (d == null || d.IsEmpty)
     {
         return(string.Empty);
     }
     return(string.Format("{0} {1}", d.Offset, GetUnitString(d.Offset, d.Unit)));
 }
Esempio n. 2
0
        public void FillDateAndNowFrom(DateOffset d)
        {
            Contract.Requires(d != null);

            SetDate(d.Year, d.Month, d.Day);
            OnPropertyChanged("Year", "Month", "Day");

            Now = d.Now;
        }
Esempio n. 3
0
        /// <summary>
        /// Установка даты меняет единицу измерения и смещение на наиболее подходящие.
        /// </summary>
        public static DateOffset RoundOffsetUnitByDate(this DateOffset d, DateTime described)
        {
            Contract.Requires(d.Year != null);
            Contract.Ensures(d.Equals(Contract.OldValue(d)));

            int?     offset = null;
            DateUnit unit   = 0;

            Action setRoundedOffsetUnitMonthOrYear = () =>
            {
                var months = DateHelper.GetTotalMonthsBetween(described, d.Year.Value, d.Month.Value);
                if (months < 12) // меньше года - месяцы
                {
                    offset = months;
                    unit   = DateUnit.Month;
                }
                else
                {
                    offset = described.Year - d.Year.Value;
                    unit   = DateUnit.Year;
                }
            };

            if (d.Month == null) // _ _ y (или d _ y без автообрезания)
            {
                offset = described.Year - d.Year.Value;
                unit   = DateUnit.Year;
            }
            else if (d.Day == null) // _ m y
            {
                setRoundedOffsetUnitMonthOrYear();
            }
            else // d m y
            {
                var days = (described - (DateTime)d).Days;
                if (days < 7) // меньше недели - дни
                {
                    offset = days;
                    unit   = DateUnit.Day;
                }
                else if (days < 4 * 7) // меньше месяца - недели
                {
                    offset = days / 7;
                    unit   = DateUnit.Week;
                }
                else
                {
                    setRoundedOffsetUnitMonthOrYear();
                }
            }

            return(new DateOffset(offset, unit, () => d.Now));
        }
Esempio n. 4
0
 /// <summary>
 /// DateOffset as partial DateTime, i.e. "2014"
 /// </summary>
 public static string GetPartialDateString(DateOffset d)
 {
     if (d == null || d.IsEmpty || d.Year == null)
     {
         return(string.Empty);
     }
     if (!d.Month.HasValue) // year
     {
         return(d.Year.ToString());
     }
     if (!d.Day.HasValue) // month year
     {
         return(System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames[d.Month.Value - 1].ToLower() + " " + d.Year.ToString());
     }
     return(((DateTime)d).ToString("d MMMM yyyy")); // full
 }
Esempio n. 5
0
        /// <summary>
        /// Округляет смещение.
        /// При укрупнении единицы смещение считается для полной даты с 1 вместо отсутствующих значений.
        /// </summary>
        public static int?RoundOffsetFor(this DateOffset d, DateUnit unit)
        {
            Contract.Requires(d != null);
            Contract.Ensures(d.Equals(Contract.OldValue(d)));
            Contract.Ensures(!d.IsEmpty || Contract.Result <int?>() == null);

            if (!d.Year.HasValue)
            {
                return(null);
            }
            int?roundedOffset;

            switch (unit)
            {
            case DateUnit.Day:
                roundedOffset = (d.Now - d.GetSortingDate()).Days;
                break;

            case DateUnit.Week:
                roundedOffset = (d.Now - d.GetSortingDate()).Days / 7;
                break;

            case DateUnit.Month:
                if (d.Month.HasValue)
                {
                    roundedOffset = DateHelper.GetTotalMonthsBetween(d.Now, d.Year.Value, d.Month.Value);
                }
                else
                {
                    roundedOffset = DateHelper.GetTotalMonthsBetween(d.Now, d.Year.Value, 1);
                }
                break;

            case DateUnit.Year:
                roundedOffset = d.Now.Year - d.Year.Value;
                break;

            default:
                throw new NotImplementedException();
            }
            return(roundedOffset);
        }
Esempio n. 6
0
        public DateOffset RelativeTo(DateOffset d)
        {
            Contract.Requires(d != null);

            var max = this >= d ? this : d;
            var min = this < d ? this : d;
            var com = CommonPartWith(d);

            switch (com)
            {
            case DateUnit.Year:
                return(new DateOffset(min.Year, null, null, () => max.GetSortingDate()));

            case DateUnit.Month:
                return(new DateOffset(min.Year, min.Month, null, () => max.GetSortingDate()));

            case DateUnit.Day:
                return(new DateOffset(min.Year, min.Month, min.Day, () => max.GetSortingDate()));

            default:
                return(new DateOffset(null, null, null, () => max.GetSortingDate()));
            }
        }
Esempio n. 7
0
        private DateUnit?CommonPartWith(DateOffset d)
        {
            DateUnit?commonPart = null;

            if (Year != null && d.Year != null)
            {
                if (Month != null && d.Month != null)
                {
                    if (Day != null && d.Day != null)
                    {
                        commonPart = DateUnit.Day;
                    }
                    else
                    {
                        commonPart = DateUnit.Month;
                    }
                }
                else
                {
                    commonPart = DateUnit.Year;
                }
            }
            return(commonPart);
        }
Esempio n. 8
0
 public DateOffset(DateOffset d)
     : this(d.Year, d.Month, d.Day, () => d.Now)
 {
     Contract.Requires(d != null);
 }