Using .NET culture information for Date/Time input

--HG--
branch : 1.4.x
This commit is contained in:
Sebastien Ros
2012-05-21 11:07:36 -07:00
parent d60af14ac6
commit daf188c57e
5 changed files with 15 additions and 30 deletions

View File

@@ -5,4 +5,4 @@ ff3ea8b4174d011d030f561209f34191fb171889 src/Orchard.Web/Modules/Orchard.Rules
b2e564aeb77515626fc504ad1093514a68b32f5f src/Orchard.Web/Modules/Orchard.Tokens b2e564aeb77515626fc504ad1093514a68b32f5f src/Orchard.Web/Modules/Orchard.Tokens
4ed51e0e76c2aacc2de90ce9984fd00cfdfae2ce src/orchard.web/Modules/Orchard.Alias 4ed51e0e76c2aacc2de90ce9984fd00cfdfae2ce src/orchard.web/Modules/Orchard.Alias
4f643eeb97d77e1cf9d2be31d28c99ee2658fee6 src/orchard.web/Modules/Orchard.Projections 4f643eeb97d77e1cf9d2be31d28c99ee2658fee6 src/orchard.web/Modules/Orchard.Projections
5dd65a4847f217d7b02bc38386287e758d0fe566 src/orchard.web/modules/Orchard.Fields cd596804ecb3476bfeb89184342d88562bdaf39b src/orchard.web/modules/Orchard.Fields

View File

@@ -3,18 +3,14 @@ using System.Globalization;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Drivers;
using Orchard.Core.Common.Models; using Orchard.Core.Common.Models;
using Orchard.Core.Shapes.Localization;
using Orchard.Localization; using Orchard.Localization;
namespace Orchard.Core.Common.DateEditor { namespace Orchard.Core.Common.DateEditor {
public class DateEditorDriver : ContentPartDriver<CommonPart> { public class DateEditorDriver : ContentPartDriver<CommonPart> {
private readonly IDateTimeLocalization _dateTimeLocalization;
private readonly Lazy<CultureInfo> _cultureInfo; private readonly Lazy<CultureInfo> _cultureInfo;
public DateEditorDriver( public DateEditorDriver(
IOrchardServices services, IOrchardServices services) {
IDateTimeLocalization dateTimeLocalization) {
_dateTimeLocalization = dateTimeLocalization;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
Services = services; Services = services;
@@ -61,8 +57,8 @@ namespace Orchard.Core.Common.DateEditor {
// date and time are formatted using the same patterns as DateTimePicker is, preventing other cultures issues // date and time are formatted using the same patterns as DateTimePicker is, preventing other cultures issues
var createdLocal = TimeZoneInfo.ConvertTimeFromUtc(part.CreatedUtc.Value, Services.WorkContext.CurrentTimeZone); var createdLocal = TimeZoneInfo.ConvertTimeFromUtc(part.CreatedUtc.Value, Services.WorkContext.CurrentTimeZone);
model.CreatedDate = createdLocal.ToString(_dateTimeLocalization.ShortDateFormat.Text, _cultureInfo.Value); model.CreatedDate = createdLocal.ToString("d", _cultureInfo.Value);
model.CreatedTime = createdLocal.ToString(_dateTimeLocalization.ShortTimeFormat.Text, _cultureInfo.Value); model.CreatedTime = createdLocal.ToString("t", _cultureInfo.Value);
} }
} }
@@ -73,10 +69,9 @@ namespace Orchard.Core.Common.DateEditor {
DateTime createdUtc; DateTime createdUtc;
string parseDateTime = String.Concat(model.CreatedDate, " ", model.CreatedTime); string parseDateTime = String.Concat(model.CreatedDate, " ", model.CreatedTime);
var dateTimeFormat = _dateTimeLocalization.ShortDateFormat + " " + _dateTimeLocalization.ShortTimeFormat;
// use current culture // use current culture
if (DateTime.TryParseExact(parseDateTime, dateTimeFormat, _cultureInfo.Value, DateTimeStyles.None, out createdUtc)) { if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out createdUtc)) {
// the date time is entered locally for the configured timezone // the date time is entered locally for the configured timezone
part.CreatedUtc = TimeZoneInfo.ConvertTimeToUtc(createdUtc, Services.WorkContext.CurrentTimeZone); part.CreatedUtc = TimeZoneInfo.ConvertTimeToUtc(createdUtc, Services.WorkContext.CurrentTimeZone);

View File

@@ -6,7 +6,6 @@ using Orchard.ArchiveLater.ViewModels;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers; using Orchard.ContentManagement.Handlers;
using Orchard.Core.Shapes.Localization;
using Orchard.Localization; using Orchard.Localization;
using System.Globalization; using System.Globalization;
@@ -14,15 +13,12 @@ namespace Orchard.ArchiveLater.Drivers {
public class ArchiveLaterPartDriver : ContentPartDriver<ArchiveLaterPart> { public class ArchiveLaterPartDriver : ContentPartDriver<ArchiveLaterPart> {
private const string TemplateName = "Parts/ArchiveLater"; private const string TemplateName = "Parts/ArchiveLater";
private readonly IArchiveLaterService _archiveLaterService; private readonly IArchiveLaterService _archiveLaterService;
private readonly IDateTimeLocalization _dateTimeLocalization;
private readonly Lazy<CultureInfo> _cultureInfo; private readonly Lazy<CultureInfo> _cultureInfo;
public ArchiveLaterPartDriver( public ArchiveLaterPartDriver(
IOrchardServices services, IOrchardServices services,
IArchiveLaterService archiveLaterService, IArchiveLaterService archiveLaterService) {
IDateTimeLocalization dateTimeLocalization) {
_archiveLaterService = archiveLaterService; _archiveLaterService = archiveLaterService;
_dateTimeLocalization = dateTimeLocalization;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
Services = services; Services = services;
@@ -51,8 +47,8 @@ namespace Orchard.ArchiveLater.Drivers {
var model = new ArchiveLaterViewModel(part) { var model = new ArchiveLaterViewModel(part) {
ScheduledArchiveUtc = part.ScheduledArchiveUtc.Value, ScheduledArchiveUtc = part.ScheduledArchiveUtc.Value,
ArchiveLater = part.ScheduledArchiveUtc.Value.HasValue, ArchiveLater = part.ScheduledArchiveUtc.Value.HasValue,
ScheduledArchiveDate = part.ScheduledArchiveUtc.Value.HasValue ? localDate.Value.ToString(_dateTimeLocalization.ShortDateFormat.Text, _cultureInfo.Value) : String.Empty, ScheduledArchiveDate = part.ScheduledArchiveUtc.Value.HasValue ? localDate.Value.ToString("d", _cultureInfo.Value) : String.Empty,
ScheduledArchiveTime = part.ScheduledArchiveUtc.Value.HasValue ? localDate.Value.ToString(_dateTimeLocalization.ShortTimeFormat.Text, _cultureInfo.Value) : String.Empty ScheduledArchiveTime = part.ScheduledArchiveUtc.Value.HasValue ? localDate.Value.ToString("t", _cultureInfo.Value) : String.Empty
}; };
return ContentShape("Parts_ArchiveLater_Edit", return ContentShape("Parts_ArchiveLater_Edit",
@@ -66,10 +62,9 @@ namespace Orchard.ArchiveLater.Drivers {
if ( model.ArchiveLater ) { if ( model.ArchiveLater ) {
DateTime scheduled; DateTime scheduled;
var parseDateTime = String.Concat(model.ScheduledArchiveDate, " ", model.ScheduledArchiveTime); var parseDateTime = String.Concat(model.ScheduledArchiveDate, " ", model.ScheduledArchiveTime);
var dateTimeFormat = _dateTimeLocalization.ShortDateFormat + " " + _dateTimeLocalization.ShortTimeFormat;
// use an english culture as it is the one used by jQuery.datepicker by default // use an english culture as it is the one used by jQuery.datepicker by default
if (DateTime.TryParseExact(parseDateTime, dateTimeFormat, _cultureInfo.Value, DateTimeStyles.None, out scheduled)) { if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out scheduled)) {
// the date time is entered locally for the configured timezone // the date time is entered locally for the configured timezone
var timeZone = Services.WorkContext.CurrentTimeZone; var timeZone = Services.WorkContext.CurrentTimeZone;

View File

@@ -3,7 +3,6 @@ using System.Xml;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers; using Orchard.ContentManagement.Handlers;
using Orchard.Core.Shapes.Localization;
using Orchard.Mvc; using Orchard.Mvc;
using Orchard.PublishLater.Models; using Orchard.PublishLater.Models;
using Orchard.PublishLater.Services; using Orchard.PublishLater.Services;
@@ -18,7 +17,6 @@ namespace Orchard.PublishLater.Drivers {
private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IPublishLaterService _publishLaterService; private readonly IPublishLaterService _publishLaterService;
private readonly IClock _clock; private readonly IClock _clock;
private readonly IDateTimeLocalization _dateTimeLocalization;
private readonly Lazy<CultureInfo> _cultureInfo; private readonly Lazy<CultureInfo> _cultureInfo;
@@ -26,12 +24,10 @@ namespace Orchard.PublishLater.Drivers {
IOrchardServices services, IOrchardServices services,
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IPublishLaterService publishLaterService, IPublishLaterService publishLaterService,
IClock clock, IClock clock) {
IDateTimeLocalization dateTimeLocalization) {
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_publishLaterService = publishLaterService; _publishLaterService = publishLaterService;
_clock = clock; _clock = clock;
_dateTimeLocalization = dateTimeLocalization;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
Services = services; Services = services;
@@ -62,8 +58,8 @@ namespace Orchard.PublishLater.Drivers {
var localDate = new Lazy<DateTime>( () => TimeZoneInfo.ConvertTimeFromUtc(part.ScheduledPublishUtc.Value.Value, Services.WorkContext.CurrentTimeZone)); var localDate = new Lazy<DateTime>( () => TimeZoneInfo.ConvertTimeFromUtc(part.ScheduledPublishUtc.Value.Value, Services.WorkContext.CurrentTimeZone));
var model = new PublishLaterViewModel(part) { var model = new PublishLaterViewModel(part) {
ScheduledPublishUtc = part.ScheduledPublishUtc.Value, ScheduledPublishUtc = part.ScheduledPublishUtc.Value,
ScheduledPublishDate = part.ScheduledPublishUtc.Value.HasValue && !part.IsPublished() ? localDate.Value.ToString(_dateTimeLocalization.ShortDateFormat.Text, _cultureInfo.Value) : String.Empty, ScheduledPublishDate = part.ScheduledPublishUtc.Value.HasValue && !part.IsPublished() ? localDate.Value.ToString("d", _cultureInfo.Value) : String.Empty,
ScheduledPublishTime = part.ScheduledPublishUtc.Value.HasValue && !part.IsPublished() ? localDate.Value.ToString(_dateTimeLocalization.ShortTimeFormat.Text, _cultureInfo.Value) : String.Empty, ScheduledPublishTime = part.ScheduledPublishUtc.Value.HasValue && !part.IsPublished() ? localDate.Value.ToString("t", _cultureInfo.Value) : String.Empty,
}; };
return ContentShape("Parts_PublishLater_Edit", return ContentShape("Parts_PublishLater_Edit",
@@ -80,10 +76,9 @@ namespace Orchard.PublishLater.Drivers {
DateTime scheduled; DateTime scheduled;
string parseDateTime = String.Concat(model.ScheduledPublishDate, " ", model.ScheduledPublishTime); string parseDateTime = String.Concat(model.ScheduledPublishDate, " ", model.ScheduledPublishTime);
var dateTimeFormat = _dateTimeLocalization.ShortDateFormat + " " + _dateTimeLocalization.ShortTimeFormat;
// use current culture // use current culture
if (DateTime.TryParseExact(parseDateTime, dateTimeFormat, _cultureInfo.Value, DateTimeStyles.None, out scheduled)) { if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out scheduled)) {
// the date time is entered locally for the configured timezone // the date time is entered locally for the configured timezone
var timeZone = Services.WorkContext.CurrentTimeZone; var timeZone = Services.WorkContext.CurrentTimeZone;

View File

@@ -25,8 +25,8 @@
// convert .NET format into jQuery format // convert .NET format into jQuery format
// http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx // http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
// http://docs.jquery.com/UI/Datepicker/formatDate // http://docs.jquery.com/UI/Datepicker/formatDate
var dateFormat = dateTimeLocalization.ShortDateFormat.Text var dateFormat = cultureInfo.DateTimeFormat.ShortDatePattern
.Replace("yyyy", "YY") .Replace("yyyy", "YY")
.Replace("yy", "Y") .Replace("yy", "Y")
.Replace("Y", "y") .Replace("Y", "y")