It looks like you're new here. If you want to get involved, click one of these buttons!
Problems with stored Json date and time!
Timestamp of the saved ticks is not the same as received timestamp, when deserialized.
#r "System.Web.Extensions"
using System.Text.RegularExpressions ;
using System.Web.Script.Serialization ;
public static DateTime UnixToDateTime(UInt64 unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 5, 30, 0, 0, DateTimeKind.Unspecified);
dateTime = dateTime.AddSeconds(unixTimeStamp);
return dateTime;
}
public static string JsonSerialize(object obj)
{
//ContribStart: was causing exception for MaxJsonLength with default settings so changed it to int.MaxValue
var jss = new JavaScriptSerializer() ;
jss.MaxJsonLength = int.MaxValue ;
jss.RecursionLimit = int.MaxValue ;
//ContribEnd:
string json = jss.Serialize(obj);
MatchCollection mc = Regex.Matches(json, @"\\/Date\((\d*?)\)\\/");
foreach (Match m in mc)
{
UInt64 unix = UInt64.Parse(m.Groups[1].Value) / 1000;
json = json.Replace(m.Groups[0].Value, UnixToDateTime(unix).ToString());
}
return json;
}
var a = JsonSerialize( DateTime.Now ) ;
var b = new JavaScriptSerializer().Deserialize<DateTime>( a ) ;
> a
"\"07-02-2018 15:39:59\""
> b
[02-07-2018 15:39:59]
<--------- day shifts as month. Is this Intended?
Also, JsonSerialize function is meant for debugging purpose and not for production purposes. If you want to have some comparison or processing based on the date values you should use the values inside the data structures client library returns directly. If you really want them as strings you should use a fixed format in the ToString(format) function call.
and returned string was in format of MM-dd-yyyy.
So obviously it is not the system. While code is in memory then it can be compared from fields. but stored data is on disk it has to be deserialized and that is where problem lies. Time is assuming various forms.
Thanks
Regards
That is, who are serializing ticks to a disk file. and at deserialization find time to be offset-ted.
will have to add 5hours and 30 minutes to it.
for example:- on C# interactive apart from other Json date quirks.
Thanks
Regards