Permalink
Browse files

fixed to #230

added new JsConfig.AlwaysUseUtc config property
  • Loading branch information...
desunit committed Dec 6, 2012
1 parent 58c2538 commit b85e1b6b28fe86d6fa28061089914a4edf30d97d
Showing with 43 additions and 7 deletions.
  1. +22 −7 src/ServiceStack.Text/Common/DateTimeSerializer.cs
  2. +21 −0 src/ServiceStack.Text/JsConfig.cs
@@ -31,6 +31,19 @@ public static class DateTimeSerializer
public const string WcfJsonPrefix = "/Date(";
public const char WcfJsonSuffix = ')';
+ /// <summary>
+ /// If AlwaysUseUtc is set to true then convert all DateTime to UTC.
+ /// </summary>
+ /// <param name="dateTime"></param>
+ /// <returns></returns>
+ private static DateTime Prepare(this DateTime dateTime)
+ {
+ if (JsConfig.AlwaysUseUtc && dateTime.Kind != DateTimeKind.Utc)
+ return dateTime.ToStableUniversalTime();
+ else
+ return dateTime;
+ }
+
public static DateTime? ParseShortestNullableXsdDateTime(string dateTimeStr)
{
if (dateTimeStr == null)
@@ -45,13 +58,15 @@ public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
return DateTime.MinValue;
if (dateTimeStr.StartsWith(EscapedWcfJsonPrefix) || dateTimeStr.StartsWith(WcfJsonPrefix))
- return ParseWcfJsonDate(dateTimeStr);
+ return ParseWcfJsonDate(dateTimeStr).Prepare();
- if (dateTimeStr.Length == DefaultDateTimeFormat.Length
- || dateTimeStr.Length == DefaultDateTimeFormatWithFraction.Length)
- return DateTime.Parse(dateTimeStr, CultureInfo.InvariantCulture);
+ if (dateTimeStr.Length == DefaultDateTimeFormat.Length
+ || dateTimeStr.Length == DefaultDateTimeFormatWithFraction.Length)
+ {
+ return DateTime.Parse(dateTimeStr, CultureInfo.InvariantCulture).Prepare();
+ }
- if (dateTimeStr.Length == XsdDateTimeFormatSeconds.Length)
+ if (dateTimeStr.Length == XsdDateTimeFormatSeconds.Length)
return DateTime.ParseExact(dateTimeStr, XsdDateTimeFormatSeconds, null,
DateTimeStyles.AdjustToUniversal);
@@ -62,10 +77,10 @@ public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
? XmlDateTimeSerializationMode.Local
: XmlDateTimeSerializationMode.RoundtripKind;
- return XmlConvert.ToDateTime(dateTimeStr, dateTimeType);
+ return XmlConvert.ToDateTime(dateTimeStr, dateTimeType).Prepare();
}
- return DateTime.Parse(dateTimeStr, null, DateTimeStyles.AssumeLocal);
+ return DateTime.Parse(dateTimeStr, null, DateTimeStyles.AssumeLocal).Prepare();
}
public static string ToDateTimeString(DateTime dateTime)
@@ -313,6 +313,26 @@ public static bool ThrowOnDeserializationError
}
}
+ /// <summary>
+ /// Gets or sets a value indicating if the framework should always convert <see cref="DateTime"/> to UTC format instead of local time.
+ /// </summary>
+ [ThreadStatic]
+ private static bool? tsAlwaysUseUtc;
+ private static bool? sAlwaysUseUtc;
+ public static bool AlwaysUseUtc
+ {
+ // obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
+ get
+ {
+ return tsAlwaysUseUtc ?? sAlwaysUseUtc ?? false;
+ }
+ set
+ {
+ tsAlwaysUseUtc = value;
+ if (!sAlwaysUseUtc.HasValue) sAlwaysUseUtc = value;
+ }
+ }
+
internal static HashSet<Type> HasSerializeFn = new HashSet<Type>();
internal static HashSet<Type> TreatValueAsRefTypes = new HashSet<Type>();
@@ -359,6 +379,7 @@ public static void Reset()
tsTypeWriter = sTypeWriter = null;
tsTypeFinder = sTypeFinder = null;
tsTreatEnumAsInteger = sTreatEnumAsInteger = null;
+ tsAlwaysUseUtc = sAlwaysUseUtc = null;
HasSerializeFn = new HashSet<Type>();
TreatValueAsRefTypes = new HashSet<Type> { typeof(KeyValuePair<,>) };
PropertyConvention = JsonPropertyConvention.ExactMatch;

0 comments on commit b85e1b6

Please sign in to comment.