Adding TimeZone

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-01-05 14:49:10 -08:00
parent 07dc0197ad
commit b1dc3b21c1
28 changed files with 351 additions and 56 deletions

View File

@@ -23,12 +23,12 @@ namespace Orchard.Core.Common {
}
[Shape]
public IHtmlString PublishedState(HtmlHelper html, DateTime createdDateTimeUtc, DateTime? publisheddateTimeUtc) {
public IHtmlString PublishedState(dynamic Display, DateTime createdDateTimeUtc, DateTime? publisheddateTimeUtc) {
if (!publisheddateTimeUtc.HasValue) {
return T("Draft");
}
return html.DateTime(createdDateTimeUtc);
return Display.DateTime(DateTimeUtc: createdDateTimeUtc);
}
[Shape]

View File

@@ -1,4 +1,5 @@
using System.Net;
using System.Linq;
using System.Net;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
@@ -44,7 +45,8 @@ namespace Orchard.Core.Settings.Drivers {
var model = new SiteSettingsPartViewModel {
Site = site,
SiteCultures = _cultureManager.ListCultures()
SiteCultures = _cultureManager.ListCultures(),
TimeZones = TimeZoneInfo.GetSystemTimeZones()
};
return ContentShape("Parts_Settings_SiteSettingsPart",
@@ -55,7 +57,8 @@ namespace Orchard.Core.Settings.Drivers {
var site = _siteService.GetSiteSettings().As<SiteSettingsPart>();
var model = new SiteSettingsPartViewModel {
Site = site,
SiteCultures = _cultureManager.ListCultures()
SiteCultures = _cultureManager.ListCultures(),
TimeZones = TimeZoneInfo.GetSystemTimeZones()
};
var previousBaseUrl = model.Site.BaseUrl;

View File

@@ -91,9 +91,16 @@ namespace Orchard.Core.Settings {
.Column<string>("SiteCulture")
.Column<string>("ResourceDebugMode", c => c.WithDefault("FromAppSetting"))
.Column<int>("PageSize")
.Column<string>("SiteTimeZone")
);
return 1;
SchemaBuilder.CreateTable("SiteSettings2PartRecord",
table => table
.ContentPartRecord()
.Column<string>("BaseUrl", c => c.Unlimited())
);
return 3;
}
public int UpdateFrom1() {
@@ -105,5 +112,14 @@ namespace Orchard.Core.Settings {
return 2;
}
public int UpdateFrom2() {
SchemaBuilder.AlterTable("SiteSettingsPartRecord",
table => table
.AddColumn<string>("SiteTimeZone")
);
return 3;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.Data.Conventions;
using Orchard.Settings;
@@ -45,6 +46,11 @@ namespace Orchard.Core.Settings.Models {
set { Record.PageSize = value; }
}
public string SiteTimeZone {
get { return Record.SiteTimeZone; }
set { Record.SiteTimeZone = value; }
}
[StringLengthMax]
public string BaseUrl {
get {

View File

@@ -1,4 +1,5 @@
using Orchard.ContentManagement.Records;
using System;
using Orchard.ContentManagement.Records;
using Orchard.Settings;
namespace Orchard.Core.Settings.Models {
@@ -24,5 +25,7 @@ namespace Orchard.Core.Settings.Models {
public virtual ResourceDebugMode ResourceDebugMode { get; set; }
public virtual int PageSize { get; set; }
public virtual string SiteTimeZone { get; set; }
}
}

View File

@@ -36,6 +36,7 @@ namespace Orchard.Core.Settings.Services {
item.Record.SiteSalt = Guid.NewGuid().ToString("N");
item.Record.SiteName = "My Orchard Project Application";
item.Record.PageTitleSeparator = " - ";
item.Record.SiteTimeZone = TimeZoneInfo.Local.Id;
}).ContentItem;
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Orchard.Core.Settings.Models;
using Orchard.Settings;
@@ -7,6 +8,7 @@ namespace Orchard.Core.Settings.ViewModels {
public class SiteSettingsPartViewModel {
public SiteSettingsPart Site { get; set; }
public IEnumerable<string> SiteCultures { get; set; }
public IEnumerable<TimeZoneInfo> TimeZones { get; set; }
[HiddenInput(DisplayValue = false)]
public int Id {
@@ -47,5 +49,10 @@ namespace Orchard.Core.Settings.ViewModels {
get { return Site.BaseUrl; }
set { Site.BaseUrl = value; }
}
public string TimeZone {
get { return Site.SiteTimeZone; }
set { Site.SiteTimeZone = value; }
}
}
}

View File

@@ -26,6 +26,12 @@
@Html.ValidationMessage("SiteCulture", "*")
<p>@Html.ActionLink(T("Add or remove supported cultures for the site.").ToString(), "Culture")</p>
</div>
<div>
<label for="TimeZone">@T("Default Time Zone")</label>
@Html.DropDownList("TimeZone", new[] { new SelectListItem { Text = T("Local to server").Text, Value = "" } }.Union(new SelectList(Model.TimeZones, "Id", "", Model.TimeZone)))
@Html.ValidationMessage("TimeZone", "*")
<span class="hint">@T("Determines the default time zone which will should be used to display date and times.")</span>
</div>
<div>
<label for="PageTitleSeparator">@T("Page title separator")</label>
@Html.EditorFor(x => x.PageTitleSeparator)

View File

@@ -7,22 +7,27 @@ using Orchard.Mvc.Html;
using Orchard.Services;
namespace Orchard.Core.Shapes {
public class DateTimeShapes : ISingletonDependency {
public class DateTimeShapes : IDependency {
private readonly IClock _clock;
private readonly IWorkContextAccessor _workContextAccessor;
public DateTimeShapes(IClock clock) {
public DateTimeShapes(
IClock clock,
IWorkContextAccessor workContextAccessor
) {
_clock = clock;
_workContextAccessor = workContextAccessor;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
[Shape]
public IHtmlString DateTimeRelative(HtmlHelper Html, DateTime dateTimeUtc) {
public IHtmlString DateTimeRelative(dynamic Display, DateTime dateTimeUtc) {
var time = _clock.UtcNow - dateTimeUtc;
if (time.TotalDays > 7)
return Html.DateTime(dateTimeUtc.ToLocalTime(), T("'on' MMM d yyyy 'at' h:mm tt"));
return Display.DateTime(DateTimeUtc: dateTimeUtc, CustomFormat: T("'on' MMM d yyyy 'at' h:mm tt"));
if (time.TotalHours > 24)
return T.Plural("1 day ago", "{0} days ago", time.Days);
if (time.TotalMinutes > 60)
@@ -34,5 +39,30 @@ namespace Orchard.Core.Shapes {
return T("a moment ago");
}
[Shape]
public IHtmlString DateTime(DateTime DateTimeUtc, LocalizedString CustomFormat) {
//using a LocalizedString forces the caller to use a localizable format
if (CustomFormat == null || String.IsNullOrWhiteSpace(CustomFormat.Text)) {
return DateTime(DateTimeUtc, T("MMM d yyyy h:mm tt"));
}
return new MvcHtmlString(ConvertToDisplayTime(DateTimeUtc).ToString(CustomFormat.Text));
}
/// <summary>
/// Converts a Coordinated Universal Time (UTC) to the time in the current time zone.
/// </summary>
/// <param name="dateTimeUtc">The Coordinated Universal Time (UTC).</param>
/// <returns>The date and time in the selected time zone. Its System.DateTime.Kind property is System.DateTimeKind.Utc if the current zone is System.TimeZoneInfo.Utc; otherwise, its System.DateTime.Kind property is System.DateTimeKind.Unspecified.</returns>
private DateTime ConvertToDisplayTime(DateTime dateTimeUtc) {
// get the time zone for the current request
var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
return TimeZoneInfo.ConvertTimeFromUtc(dateTimeUtc, timeZone);
}
}
}