Difference between revisions of "PascalTZ"

From Free Pascal wiki
Jump to navigationJump to search
 
(12 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 +
{{LanguageBar}}
 +
 
=== About ===
 
=== About ===
PascalTZ stands for "Pascal Time Zone". It allows you to convert local times to GMT/UTC/Zulu and across time zones taking in account the different rules for this change on every time zone. PascalTZ uses the public database available at http://www.twinsun.com/tz/tz-link.htm to know how to change time for any past time (rules changed over the years) and it is almost 100% reliable for times in 20th and 21th century, and quite precise for prior times as some rules in 19th century and before were never properly documented. Time zone changes for the future times are not reliable as the rules can change sometimes suddenly like Venezuela 2007 change proposed a bunch of days before the change, or the 2009 Argentina ones.
 
  
The unit can:
+
PascalTZ stands for "Pascal Time Zone". It allows you to convert between local times in various [http://en.wikipedia.org/wiki/Time_zone time zones] and [http://en.wikipedia.org/wiki/Gmt GMT]/[http://en.wikipedia.org/wiki/Coordinated_Universal_Time UTC], taking into account historical changes to time zone rules. PascalTZ uses the [https://www.iana.org/time-zones Time Zone Database] (often called <tt>tz</tt> or <tt>zoneinfo</tt>) to determine how to correctly adjust time for various time zones. The correctness of time zone conversions in future relies on using an up to date database. Beware, time zone rules may be changed by governments around the world, sometimes with a very short notice.
 +
 
 +
PascalTZ component can be used in pure FPC projects or installed as a design and runtime package in Lazarus IDE. It also comes with a testing framework, a collection of time zone conversion test vectors and test cases for internal functions.
 +
 
 +
More information can be found at [https://github.com/dezlov/PascalTZ GitHub:PascalTZ].
 +
 
 +
=== Example ===
 +
 
 +
<syntaxhighlight lang=pascal>
 +
uses
 +
  SysUtils, DateUtils, uPascalTZ;
 +
 
 +
var
 +
  PascalTZ: TPascalTZ;
 +
  DateTime: TDateTime;
 +
 
 +
begin
 +
  PascalTZ := TPascalTZ.Create;
 +
 
 +
  // Load time zone database from "tzdata" directory
 +
  // Download from: https://www.iana.org/time-zones
 +
  PascalTZ.DatabasePath := 'tzdata';
 +
 
 +
  // Current local and UTC time
 +
  DateTime := Now;
 +
  WriteLn('Local time: ', DateTimeToStr(DateTime));
 +
  DateTime := LocalTimeToUniversal(DateTime);
 +
  WriteLn('UTC time: ', DateTimeToStr(DateTime));
 +
 
 +
  // Convert current time to Paris time
 +
  DateTime := PascalTZ.GMTToLocalTime(DateTime, 'Europe/Paris');
 +
  WriteLn('Time in Paris: ', DateTimeToStr(DateTime));
  
* Convert a given time/date in local time to/from GMT time.
+
  // Convert Paris time to Chicago time
* Convert a given time/date across two zone times.
+
  DateTime := PascalTZ.Convert(DateTime, 'Europe/Paris', 'America/Chicago');
* In the conversion process detect invalid times.
+
  WriteLn('Time in Chicago: ', DateTimeToStr(DateTime));
* Be updateable simply upgrading the [http://www.twinsun.com/tz/tz-link.htm TZ Database]
 
  
The download contains the component, an installation package and a demo application, that illustrates the features of the component along with some instrumentation for evaluating the component on a given system. The unit can also used in non Lazarus projects (plain fpc applications) as the unit does not require any user graphic interface but is included as a package for easy integration in applications.
+
  // Check if a time zone exists
 +
  WriteLn('Africa/Lagos exists? ', PascalTZ.TimeZoneExists('Africa/Lagos'));
 +
  WriteLn('Australia/Darwin exists? ', PascalTZ.TimeZoneExists('Australia/Darwin'));
  
=== Author ===
+
  PascalTZ.Free;
 +
end.
 +
</syntaxhighlight>
  
2009 - [[User:Joshy|Jose Mejuto]]
+
=== Authors ===
 +
 
 +
This library was originally published by [[User:Joshy|José Mejuto]] in 2009 and is maintained by [[User:dezlov|Denis Kozlov]] since 2015.
  
 
=== License ===
 
=== License ===
[http://svn.freepascal.org/svn/lazarus/trunk/COPYING.modifiedLGPL modified] [http://svn.freepascal.org/svn/lazarus/trunk/COPYING.LGPL LGPL] (same as the FPC RTL and the Lazarus LCL). You can contact the author if the modified LGPL doesn't work with your project licensing.
+
 
 +
[http://svn.freepascal.org/svn/lazarus/trunk/COPYING.modifiedLGPL Modified] [http://svn.freepascal.org/svn/lazarus/trunk/COPYING.LGPL LGPL] (same as the FPC RTL and the Lazarus LCL).
  
 
=== Download ===
 
=== Download ===
The latest stable release can be found on ''link to the lazarus-ccr sf download location'' (pending).
+
 
 +
[https://github.com/dezlov/PascalTZ/releases GitHub:PascalTZ releases]
  
 
=== Change Log ===
 
=== Change Log ===
  
* Version 1.0 2009.11.10
+
* Version 1.0 (2009-11-10) [https://github.com/dezlov/PascalTZ/blob/v2.0/CHANGELOG.md]
 
+
* Version 2.0 (2016-07-19) [https://github.com/dezlov/PascalTZ/blob/v2.0/CHANGELOG.md]
=== Dependencies / System Requirements ===
 
  
* [http://www.twinsun.com/tz/tz-link.htm TZ Database]
+
=== Bug Reports ===
  
Status: ''Stable''
+
Bug reports and suggestions can be logged at [https://github.com/dezlov/PascalTZ/issues GitHub:PascalTZ issues].
  
Issues: TDateTime in fpc seems to have problems for dates before 30 Dec 1899, so the time operations before such date could be wrong. If you need to operate with early dates you can derive a new class from TPascalTZ and expose the TTZDateTime to operate with.
+
=== See also ===
  
=== Installation ===
+
Since 2.6.2, FPC has the functions <code>LocalTimeToUniversal</code> and <code>UniversalTimeToLocal</code> in the ''dateutils'' unit to convert between local time and UTC time. These functions can be useful and an alternative for PascalTZ if you are not interested in historical/future date/times (i.e. the functions use current daylight saving time etc to convert to/from UTC time). These functions do allow for specifying manual offsets against UTC, but then you would need to keep track of those offsets - which PascalTZ does for you.
  
* Just add the '''upascaltz''' to the uses clause of your project.
+
[[Category:Components]]
* Load a database in a new instance of the class.
+
[[Category:Lazarus-CCR]]
 +
[[Category:Lazarus]]
 +
[[Category:FPC]]

Latest revision as of 21:17, 9 July 2020

English (en) русский (ru)

About

PascalTZ stands for "Pascal Time Zone". It allows you to convert between local times in various time zones and GMT/UTC, taking into account historical changes to time zone rules. PascalTZ uses the Time Zone Database (often called tz or zoneinfo) to determine how to correctly adjust time for various time zones. The correctness of time zone conversions in future relies on using an up to date database. Beware, time zone rules may be changed by governments around the world, sometimes with a very short notice.

PascalTZ component can be used in pure FPC projects or installed as a design and runtime package in Lazarus IDE. It also comes with a testing framework, a collection of time zone conversion test vectors and test cases for internal functions.

More information can be found at GitHub:PascalTZ.

Example

uses
  SysUtils, DateUtils, uPascalTZ;

var
  PascalTZ: TPascalTZ;
  DateTime: TDateTime;

begin
  PascalTZ := TPascalTZ.Create;

  // Load time zone database from "tzdata" directory
  // Download from: https://www.iana.org/time-zones
  PascalTZ.DatabasePath := 'tzdata';

  // Current local and UTC time
  DateTime := Now;
  WriteLn('Local time: ', DateTimeToStr(DateTime));
  DateTime := LocalTimeToUniversal(DateTime);
  WriteLn('UTC time: ', DateTimeToStr(DateTime));

  // Convert current time to Paris time
  DateTime := PascalTZ.GMTToLocalTime(DateTime, 'Europe/Paris');
  WriteLn('Time in Paris: ', DateTimeToStr(DateTime));

  // Convert Paris time to Chicago time
  DateTime := PascalTZ.Convert(DateTime, 'Europe/Paris', 'America/Chicago');
  WriteLn('Time in Chicago: ', DateTimeToStr(DateTime));

  // Check if a time zone exists
  WriteLn('Africa/Lagos exists? ', PascalTZ.TimeZoneExists('Africa/Lagos'));
  WriteLn('Australia/Darwin exists? ', PascalTZ.TimeZoneExists('Australia/Darwin'));

  PascalTZ.Free;
end.

Authors

This library was originally published by José Mejuto in 2009 and is maintained by Denis Kozlov since 2015.

License

Modified LGPL (same as the FPC RTL and the Lazarus LCL).

Download

GitHub:PascalTZ releases

Change Log

  • Version 1.0 (2009-11-10) [1]
  • Version 2.0 (2016-07-19) [2]

Bug Reports

Bug reports and suggestions can be logged at GitHub:PascalTZ issues.

See also

Since 2.6.2, FPC has the functions LocalTimeToUniversal and UniversalTimeToLocal in the dateutils unit to convert between local time and UTC time. These functions can be useful and an alternative for PascalTZ if you are not interested in historical/future date/times (i.e. the functions use current daylight saving time etc to convert to/from UTC time). These functions do allow for specifying manual offsets against UTC, but then you would need to keep track of those offsets - which PascalTZ does for you.