Why does Time.strptime () return the current date?

Making a GET request for a private (no public documentation) API returns data in JSON format.

The im value is interested in the date. It returns the date in ASP.NET JSON date format. This is how it looks:

AanmeldDatum: "/Date(1406675114000+0200)/"

      

There is another variable, AangebodenSindsTekst, which stands for OfferedSinceText, and its value is "Aug 8, 2014". Thus, the unknown date format has to be parsed into that particular value.

I've tried this:

require 'time'

foo = Time.strptime('1406675114000+0200', '%N') # => 2014-08-12 13:38:46 +0200
foo = Time.strptime('1406675114000+0200', '%N%z') # => 2014-08-12 14:38:58 +0200

      

But it just returns the current time.

I know that I am 100% sure that /Date(1406675114000+0200)/

when parsing should return the date 2014-07-30

.

The question is, how can I be sure what it does?

+3


source to share


1 answer


Using '%N'

or '%N%z'

seems to throw an error in strptime

, resulting in the current date / time:

Time.strptime('1262300400000+0100', '%N') # => 2014-08-14 16:30:01 -0700
Time.strptime('1262300400000+0100', '%N%z') # => 2014-08-14 08:30:01 -0700

      

Usage '%Q%z'

parses:

Time.strptime('1262300400000+0100', '%Q%z') # => 2010-01-01 00:00:00 +0100

      

Time.strptime

the documentation
doesn't mention '%Q'

, but the DateTime.strptime

doc does.

If you've parsed a date string, then it's easy to format:

foo = Time.strptime('1262300400000+0100', '%Q%z') # => 2010-01-01 00:00:00 +0100
foo.strftime('%Y-%m-%d %H:%M:%S %z') # => "2010-01-01 00:00:00 +0100"

      



For more information on formatting settings, see the documentationstrftime

.

Please note that there are some shortcuts for things like '%Y-%m-%d'

and '%H:%M:%S'

:

foo.strftime('%F %T %z') # => "2010-01-01 00:00:00 +0100"

      

The real problem is how .NET serializes the date to JSON. A googles search reveals several pages talking about this:

.NET is not alone in this. Ruby JSON and YAML serial processors will sometimes do similar things, and the fix is ​​to look at how the JSON is outputted and then provide handlers to_json

or to_s

or to_h

that will result in the JSON output that best represents the data. JSON is supposed to be transferred between JavaScript and backend systems, and .NET is not the only language that offers this capability. Perl, Ruby, Python, and who knows how many other languages ​​can parse and emit JSON, and they all have to play by the same rules.

+3


source







All Articles