1

I'm uses SuperObject library for work with JSON.

I have this JSON(part of Mozilla FireFox, Chrome Bookmarks file):

   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13009663942000000",
            "id": "11",
            "meta_info": "{\"sync\":{\"transaction_version\":\"3\"}}",
            "name": "\u041D\u0430\u0447\u0430\u043B\u044C\u043D\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
            "type": "url",
            "url": "http://www.mozilla.com/ru/firefox/central/"
         }, {

I tried use function JavaTimeToDelphiDateTime with data as integer, but it doesn't work.

I need read "date_added" field as TDateTime. How to do that, using SuperObject library?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Alexandr
  • 338
  • 1
  • 4
  • 16
  • 1
    It seems that your JSON timestamp is in microseconds (not in milliseconds as one might expect). Read it as a `Int64` value, but before you pass it to your `JavaTimeToDelphiDateTime` function, convert it to units that the function expects ([`which is milliseconds ?`](http://delphi.fosdal.com/2008/09/snippet-convert-java-time-to-tdatetime.html)). – TLama Oct 21 '13 at 08:56
  • thanks. That function returns 01.02.5042 12:34:21 :d Also I div value on 1000, to convert it in milliseconds, result stays wrong. If i div data on million, I get 05.04.1970... – Alexandr Oct 21 '13 at 09:50
  • 1
    I don't know anything about the function you're using for conversion (since it's not a part of Delphi). I've just tried to convert your timestamp by using [`this website`](http://www.epochconverter.com/), which says *"Assuming that this timestamp is in microseconds (1/1,000,000 second): Thu, 24 Mar 2011 11:33:14 GMT"*, which looks reasonable. Include the code you're using for conversion into the question, please. Except that also elaborate what *"doesn't work"* mean - what results you got and what you're expecting to get. – TLama Oct 21 '13 at 10:03
  • 2
    Well, it seems you don't want to update your question... Never mind. By using [`this function`](http://delphi.fosdal.com/2008/09/snippet-convert-java-time-to-tdatetime.html) divide your value by 10,000 and you'll get 24.3.2011 12:33:14. Hard to say if that's what you want to get and hard to say why. That's what should have been in your question. In the meantime I've voted this question to close... – TLama Oct 21 '13 at 13:01
  • I've updated my post now. Thanks for your help! Could you please answer on the question? – Alexandr Oct 21 '13 at 14:51
  • You're welcome! Anyway, feel free to post the answer by yourself ;-) – TLama Oct 21 '13 at 14:53
  • BTW That date_added number is probably generated as 'ticks' every 100 nanosecond – Jan Doggen Oct 29 '13 at 09:25
  • it isn't a random, is a encoded date & time. – Alexandr Oct 29 '13 at 10:03

1 Answers1

3

The solution:

function JavaTimeToDateTime(javatime:Int64):TDateTime;
// java time -> Win32 file time -> UTC time
// adjust to active time zone -> TDateTime
var
  UTCTime, LocalTime: TSystemTime;
begin
  FileTimeToSystemTime(TFileTime(Int64(javatime + 11644473600000) * 10000), UTCTime);
  SystemTimeToTzSpecificLocalTime(nil, UTCTime, LocalTime);
  Result := SystemTimeToDateTime(LocalTime);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //13009663942000000 is the value, read from "date_added" field as Int64.
  ShowMessage(DateTimeToStr(JavaTimeToDateTime((13009663942000000 div 10000))));
end;
Alexandr
  • 338
  • 1
  • 4
  • 16