/// <summary>
 /// Get the local time from its UTC representation.
 /// </summary>
 /// <param name="utcDate">UTC-based time.</param>
 /// <returns>Local time.</returns>
 public DateTime DateToLocal(DateTime?utcDate)
 {
     if (utcDate == null || utcDate.Value <= (DateTime)SqlDateTime.MinValue)
     {
         utcDate = SqlDateTime.MinValue.Value.AddDays(1);
     }
     else if (utcDate >= (DateTime)SqlDateTime.MaxValue)
     {
         utcDate = SqlDateTime.MaxValue.Value.Subtract(TimeSpan.FromDays(1));
     }
     return((DateTime)_dateServices.ConvertToSiteTimeZone(utcDate.Value));
 }
Exemple #2
0
 public void Evaluate(EvaluateContext context)
 {
     context.For("Date", () => _clock.UtcNow)
     // {Date.Since}
     .Token("Since", DateTimeRelative)
     // {Date.Local}
     .Token("Local", d => _dateLocalizationServices.ConvertToLocalizedString(d))
     .Chain("Local", "Date", d => _dateLocalizationServices.ConvertToSiteTimeZone(d))
     // {Date.Short}
     .Token("Short", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.ShortDateTimeFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.ShortDate}
     .Token("ShortDate", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.ShortDateFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.ShortTime}
     .Token("ShortTime", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.ShortTimeFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.Long}
     .Token("Long", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.LongDateTimeFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.LongDate}
     .Token("LongDate", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.LongDateFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.LongTime}
     .Token("LongTime", d => _dateLocalizationServices.ConvertToLocalizedString(d, _dateTimeFormats.LongTimeFormat, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date}
     .Token(
         token => token == String.Empty ? String.Empty : null,
         (token, d) => _dateLocalizationServices.ConvertToLocalizedString(d, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }))
     // {Date.Format:<formatString>}
     .Token(
         token => token.StartsWith("Format:", StringComparison.OrdinalIgnoreCase) ? token.Substring("Format:".Length) : null,
         (token, d) => _dateLocalizationServices.ConvertToLocalizedString(d, token, new DateLocalizationOptions()
     {
         EnableTimeZoneConversion = false
     }));
 }
        public DataTable GenerateDataTable(IEnumerable <Submission> submissions)
        {
            var records     = submissions.Select(x => Tuple.Create(x, x.ToNameValues())).ToArray();
            var columnNames = new HashSet <string>();
            var dataTable   = new DataTable();

            foreach (var key in
                     from record in records
                     from string key in record.Item2 where !columnNames.Contains(key)
                     where !String.IsNullOrWhiteSpace(key)
                     select key)
            {
                columnNames.Add(key);
            }

            dataTable.Columns.Add("Id");
            dataTable.Columns.Add("Created");
            foreach (var columnName in columnNames)
            {
                dataTable.Columns.Add(columnName);
            }

            foreach (var record in records)
            {
                var dataRow = dataTable.NewRow();

                dataRow["Id"]      = record.Item1.Id;
                dataRow["Created"] = _dateLocalizationServices.ConvertToSiteTimeZone(record.Item1.CreatedUtc).ToString(_cultureAccessor.CurrentCulture);
                foreach (var columnName in columnNames)
                {
                    var value = record.Item2[columnName];
                    dataRow[columnName] = value;
                }

                dataTable.Rows.Add(dataRow);
            }

            return(dataTable);
        }
Exemple #4
0
        public void Validating(OrchardForms.ValidatingContext context)
        {
            if (context.FormName != DateTimeFilterForm.FormName)
            {
                return;
            }

            var op = (DateTimeOperator)Enum.Parse(typeof(DateTimeOperator), Convert.ToString(context.ValueProvider.GetValue("Operator").AttemptedValue));

            string   dateFrom    = context.ValueProvider.GetValue("DateFrom").AttemptedValue;
            string   timeFrom    = context.ValueProvider.GetValue("TimeFrom").AttemptedValue;
            string   dateTo      = context.ValueProvider.GetValue("DateTo").AttemptedValue;
            string   timeTo      = context.ValueProvider.GetValue("TimeTo").AttemptedValue;
            var      cultureFrom = CultureInfo.GetCultureInfo(context.ValueProvider.GetValue("CultureFrom").AttemptedValue);
            var      cultureTo   = CultureInfo.GetCultureInfo(context.ValueProvider.GetValue("CultureTo").AttemptedValue);
            var      utcNow      = DateTime.UtcNow;
            DateTime from        = _dateLocalizationServices.ConvertToSiteTimeZone(utcNow);
            DateTime to          = _dateLocalizationServices.ConvertToSiteTimeZone(utcNow);

            switch (op)
            {
            case DateTimeOperator.Between:
                bool parseOk = true;

                try {
                    from = Convert.ToDateTime(dateFrom + " " + timeFrom, cultureFrom);
                } catch {
                    context.ModelState.AddModelError("DateFrom", T("Invalid dates and times").Text);
                    parseOk = false;
                }

                try {
                    to = Convert.ToDateTime(dateTo + " " + timeTo, cultureTo);
                } catch {
                    context.ModelState.AddModelError("DateTo", T("Invalid dates and times").Text);
                    parseOk = false;
                }

                if (parseOk && from > to)
                {
                    context.ModelState.AddModelError("DateFrom", T("First date must be before second date").Text);
                }

                break;

            case DateTimeOperator.LessThan:
                try {
                    to = Convert.ToDateTime(dateTo + " " + timeTo, cultureTo);
                } catch {
                    context.ModelState.AddModelError("DateTo", T("Invalid dates and times").Text);
                }

                break;

            case DateTimeOperator.GreaterThan:
                try {
                    from = Convert.ToDateTime(dateFrom + " " + timeFrom, cultureFrom);
                } catch {
                    context.ModelState.AddModelError("DateFrom", T("Invalid dates and times").Text);
                }

                break;

            default:
                context.ModelState.AddModelError("Operator", T("Invalid operator").Text);
                break;
            }
        }