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;
2
  • 2
    See here for a possible solution.
    – Ken White
    Commented Jul 21, 2014 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
    Commented Jul 21, 2014 at 13:04

2 Answers 2

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.

6
  • 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
    Commented Jul 21, 2014 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 :) i.imgur.com/mYaUEal.png
    – mca64
    Commented Jul 22, 2014 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 i.imgur.com/h1nz0qX.png
    – mca64
    Commented Aug 1, 2014 at 8: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. Commented Aug 1, 2014 at 9:01
  • yes , i know. Hope no more suprises from this justin date :)
    – mca64
    Commented Aug 1, 2014 at 9:30
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.

2
  • hmmm i need to convert this "'Thu Jul 17 17:20:38 2014'" pasted code no need to modify cause it works.
    – mca64
    Commented Jul 21, 2014 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
    Commented Jul 22, 2014 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.