0

Is possible to convert

'Thu Jul 17 17:20:38 2014'

with this function? Tried my best, but no result. This format uses justin.tv API, for twitch.tv i use code below and it works. Thanks for help.

var
t1, t2: Tdate;
dzien: integer;
begin
  t1 := StrToDateTime('"2014-07-21T12:49:08Z"');
  t2 := TTimeZone.Local.ToUniversalTime(Now);
  dzien := trunc(t2 - t1);
  if dzien > 0 then
    Result := (Format('%d days, %s', [dzien, FormatDateTime('hh:nn:ss',
      Frac(t2 - t1))]))
  else
    Result := (Format('%s', [FormatDateTime('hh:nn:ss', Frac(t2 - t1))]));
end;
mca64
  • 534
  • 1
  • 5
  • 17
  • 2
    See [here](http://stackoverflow.com/q/3786823) for a possible solution. – Ken White Jul 21 '14 at 12:59
  • TLama pasted code works, but twitch.tv api is usless for me. Justin.tv has what i need but they use strange date format. Thanks Ken White i will try again.. – mca64 Jul 21 '14 at 13:04

2 Answers2

2

It is easy enough to parse the string yourself. Like this:

uses
  Types, SysUtils, DateUtils, StrUtils;

function DecodeJustinTvDateTime(const Value: string): TDateTime;

  function MonthNumber(const MonthStr: string): Integer;
  var
    FormatSettings: TFormatSettings;
  begin
    FormatSettings := TFormatSettings.Create('en-us');
    for Result := low(FormatSettings.ShortMonthNames) to high(FormatSettings.ShortMonthNames) do begin
      if SameText(MonthStr, FormatSettings.ShortMonthNames[Result]) then begin
        exit;
      end;
    end;
    raise EConvertError.Create('Unrecognised month name');
  end;

var
  items: TStringDynArray;
  Day, Month, Year, Time, Hour, Minute, Second: string;
begin
  items := SplitString(Value, ' ');
  if Length(items)<>5 then begin
    raise EConvertError.Create('Unrecognised date time format');
  end;

  // items[0] is day of the week which we can ignore
  Month := items[1];
  Day := items[2];
  Time := items[3];
  Year := items[4];

  items := SplitString(Time, ':');
  Assert(Length(items)=3);
  if Length(items)<>3 then begin
    raise EConvertError.Create('Unrecognised time format');
  end;

  Hour := items[0];
  Minute := items[1];
  Second := items[2];

  Result := EncodeDateTime(
    StrToInt(Year),
    MonthNumber(Month),
    StrToInt(Day),
    StrToInt(Hour),
    StrToInt(Minute),
    StrToInt(Second),
    0
  );
end;

The error checking here is a little lame and you might care to improve on it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • thank You. I saw many format in StrToDateTime and dont know why justin.tv makes life harder with custom format. Actually i dont know why they share date when it should be just uptime. – mca64 Jul 21 '14 at 17:55
  • 1
    i was happy but not for long, justin.tv dont use utc time, but los angeles time zone. So much work to get this value for "uptime". Finally is over :) http://i.imgur.com/mYaUEal.png – mca64 Jul 22 '14 at 12:39
  • worked great until today. Now "Fri Aug 1 01:42:17 2014" has two spaces at once. Little fix and works. Here how all looks http://i.imgur.com/h1nz0qX.png – mca64 Aug 01 '14 at 08:57
  • That's essentially an issue between you and whoever is generating this text. I'd probably get a string splitter that coped with adjacent whitespace. My splitter does that. – David Heffernan Aug 01 '14 at 09:01
  • yes , i know. Hope no more suprises from this justin date :) – mca64 Aug 01 '14 at 09:30
  • after so much work i spent witch justin.tv api now "The Justin.tv website, mobile apps, and APIs are no longer in service." -.- – mca64 Aug 05 '14 at 17:45
1
procedure TForm6.Button1Click(Sender: TObject);
var
  t1: TDateTime;
  ts:TFormatSettings;
begin
  ts:=TFormatSettings.Create;
  ts.ShortDateFormat:='yyyy-MM-dd';
  ts.DateSeparator:='-';
  ts.TimeSeparator:=':';
  t1 := StrToDateTime('2014-07-21T12:49:08Z',ts);
end;

t1 contains date and time from your string.

LHristov
  • 1,103
  • 7
  • 16
  • hmmm i need to convert this "'Thu Jul 17 17:20:38 2014'" pasted code no need to modify cause it works. – mca64 Jul 21 '14 at 13:12
  • thanks for this. For polish windows this works t1 := StrToDateTime('"2014-07-21T12:49:08Z"') but for english doesnt, your code fix this problem. Funny that this api is from USA – mca64 Jul 22 '14 at 20:06