Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-04

How to convert ISO format string in FreePascal

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but can't find but haven't found a way yet. My attempt is based on the method here,
https://forums.codegear.com/thread.jspa?threadID=16074, but StrToDateTime in FPC does not use the FormatSettings parameter.

function // function TryStrToDate(const S: string; out Value:
TDateTime; const FormatSettings: TFormatSettings): Boolean;
// function TryStrToTime(const S: string; out Value: TDateTime; const
FormatSettings: TFormatSettings): Boolean;
// function TryStrToDateTime(const S: string; out Value: TDateTime;
const FormatSettings: TFormatSettings): Boolean;
(const S: string): TDateTime;

The references to FormatSettings are commented out, they appear to be an abandoned attempt

// function TryStrToDate(const S: string; out Value: TDateTime; const
FormatSettings: TFormatSettings): Boolean;
// function TryStrToTime(const S: string; out Value: TDateTime; const
FormatSettings: TFormatSettings): Boolean;
// function TryStrToDateTime(const S: string; out Value: TDateTime;
const FormatSettings: TFormatSettings): Boolean;
function StrToDateTimeDef(const S: string; const Defvalue :
TDateTime): TDateTime;

I switched to using the unixtime value which is also present in the records I am using, but it does not allow for Daylight Savings Time and is currently an hour behind. Is there some way the FreePascal libraries can adjust for DST? Is there some other unit I can use or work through?

1272379859.203125 gives 2010-04-27 14:50:59 instead of 2010-04-27 15:50:59 in the UK.

My current code is based on "TDateTime(25569 + (StrToInt(epoch) * 1.0)/86400.0)".

Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

José Mejuto
to Frank Church

Hello FPC-Pascal,

Tuesday, April 27, 2010, 5:43:30 PM, you wrote:

FC> I switched to using the unixtime value which is also present in the FC> records I am using, but it does not allow for Daylight Savings Time FC> and is currently an hour behind. Is there some way the FreePascal FC> libraries can adjust for DST? Is there some other unit I can use or FC> work through?
FC> 1272379859.203125 gives 2010-04-27 14:50:59 instead of 2010-04-27 FC> 15:50:59 in the UK.
FC> My current code is based on "TDateTime(25569 + (StrToInt(epoch) * FC> 1.0)/86400.0)".

If you want DST time in "historic" mode you can use PascalTZ http://wiki.lazarus.freepascal.org/PascalTZ as unixtime is GMT.

Best regards, José

Reply by email

Luiz Americo Pereira Camara
to Frank Church: I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but…
Frank Church wrote
I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but can't find but haven't found a way yet.

A very simple function i use:

function XMLDateTime2DateTime(const XMLDateTime: String): TDateTime;
var
  DateOnly: String;
  TPos: Integer;
begin
  TPos := Pos('T', XMLDateTime);
  if TPos  0 then
    DateOnly := Copy(XMLDateTime, 1, TPos - 1)
  else
    DateOnly := XMLDateTime;
  Result := ScanDateTime('yyyy-mm-dd', DateOnly);
end;

Luiz

Reply by email

Graeme Geldenhuys
to Frank Church: I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but…
Frank Church wrote
I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but can't find but haven't found a way yet.

I have written the following for the tiOPF project and us it in our company projects for all date/time values (including storage in database fields).

See attached file for the relevant functions. The code has been some unit tests as well, and can be found in the tiOPF testing framework. tiOPF is hosted on SourceForge.

NOTE:
I do not manage weeks or timezone information in the ISO 8601 standard.

Some constants I forgot to add to the txt file.

  { Summary of ISO 8601  http://www.cl.cam.ac.uk/~mgk25/iso-time.html }
  cIntlDateTimeStor = 'yyyymmdd"T"hhmmss';    // for storage
  cIntlDateTimeDisp = 'yyyy-mm-dd hh:mm:ss';  // for display
  CIntlDateDisp     = 'yyyy-mm-dd';  // for display
Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Marco van de Voort
to Frank Church: I want to convert a time stamp in yyyy-mm-dd hh:mm:ss format to a TDatetime in FPC but…

In our previous episode, Frank Church said:

attempt is based on the method here, https://forums.codegear.com/thread.jspa?threadID=16074, but StrToDateTime in FPC does not use the FormatSettings parameter.

Datetime handling has been rewritten since 2.4.0, and this is already merged back to 2.4.1 afaik. (though there are still open tickets iirc)

For 2.4.0 you could try to use scandatetime:

http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

Reply by email

Frank Church
to Marco van de Voort

Is the an LCL routine that can get the timezone from the computer on both Windows and Linux? None of the examples mention a way of getting the computer's time zone.

Marco van de Voort wrote 17 lines
In our previous episode, Frank Church said: > attempt is based on the method here, > https://forums.codegear.com/thread.jspa?threadID=16074, but > StrToDateTime in FPC does not use the FormatSettings parameter. Datetime handling has been rewritten since 2.4.0, and this is already merged back to 2.4.1 afaik. (though there are still open tickets iirc) For 2.4.0 you could try to use scandatetime: http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

José Mejuto
to Frank Church

Hello FPC-Pascal,

Wednesday, May 5, 2010, 11:23:42 PM, you wrote:

FC> Is the an LCL routine that can get the timezone from the computer on both FC> Windows and Linux? None of the examples mention a way of getting the FC> computer's time zone.

Usually you will have an environment variable TZ, but at least in Windows this is not usually well formed and it is basically the most easy representation without daylight saving.

In my case, Spain, I get TZ=GMT-1 and the right one should be TZ=CET-1CEST

In my experience is much better to ask the user for the right time zone based in a bunch of cities, a lot of computers have a wrong time zone, I saw spanish computers with time zone Mexico, Colombia, Argentina... The user simply change local time and go on.

Best regards, José

Reply by email