pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: JSON / ASP.NET AJAX Dates support in PostgreSQL
Date: Fri, 14 Apr 2023 10:44:08 -0700
Message-ID: <910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com> (raw)
In-Reply-To: <20230414163116.ubjrzvgjtqafajcl@hjp.at>
References: <DBAP191MB12896A817739B869A781C00CB0989@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
	<c6fceaf9-0823-b2ad-ae00-3aa54ee017b1@gmail.com>
	<20230414163116.ubjrzvgjtqafajcl@hjp.at>

On 4/14/23 9:31 AM, Peter J. Holzer wrote:
> On 2023-04-13 10:07:09 -0500, Ron wrote:
>> On 4/13/23 09:44, Sebastien Flaesch wrote:
>>      Is there an easy way to convert JSON data containing ASP.NET AJAX Dates
>>      into PostgreSQL timestamp?
>>
>>      I have this kind of JSON data:
>>
>>      {
>>          "PurchaseOrder" : "4500000000",
>>          "CreationDate" : "\/Date(1672358400000)\/",
>>          "LastChangeDateTime" : "\/Date(1672692813062+0100)\/"
>>      }
>>
>>      Warning: Note the backslash before the slashes!
> 
> That's a Noop. According to RFC 8259, "\/" is the same as "/" (no idea
> why they even specified that - it seems quite pointless).

It is a cheat explained here:

https://weblogs.asp.net/bleroy/dates-and-json


"Our current approach is using a small loophole in the JSON specs. In a 
JSON string literal, you may (or may not) escape some characters. Among 
those characters, weirdly enough, there is the slash character ('/'). 
This is weird because there actually is no reason that I can think of 
why you'd want to do that. We've used it to our benefit to disambiguate 
a string from a date literal.

The new format is "\/Date(1198908717056)\/" where the number is again 
the number of milliseconds since January 1st 1970 UTC. I would gladly 
agree that this is still not super readable, which could be solved by 
using ISO 8601 instead.

The point is that this disambiguates a date literal from a string that 
looks like the same date literal, while remaining pure JSON that will be 
parsed by any standard JSON parser. Of course, a parser that doesn't 
know about this convention will just see a string, but parsers that do 
will be able to parse those as dates without a risk for false positives 
(except if the originating serializer escaped slashes, but I don't know 
of one that does).
"
> 
>>      According to JSON spec this is valid JSON and used by AJAX Date format.
> 
> It's valid JSON, but for JSON it's just a string, not a date.
> 
> Any interpretation is strictly by convention between the sender and the
> receiver.
> 
> 
>> This looks like "milliseconds since the Unix epoch:
>>
>> $ date -d @1672692813.062
>> Mon 02 Jan 2023 02:53:33 PM CST
>>
>> Thus:
>> select to_timestamp(cast(1672692813062 as bigint))::timestamp;
> 
> ITYM:
> 
>      select to_timestamp(1672692813062/1000.0);
> 
>          hp
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (9+ messages)  latest in thread

Message-ID: <910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com>
Permalink:  ../910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com/
Also on:    postgresql.org/message-id/910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com
  Subject: Re: JSON / ASP.NET AJAX Dates support in PostgreSQL
  In-Reply-To: <910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com>
  References: <DBAP191MB12896A817739B869A781C00CB0989@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> <c6fceaf9-0823-b2ad-ae00-3aa54ee017b1@gmail.com> <20230414163116.ubjrzvgjtqafajcl@hjp.at> <910fb9a0-9744-0eed-58d4-648806cea02d@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox