Ejemplo n.º 1
0
        public DateTime?ConvertFromLocalizedString(string dateTimeString, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            if (dateTimeString == null || dateTimeString == options.NullText)
            {
                return(null);
            }

            var parts = _dateFormatter.ParseDateTime(dateTimeString);

            DateTime dateValue;

            if (options.EnableCalendarConversion && !(CurrentCalendar is GregorianCalendar))
            {
                dateValue = ConvertFromSiteCalendar(parts);
            }
            else
            {
                dateValue = parts.ToDateTime(new GregorianCalendar());
            }

            if (options.EnableTimeZoneConversion)
            {
                dateValue = ConvertFromSiteTimeZone(dateValue);
            }

            return(dateValue);
        }
Ejemplo n.º 2
0
        public string ConvertToLocalizedString(DateTime?date, string format, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            if (!date.HasValue)
            {
                return(options.NullText);
            }

            var dateValue = date.Value;
            var offset    = TimeSpan.Zero;

            if (options.EnableTimeZoneConversion)
            {
                dateValue = ConvertToSiteTimeZone(dateValue);
                offset    = CurrentTimeZone.GetUtcOffset(date.Value);
            }

            var parts = DateTimeParts.FromDateTime(dateValue, offset);

            if (options.EnableCalendarConversion && !(CurrentCalendar is GregorianCalendar))
            {
                parts = ConvertToSiteCalendar(dateValue, offset);
            }

            return(_dateFormatter.FormatDateTime(parts, format));
        }
Ejemplo n.º 3
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new DateTimeFieldViewModel();

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();

                var options = new DateLocalizationOptions();

                // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    options.EnableTimeZoneConversion = false;
                }

                // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    options.EnableCalendarConversion = false;
                    options.IgnoreDate = true;
                }

                var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
                var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

                DateTime?value = null;

                // Try to parse data if not required or if there are no missing fields.
                if (!settings.Required || ((!showDate || !String.IsNullOrWhiteSpace(viewModel.Editor.Date)) && (!showTime || !String.IsNullOrWhiteSpace(viewModel.Editor.Time))))
                {
                    try {
                        value = DateLocalizationServices.ConvertFromLocalizedString(viewModel.Editor.Date, viewModel.Editor.Time, options);
                    }
                    catch {
                        updater.AddModelError(GetPrefix(field, part), T("{0} could not be parsed as a valid date and time.", T(field.DisplayName)));
                    }
                }

                // Hackish workaround to make sure a time-only field with an entered time equivalent to
                // 00:00 UTC doesn't get stored as a full DateTime.MinValue in the database, resulting
                // in it being interpreted as an empty value when subsequently retrieved.
                if (value.HasValue && settings.Display == DateTimeFieldDisplays.TimeOnly && value == DateTime.MinValue)
                {
                    value = value.Value.AddDays(1);
                }

                if (settings.Required && (!value.HasValue || (settings.Display != DateTimeFieldDisplays.TimeOnly && value.Value.Date == DateTime.MinValue)))
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is required.", T(field.DisplayName)));
                }

                field.DateTime = value.HasValue ? value.Value : DateTime.MinValue;
            }

            return(Editor(part, field, shapeHelper));
        }
 public Tuple <DateTime, DateTime> ParseDateRange(string datesString)
 {
     if (!string.IsNullOrEmpty(datesString))
     {
         var options = new DateLocalizationOptions {
             EnableTimeZoneConversion = false
         };
         var dateStrings = datesString.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
         try {
             return(new Tuple <DateTime, DateTime>(_dateLocalizationServices.ConvertFromLocalizedString(dateStrings[0], "00:00:00 AM", options).GetValueOrDefault(), _dateLocalizationServices.ConvertFromLocalizedString(dateStrings[1], "00:00:00 AM", options).GetValueOrDefault()));
         }
         catch (FormatException) {
             return(null);
         }
     }
     return(null);
 }
Ejemplo n.º 5
0
        protected override DriverResult Display(ContentPart part, DateTimeField field, string displayType, dynamic shapeHelper)
        {
            return(ContentShape("Fields_DateTime", // this is just a key in the Shape Table
                                GetDifferentiator(field, part),
                                () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                var value = field.DateTime;
                var options = new DateLocalizationOptions();

                // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    options.EnableTimeZoneConversion = false;
                }

                // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    options.EnableCalendarConversion = false;
                    options.IgnoreDate = true;
                }

                var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
                var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

                var viewModel = new DateTimeFieldViewModel {
                    Name = field.DisplayName,
                    Hint = settings.Hint,
                    IsRequired = settings.Required,
                    Editor = new DateTimeEditor()
                    {
                        Date = showDate ? DateLocalizationServices.ConvertToLocalizedDateString(value, options) : null,
                        Time = showTime ? DateLocalizationServices.ConvertToLocalizedTimeString(value, options) : null,
                        ShowDate = showDate,
                        ShowTime = showTime,
                        DatePlaceholder = settings.DatePlaceholder,
                        TimePlaceholder = settings.TimePlaceholder
                    }
                };

                return shapeHelper.Fields_DateTime(     // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
                    Model: viewModel);
            }
                                ));
        }
        public ActionResult Create()
        {
            var now = _clock.UtcNow;
            var localizationOptions = new DateLocalizationOptions {
                EnableTimeZoneConversion = true
            };
            var viewModel = new CustomSitemapEntryEditViewModel
            {
                LastModifiedUtc = new DateTimeEditor
                {
                    Date     = _dateLocalizationServices.ConvertToLocalizedDateString(now, localizationOptions),
                    Time     = _dateLocalizationServices.ConvertToLocalizedTimeString(now, localizationOptions),
                    ShowDate = true,
                    ShowTime = true
                }
            };

            return(View(viewModel));
        }
Ejemplo n.º 7
0
        public string ConvertToLocalizedTimeString(DateTime?date, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            if (!date.HasValue)
            {
                return(options.NullText);
            }

            var dateValue = date.Value;
            var offset    = TimeSpan.Zero;

            if (options.EnableTimeZoneConversion)
            {
                if (options.IgnoreDate)
                {
                    // The caller has asked us to ignore the date part. This usually because the source
                    // is a time-only field. In such cases (with an undefined date) it does not make sense
                    // to consider DST variations throughout the year, so we will use an arbitrary (but fixed)
                    // non-DST date for the conversion to ensure DST is never applied during conversion. The
                    // date part is usually DateTime.MinValue which we should not use because time zone
                    // conversion cannot wrap DateTime.MinValue around to the previous day, resulting in
                    // an undefined result. Instead we convert the date to a hard-coded date of 2000-01-01
                    // before the conversion, and back to the original date after.
                    var tempDate = new DateTime(2000, 1, 1, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                    tempDate  = ConvertToSiteTimeZone(tempDate);
                    dateValue = new DateTime(dateValue.Year, dateValue.Month, dateValue.Day, tempDate.Hour, tempDate.Minute, tempDate.Second, tempDate.Millisecond, tempDate.Kind);
                }
                else
                {
                    dateValue = ConvertToSiteTimeZone(dateValue);
                }

                offset = CurrentTimeZone.GetUtcOffset(date.Value);
            }

            var parts = DateTimeParts.FromDateTime(dateValue, offset);

            // INFO: No calendar conversion in this method - we expect the date component to be DateTime.MinValue and irrelevant anyway.

            return(_dateFormatter.FormatDateTime(parts, _dateTimeFormatProvider.LongTimeFormat));
        }
Ejemplo n.º 8
0
        public string ConvertToLocalizedTimeString(DateTime?date, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            if (!date.HasValue)
            {
                return(options.NullText);
            }

            var dateValue = date.Value;
            var offset    = TimeSpan.Zero;

            if (options.EnableTimeZoneConversion)
            {
                if (options.IgnoreDate)
                {
                    // The caller has asked us to ignore the date part and assume it is today. This usually because the source
                    // is a time-only field, in which case the date part is usually DateTime.MinValue which we should not use
                    // for the following reasons:
                    // * DST can be active or not dependeng on the time of the year. We want the conversion to always act as if the time represents today, but we don't want that date stored.
                    // * Time zone conversion cannot wrap DateTime.MinValue around to the previous day, resulting in undefined result.
                    // Therefore we convert the date to today's date before the conversion, and back to the original date after.
                    var today    = _clock.UtcNow.Date;
                    var tempDate = new DateTime(today.Year, today.Month, today.Day, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                    tempDate  = ConvertToSiteTimeZone(tempDate);
                    dateValue = new DateTime(dateValue.Year, dateValue.Month, dateValue.Day, tempDate.Hour, tempDate.Minute, tempDate.Second, tempDate.Millisecond, tempDate.Kind);
                }
                else
                {
                    dateValue = ConvertToSiteTimeZone(dateValue);
                }

                offset = CurrentTimeZone.GetUtcOffset(date.Value);
            }

            var parts = DateTimeParts.FromDateTime(dateValue, offset);

            // INFO: No calendar conversion in this method - we expect the date component to be DateTime.MinValue and irrelevant anyway.

            return(_dateFormatter.FormatDateTime(parts, _dateTimeFormatProvider.LongTimeFormat));
        }
Ejemplo n.º 9
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;
            var options  = new DateLocalizationOptions();

            // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
            if (settings.Display == DateTimeFieldDisplays.DateOnly)
            {
                options.EnableTimeZoneConversion = false;
            }

            // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
            if (settings.Display == DateTimeFieldDisplays.TimeOnly)
            {
                options.EnableCalendarConversion = false;
                options.IgnoreDate = true;
            }

            var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
            var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

            var viewModel = new DateTimeFieldViewModel {
                Name            = field.DisplayName,
                Hint            = settings.Hint,
                IsRequired      = settings.Required,
                HasDefaultValue = settings.DefaultValue.HasValue,
                Editor          = new DateTimeEditor()
                {
                    Date            = showDate ? DateLocalizationServices.ConvertToLocalizedDateString(value, options) : null,
                    Time            = showTime ? DateLocalizationServices.ConvertToLocalizedTimeString(value, options) : null,
                    ShowDate        = showDate,
                    ShowTime        = showTime,
                    DatePlaceholder = settings.DatePlaceholder,
                    TimePlaceholder = settings.TimePlaceholder
                }
            };

            return(ContentShape("Fields_DateTime_Edit", GetDifferentiator(field, part),
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }
 public DateTime[] ParseMultiDates(string datesString)
 {
     if (!string.IsNullOrEmpty(datesString))
     {
         var options = new DateLocalizationOptions {
             EnableTimeZoneConversion = false
         };
         var dateStrings = datesString.Split(',');
         var dates       = new DateTime[dateStrings.Length];
         for (var i = 0; i < dateStrings.Length; i++)
         {
             try {
                 dates[i] = _dateLocalizationServices.ConvertFromLocalizedString(dateStrings[i], "00:00:00 AM", options).GetValueOrDefault();
             }
             catch (FormatException) {
                 return(null);
             }
         }
         return(dates);
     }
     return(null);
 }
Ejemplo n.º 11
0
 public string ConvertToLocalizedDateString(DateTime date, DateLocalizationOptions options = null)
 {
     return(ConvertToLocalizedDateString(ToNullable(date), options));
 }
Ejemplo n.º 12
0
        public DateTime?ConvertFromLocalizedString(string dateString, string timeString, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            var hasDate = dateString != null && dateString != options.NullText;
            var hasTime = timeString != null && timeString != options.NullText;

            if (!hasDate && !hasTime)
            {
                return(null);
            }

            var parts = new DateTimeParts(
                hasDate ? _dateFormatter.ParseDate(dateString) : DateParts.MinValue,
                hasTime ? _dateFormatter.ParseTime(timeString) : TimeParts.MinValue
                );

            DateTime dateValue;

            if (hasDate && options.EnableCalendarConversion && !(CurrentCalendar is GregorianCalendar))
            {
                dateValue = ConvertFromSiteCalendar(parts);
            }
            else
            {
                dateValue = parts.ToDateTime(new GregorianCalendar());
            }

            if (hasTime && options.EnableTimeZoneConversion)
            {
                // If there is no date component (technically the date component is that of DateTime.MinValue)
                // then we must employ some trickery. With an undefined date it does not make sense
                // to consider DST variations throughout the year, so we will use an arbitrary (but fixed)
                // non-DST date for the conversion to ensure DST is never applied during conversion. The
                // date part is usually DateTime.MinValue which we should not use because time zone
                // conversion cannot wrap DateTime.MinValue around to the previous day, resulting in
                // an undefined result. Instead we convert the date to a hard-coded date of 2000-01-01
                // before the conversion, and back to the original date after.
                if (!hasDate)
                {
                    dateValue = new DateTime(2000, 1, 1, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                }
                dateValue = ConvertFromSiteTimeZone(dateValue);
                if (!hasDate)
                {
                    dateValue = new DateTime(DateTime.MinValue.Year, DateTime.MinValue.Month, DateTime.MinValue.Day, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                }
            }

            if (options.EnableTimeZoneConversion)
            {
                dateValue = DateTime.SpecifyKind(dateValue, DateTimeKind.Utc);
            }

            return(dateValue);
        }
Ejemplo n.º 13
0
 public DateTime?ConvertFromLocalizedTimeString(string timeString, DateLocalizationOptions options = null)
 {
     return(ConvertFromLocalizedString(null, timeString, options));
 }
Ejemplo n.º 14
0
 public DateTime?ConvertFromLocalizedDateString(string dateString, DateLocalizationOptions options = null)
 {
     return(ConvertFromLocalizedString(dateString, null, options));
 }
Ejemplo n.º 15
0
 public string ConvertToLocalizedString(DateTime?date, DateLocalizationOptions options = null)
 {
     return(ConvertToLocalizedString(date, _dateTimeFormatProvider.ShortDateTimeFormat, options));
 }
Ejemplo n.º 16
0
        public DateTime?ConvertFromLocalizedString(string dateString, string timeString, DateLocalizationOptions options = null)
        {
            options = options ?? new DateLocalizationOptions();

            var hasDate = dateString != null && dateString != options.NullText;
            var hasTime = timeString != null && timeString != options.NullText;

            if (!hasDate && !hasTime)
            {
                return(null);
            }

            var parts = new DateTimeParts(
                hasDate ? _dateFormatter.ParseDate(dateString) : DateParts.MinValue,
                hasTime ? _dateFormatter.ParseTime(timeString) : TimeParts.MinValue
                );

            DateTime dateValue;

            if (hasDate && options.EnableCalendarConversion && !(CurrentCalendar is GregorianCalendar))
            {
                dateValue = ConvertFromSiteCalendar(parts);
            }
            else
            {
                dateValue = parts.ToDateTime(new GregorianCalendar());
            }

            if (hasTime && options.EnableTimeZoneConversion)
            {
                // If there is no date component (technically the date component is that of DateTime.MinValue) then
                // we must employ some trickery, for two reasons:
                // * DST can be active or not dependeng on the time of the year. We want the conversion to always act as if the time represents today, but we don't want that date stored.
                // * Time zone conversion cannot wrap DateTime.MinValue around to the previous day, resulting in undefined result.
                // Therefore we convert the date to today's date before the conversion, and back to DateTime.MinValue after.
                if (!hasDate)
                {
                    var now = _clock.UtcNow;
                    dateValue = new DateTime(now.Year, now.Month, now.Day, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                }
                dateValue = ConvertFromSiteTimeZone(dateValue);
                if (!hasDate)
                {
                    dateValue = new DateTime(DateTime.MinValue.Year, DateTime.MinValue.Month, DateTime.MinValue.Day, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind);
                }
            }

            return(dateValue);
        }