Skip to content

Support any number of trailing zeroes when decoding dates. #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Parse/Internal/FacebookAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public IDictionary<string, object> GetAuthData(string facebookId, string accessT
return new Dictionary<string, object> {
{"id", facebookId},
{"access_token", accessToken},
{"expiration_date", expiration.ToString(ParseClient.DateFormatString)}
{"expiration_date", expiration.ToString(ParseClient.DateFormatStrings.First())}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public Task SetAsync(ParseInstallation installation, CancellationToken cancellat
var data = installation.ServerDataToJSONObjectForSerialization();
data["objectId"] = installation.ObjectId;
if (installation.CreatedAt != null) {
data["createdAt"] = installation.CreatedAt.Value.ToString(ParseClient.DateFormatString);
data["createdAt"] = installation.CreatedAt.Value.ToString(ParseClient.DateFormatStrings.First());
}
if (installation.UpdatedAt != null) {
data["updatedAt"] = installation.UpdatedAt.Value.ToString(ParseClient.DateFormatString);
data["updatedAt"] = installation.UpdatedAt.Value.ToString(ParseClient.DateFormatStrings.First());
}

ParseClient.ApplicationSettings["CurrentInstallation"] = Json.Encode(data);
Expand Down
5 changes: 3 additions & 2 deletions Parse/Internal/ParseDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ internal static DateTime ParseDate(string input) {
// TODO(hallucinogen): Figure out if we should be more flexible with the date formats
// we accept.
return DateTime.ParseExact(input,
ParseClient.DateFormatString,
CultureInfo.InvariantCulture);
ParseClient.DateFormatStrings,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
}
}
}
3 changes: 2 additions & 1 deletion Parse/Internal/ParseEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Parse.Internal {
/// <summary>
Expand Down Expand Up @@ -31,7 +32,7 @@ public object Encode(object value) {
// encoded object. Otherwise, just return the original object.
if (value is DateTime) {
return new Dictionary<string, object> {
{"iso", ((DateTime)value).ToString(ParseClient.DateFormatString)},
{"iso", ((DateTime)value).ToString(ParseClient.DateFormatStrings.First())},
{"__type", "Date"}
};
}
Expand Down
5 changes: 3 additions & 2 deletions Parse/Internal/User/Controller/ParseCurrentUserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -34,10 +35,10 @@ public Task SetAsync(ParseUser user, CancellationToken cancellationToken) {
var data = user.ServerDataToJSONObjectForSerialization();
data["objectId"] = user.ObjectId;
if (user.CreatedAt != null) {
data["createdAt"] = user.CreatedAt.Value.ToString(ParseClient.DateFormatString);
data["createdAt"] = user.CreatedAt.Value.ToString(ParseClient.DateFormatStrings.First());
}
if (user.UpdatedAt != null) {
data["updatedAt"] = user.UpdatedAt.Value.ToString(ParseClient.DateFormatString);
data["updatedAt"] = user.UpdatedAt.Value.ToString(ParseClient.DateFormatStrings.First());
}

ParseClient.ApplicationSettings["CurrentUser"] = Json.Encode(data);
Expand Down
11 changes: 10 additions & 1 deletion Parse/ParseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ namespace Parse {
/// configuration for the Parse library.
/// </summary>
public static partial class ParseClient {
internal const string DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'";
internal static readonly string[] DateFormatStrings = {
// Official ISO format
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'",

// It's possible that the string converter server-side may trim trailing zeroes,
// so these two formats cover ourselves from that.
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ff'Z'",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f'Z'",
};


private static readonly object mutex = new object();
private static readonly string[] assemblyNames = {
Expand Down
32 changes: 32 additions & 0 deletions ParseTest.Unit/DecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ public void TestDecodeDate() {
Assert.AreEqual(0, dateTime.Millisecond);
}

[Test]
public void TestDecodeImproperDate() {
IDictionary<string, object> value = new Dictionary<string, object>() {
{ "__type", "Date" },
{ "iso", "1990-08-30T12:03:59.0Z" }
};

DateTime dateTime = (DateTime)ParseDecoder.Instance.Decode(value);
Assert.AreEqual(1990, dateTime.Year);
Assert.AreEqual(8, dateTime.Month);
Assert.AreEqual(30, dateTime.Day);
Assert.AreEqual(12, dateTime.Hour);
Assert.AreEqual(3, dateTime.Minute);
Assert.AreEqual(59, dateTime.Second);
Assert.AreEqual(0, dateTime.Millisecond);

// Test multiple trailing zeroes
value = new Dictionary<string, object>() {
{ "__type", "Date" },
{ "iso", "1990-08-30T12:03:59.00Z" }
};

dateTime = (DateTime)ParseDecoder.Instance.Decode(value);
Assert.AreEqual(1990, dateTime.Year);
Assert.AreEqual(8, dateTime.Month);
Assert.AreEqual(30, dateTime.Day);
Assert.AreEqual(12, dateTime.Hour);
Assert.AreEqual(3, dateTime.Minute);
Assert.AreEqual(59, dateTime.Second);
Assert.AreEqual(0, dateTime.Millisecond);
}

[Test]
public void TestDecodeBytes() {
IDictionary<string, object> value = new Dictionary<string, object>() {
Expand Down