mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Incremental work on IDateLocalizationServices.
This commit is contained in:
@@ -45,8 +45,8 @@ namespace Orchard.Tokens.Providers {
|
||||
.Token("Since", DateTimeRelative)
|
||||
.Chain("Since", "Date", DateTimeRelative)
|
||||
// {Date.Local}
|
||||
.Token("Local", d => _dateServices.ConvertToLocal(d))
|
||||
.Chain("Local", "Date", d => _dateServices.ConvertToLocal(d))
|
||||
.Token("Local", d => _dateServices.ConvertToSiteTimeZone(d))
|
||||
.Chain("Local", "Date", d => _dateServices.ConvertToSiteTimeZone(d))
|
||||
// {Date.ShortDate}
|
||||
.Token("ShortDate", d => d.ToString(_dateTimeLocalization.ShortDateFormat, _cultureInfo.Value))
|
||||
// {Date.ShortTime}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Framework.Localization.Models;
|
||||
using Orchard.Settings;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
@@ -20,11 +21,11 @@ namespace Orchard.Localization.Services {
|
||||
_calendarManager = calendarManager;
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertToLocal(DateTime date) {
|
||||
return ConvertToLocal(ToNullable(date));
|
||||
public virtual DateTime? ConvertToSiteTimeZone(DateTime date) {
|
||||
return ConvertToSiteTimeZone(ToNullable(date));
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertToLocal(DateTime? date) {
|
||||
public virtual DateTime? ConvertToSiteTimeZone(DateTime? date) {
|
||||
if (!date.HasValue) {
|
||||
return null;
|
||||
}
|
||||
@@ -32,6 +33,43 @@ namespace Orchard.Localization.Services {
|
||||
return TimeZoneInfo.ConvertTimeFromUtc(date.Value, workContext.CurrentTimeZone);
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromSiteTimeZone(DateTime date) {
|
||||
return ConvertFromSiteTimeZone(ToNullable(date));
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromSiteTimeZone(DateTime? date) {
|
||||
if (!date.HasValue) {
|
||||
return null;
|
||||
}
|
||||
var workContext = _workContextAccessor.GetContext();
|
||||
return TimeZoneInfo.ConvertTimeToUtc(date.Value, workContext.CurrentTimeZone);
|
||||
}
|
||||
|
||||
public virtual DateTimeParts? ConvertToSiteCalendar(DateTime? date) {
|
||||
if (!date.HasValue){
|
||||
return null;
|
||||
}
|
||||
var calendar = CurrentCalendar;
|
||||
return new DateTimeParts(
|
||||
calendar.GetYear(date.Value),
|
||||
calendar.GetMonth(date.Value),
|
||||
calendar.GetDayOfMonth(date.Value),
|
||||
calendar.GetHour(date.Value),
|
||||
calendar.GetMinute(date.Value),
|
||||
calendar.GetSecond(date.Value),
|
||||
Convert.ToInt32(calendar.GetMilliseconds(date.Value)));
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromSiteCalendar(DateTimeParts? parts) {
|
||||
if (!parts.HasValue) {
|
||||
return null;
|
||||
}
|
||||
var calendar = CurrentCalendar;
|
||||
return new DateTime(parts.Value.Date.Year, parts.Value.Date.Month, parts.Value.Date.Day, parts.Value.Time.Hour, parts.Value.Time.Minute, parts.Value.Time.Second, parts.Value.Time.Millisecond, calendar);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public virtual string ConvertToLocalString(DateTime date, string nullText = null) {
|
||||
return ConvertToLocalString(ToNullable(date), _dateTimeLocalization.LongDateTimeFormat, nullText);
|
||||
}
|
||||
@@ -41,7 +79,7 @@ namespace Orchard.Localization.Services {
|
||||
}
|
||||
|
||||
public virtual string ConvertToLocalString(DateTime? date, string format, string nullText = null) {
|
||||
var localDate = ConvertToLocal(date);
|
||||
var localDate = ConvertToSiteTimeZone(date);
|
||||
if (!localDate.HasValue) {
|
||||
return nullText;
|
||||
}
|
||||
@@ -80,18 +118,6 @@ namespace Orchard.Localization.Services {
|
||||
return ConvertToLocalString(date, _dateTimeLocalization.ShortTimeFormat, nullText);
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromLocal(DateTime date) {
|
||||
return ConvertFromLocal(ToNullable(date));
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromLocal(DateTime? date) {
|
||||
if (!date.HasValue) {
|
||||
return null;
|
||||
}
|
||||
var workContext = _workContextAccessor.GetContext();
|
||||
return TimeZoneInfo.ConvertTimeToUtc(date.Value, workContext.CurrentTimeZone);
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromLocalString(string dateString) {
|
||||
if (String.IsNullOrWhiteSpace(dateString)) {
|
||||
return null;
|
||||
@@ -117,7 +143,7 @@ namespace Orchard.Localization.Services {
|
||||
localDate = calendar.ToDateTime(localDate.Year, localDate.Month, localDate.Day, localDate.Hour, localDate.Minute, localDate.Second, localDate.Millisecond);
|
||||
}
|
||||
|
||||
return ConvertFromLocal(localDate);
|
||||
return ConvertFromSiteTimeZone(localDate);
|
||||
}
|
||||
|
||||
public virtual DateTime? ConvertFromLocalString(string dateString, string timeString) {
|
||||
@@ -147,7 +173,7 @@ namespace Orchard.Localization.Services {
|
||||
localDateTime = calendar.ToDateTime(localDateTime.Year, localDateTime.Month, localDateTime.Day, localDateTime.Hour, localDateTime.Minute, localDateTime.Second, localDateTime.Millisecond);
|
||||
}
|
||||
|
||||
return ConvertFromLocal(localDateTime);
|
||||
return ConvertFromSiteTimeZone(localDateTime);
|
||||
}
|
||||
|
||||
protected virtual CultureInfo CurrentCulture {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Orchard.Framework.Localization.Models;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
|
||||
@@ -10,18 +11,48 @@ namespace Orchard.Localization.Services {
|
||||
public interface IDateLocalizationServices : IDependency {
|
||||
|
||||
/// <summary>
|
||||
/// Converts a non-nullable date from Gregorian calendar UTC to the Orchard configured calendar and time zone.
|
||||
/// Converts a non-nullable date from UTC to the Orchard configured time zone.
|
||||
/// </summary>
|
||||
/// <param name="date">The non-nullable UTC date to convert. DateTime.MinValue is translated to null.</param>
|
||||
/// <param name="dateUtc">The non-nullable UTC date to convert. DateTime.MinValue is translated to null.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertToLocal(DateTime date);
|
||||
DateTime? ConvertToSiteTimeZone(DateTime dateUtc);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a nullable date from Gregorian calendar UTC to the Orchard configured calendar and time zone.
|
||||
/// Converts a nullable date from UTC to the Orchard configured time zone.
|
||||
/// </summary>
|
||||
/// <param name="date">The nullable UTC date to convert.</param>
|
||||
/// <param name="dateUtc">The nullable UTC date to convert.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertToLocal(DateTime? date);
|
||||
DateTime? ConvertToSiteTimeZone(DateTime? dateUtc);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a non-nullable date from the Orchard configured time zone to UTC.
|
||||
/// </summary>
|
||||
/// <param name="dateLocal">The non-nullable local date to convert. DateTime.MinValue is translated to null.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertFromSiteTimeZone(DateTime dateLocal);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a nullable date from the Orchard configured time zone to UTC.
|
||||
/// </summary>
|
||||
/// <param name="dateLocal">The nullable local date to convert.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertFromSiteTimeZone(DateTime? dateLocal);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a date from Gregorian calendar to the Orchard configured calendar.
|
||||
/// </summary>
|
||||
/// <param name="date">The Gregorian calendar date to convert.</param>
|
||||
/// <returns>Null if the supplied date parameter was null. Otherwise a <c>DateTimeParts</c> instance representing the converted date.</returns>
|
||||
DateTimeParts? ConvertToSiteCalendar(DateTime? date);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a date from the Orchard configured calendar to Gregorian calendar.
|
||||
/// </summary>
|
||||
/// <param name="parts">A <c>DateTimeParts</c> instance representing the Orchard configured calendar date to convert.</param>
|
||||
/// <returns>Null if the supplied parts parameter was null. Otherwise the converted Gregorian calendar date.</returns>
|
||||
DateTime? ConvertFromSiteCalendar(DateTimeParts? parts);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Converts a non-nullable date from Gregorian calendar UTC to the Orchard configured calendar and time zone and formats it using the default long date and time format string.
|
||||
@@ -81,20 +112,6 @@ namespace Orchard.Localization.Services {
|
||||
/// <returns></returns>
|
||||
string ConvertToLocalTimeString(DateTime? date, string nullText = null);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a non-nullable date to Gregorian calendar UTC from the Orchard configured calendar and time zone.
|
||||
/// </summary>
|
||||
/// <param name="date">The non-nullable UTC date to convert. DateTime.MinValue is translated to null.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertFromLocal(DateTime date);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a nullable date from Gregorian calendar UTC to the Orchard configured calendar and time zone.
|
||||
/// </summary>
|
||||
/// <param name="date">The nullable UTC date to convert.</param>
|
||||
/// <returns></returns>
|
||||
DateTime? ConvertFromLocal(DateTime? date);
|
||||
|
||||
/// <summary>
|
||||
/// Parses a date and time string using the Orchard configured culture and converts it to Gregorian calendar UTC from the Orchard configured calendar and time zone.
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user