mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-26 20:16:15 +08:00
Changed implementation of DateTokens to use localization abstractions. Added more tokens. Beefed up unit tests to cover all tokens.
This commit is contained in:
@@ -2,27 +2,27 @@
|
||||
|
||||
namespace Orchard.AuditTrail.Helpers {
|
||||
public static class DateTimeExtensions {
|
||||
public static DateTime? Earliest(this DateTime? value) {
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
return Earliest(value.Value);
|
||||
}
|
||||
|
||||
public static DateTime Earliest(this DateTime value) {
|
||||
public static DateTime StartOfDay(this DateTime value) {
|
||||
return new DateTime(value.Year, value.Month, value.Day, 0, 0, 0, 0, value.Kind);
|
||||
}
|
||||
|
||||
public static DateTime? Latest(this DateTime? value) {
|
||||
public static DateTime? StartOfDay(this DateTime? value) {
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
return StartOfDay(value.Value);
|
||||
}
|
||||
|
||||
public static DateTime EndOfDay(this DateTime value) {
|
||||
return new DateTime(value.Year, value.Month, value.Day, 23, 59, 59, 999, value.Kind);
|
||||
}
|
||||
|
||||
public static DateTime? EndOfDay(this DateTime? value) {
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
var v = value.Value;
|
||||
return new DateTime(v.Year, v.Month, v.Day, 23, 59, 59, 999, v.Kind);
|
||||
}
|
||||
|
||||
public static DateTime Latest(this DateTime value) {
|
||||
return new DateTime(value.Year, value.Month, value.Day, 23, 59, 59, 999, value.Kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,7 +206,7 @@ namespace Orchard.AuditTrail.Services {
|
||||
}
|
||||
|
||||
public IEnumerable<AuditTrailEventRecord> Trim(TimeSpan retentionPeriod) {
|
||||
var dateThreshold = (_clock.UtcNow.Latest() - retentionPeriod);
|
||||
var dateThreshold = (_clock.UtcNow.EndOfDay() - retentionPeriod);
|
||||
var query = _auditTrailRepository.Table.Where(x => x.CreatedUtc <= dateThreshold);
|
||||
var records = query.ToArray();
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ namespace Orchard.AuditTrail.Services {
|
||||
public override void Filter(QueryFilterContext context) {
|
||||
var userName = context.Filters.Get("username");
|
||||
var category = context.Filters.Get("category");
|
||||
var from = GetDateFromFilter(context.Filters, "From", "from").Earliest();
|
||||
var to = GetDateFromFilter(context.Filters, "To", "to").Latest();
|
||||
var from = GetDateFromFilter(context.Filters, "From", "from").StartOfDay();
|
||||
var to = GetDateFromFilter(context.Filters, "To", "to").EndOfDay();
|
||||
var query = context.Query;
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(userName)) {
|
||||
@@ -57,13 +57,12 @@ namespace Orchard.AuditTrail.Services {
|
||||
}
|
||||
|
||||
private DateTime? GetDateFromFilter(Filters filters, string fieldName, string prefix) {
|
||||
var dateString = filters.Get(prefix + ".Date");
|
||||
try {
|
||||
var dateString = filters.Get(prefix + ".Date");
|
||||
var timeString = filters.Get(prefix + ".Time");
|
||||
return _dateLocalizationServices.ConvertFromLocalizedString(dateString, timeString);
|
||||
return _dateLocalizationServices.ConvertFromLocalizedDateString(dateString);
|
||||
}
|
||||
catch (FormatException ex) {
|
||||
filters.UpdateModel.AddModelError(prefix, T(@"Error parsing ""{0}"" date: {1}", fieldName, ex.Message));
|
||||
filters.UpdateModel.AddModelError(prefix, T(@"Error parsing '{0}' date string '{1}': {2}", fieldName, dateString, ex.Message));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user