Created
April 2, 2018 14:04
-
-
Save hughbiquitous/d1098da73767a6bbd5889bbb8c3dd1a8 to your computer and use it in GitHub Desktop.
BsonDocument.Parse does not interpret ISO8601 date/time strings as ISODate values; these post-processing methods tidy that up after the fact.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using MongoDB.Bson; | |
namespace CSharp2233 | |
{ | |
public static class BsonExtensions | |
{ | |
private static readonly string[] _formats = | |
{ | |
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFZ", | |
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFK", | |
}; | |
/// <summary> | |
/// When you call BsonDocument.Parse on a JSON string containing ISO8601 dates (e.g., "2018-04-02T08:03:12.3456789-04:00") | |
/// it does not interpret them as datetime values; it just treats them as strings. | |
/// </summary> | |
/// <remarks>I logged it at https://jira.mongodb.org/browse/CSHARP-2233 ... they closed it "as designed" so this will | |
/// be an enduring solution.</remarks> | |
public static BsonDocument ConvertToIsoDates(this BsonDocument bsonDocument) | |
{ | |
for (var i = 0; i < bsonDocument.ElementCount; ++i) | |
{ | |
var bsonValue = bsonDocument[i]; | |
switch (bsonValue.BsonType) | |
{ | |
case BsonType.String: | |
if (DateTime.TryParseExact(bsonValue.AsString, | |
_formats, | |
CultureInfo.InvariantCulture, | |
DateTimeStyles.None, | |
out var result)) | |
{ | |
bsonDocument[i] = new BsonDateTime(result); | |
} | |
break; | |
case BsonType.Array: | |
bsonDocument[i].AsBsonArray.ConvertToIsoDates(); | |
break; | |
case BsonType.Document: | |
bsonDocument[i].AsBsonDocument.ConvertToIsoDates(); | |
break; | |
} | |
} | |
return bsonDocument; | |
} | |
/// <summary> | |
/// When you call BsonDocument.Parse on a JSON string containing ISO8601 dates (e.g., "2018-04-02T08:03:12.3456789-04:00") | |
/// it does not interpret them as datetime values; it just treats them as strings. | |
/// </summary> | |
/// <remarks>I logged it at https://jira.mongodb.org/browse/CSHARP-2233 ... they closed it "as designed" so this will | |
/// be an enduring solution.</remarks> | |
public static BsonArray ConvertToIsoDates(this BsonArray bsonArray) | |
{ | |
for (var i = 0; i < bsonArray.Count; ++i) | |
{ | |
var bsonValue = bsonArray[i]; | |
switch (bsonValue.BsonType) | |
{ | |
case BsonType.String: | |
if (DateTime.TryParseExact(bsonValue.AsString, | |
_formats, | |
CultureInfo.InvariantCulture, | |
DateTimeStyles.None, | |
out var result)) | |
{ | |
bsonArray[i] = new BsonDateTime(result); | |
} | |
break; | |
case BsonType.Array: | |
bsonArray[i].AsBsonArray.ConvertToIsoDates(); | |
break; | |
case BsonType.Document: | |
bsonArray[i].AsBsonDocument.ConvertToIsoDates(); | |
break; | |
} | |
} | |
return bsonArray; | |
} | |
} | |
} |
Thanks a lot. It actually works for me.
Thanks a lot. This really helped. Here are some other formats in case someone is interested
private static readonly string[] _formats =
{
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFZ",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFK",
"yyyy-MM-ddTHH:mm:ssZ",
"yyyy-MM-ddTHH:mm:ss"
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. It worked great.