timeutils.pas

来自「用DELPHI实现的 PGP 加密算法」· PAS 代码 · 共 648 行 · 第 1/2 页

PAS
648
字号
{$J+,Z4}
unit TimeUtils;

{:      Primoz Gabrijelcic's Time Zone Routines v1.2:

	Date/Time Routines to enhance your 32-bit Delphi Programming.

	(c) 1999, 2000 Primoz Gabrijelcic

	===================================================

	original file name gpTimezone.pas

	modified by Michael in der Wiesche <idw.doc@t-online.de>:
	- added two UnixTime conversion functions
	- added a SystemTimeToDateTime routine
	- included required ESBDates routines
	- removed Registry dependent routines
	- added SECONDS_PER_DAY constant
	- renamed MINUTESPERDAY constant
	- changed some function parameters to const
	- some minor optical and comment changes ...

	Portions Copyright (c) Michael in der Wiesche, 2002-2003

	===================================================

	The original routines are used by ESB Consultancy and Primoz Gabrijelcic
	within the development of their Customised Application.
	Primoz Gabrijelcic retains full copyright.
	mailto:gabr@17slon.com
	http://17slon.com/gp/gp/
	http://www.eccentrica.org/gabr/gp/
	http://members.xoom.com/primozg/gp/

	Primoz Gabrijelcic grants users of this code royalty free rights
	to do with this code as they wish.

	Primoz Gabrijelcic makes no guarantees nor excepts any liabilities
	due to the use of these routines.

	We do ask that if this code helps you in your development
	that you send as an email mailto:info@esbconsult.com.au or even
	a local postcard. It would also be nice if you gave us a
	mention in your About Box, Help File or Documentation.

	ESB Consultancy Home Page: http://www.esbconsult.com.au

	Mail Address: PO Box 2259, Boulder, WA 6432 AUSTRALIA }

interface

uses
  Windows,
  SysUtils;

const
  MINUTES_PER_DAY = 24*60;
  SECONDS_PER_DAY = 24*60*60;

  {: Returns true if date1 and date2 are almost the same (difference between
     them is less than 1/10 of a millisecond. }
  function DateEQ(const date1, date2: TDateTime): boolean;

  {: Corrects date part so it will represent exact (as possible) millisecond,
     not maybe small part before or after that. Useful when you want to use
     Trunc/Int and Frac functions to get date or time part from TDateTime
     variable. }
  function FixDT(const date: TDateTime): TDateTime;

  {: Returns the Day of the Month number from a given date/time. }
  function Date2Day(const DT: TDateTime): Word;

  {: Returns the Month number from a given date/time, 1 = Jan, etc. }
  function Date2Month(const DT: TDateTime): Word;

  {: Returns the Year from a given date/time. }
  function Date2Year(const DT: TDateTime): Word;

  {: Returns the Hour from a given date/time. }
  function Time2Hr (const DT: TDateTime): Word;

  {: Returns the Minute from a given date/time. }
  function Time2Min (const DT: TDateTime): Word;

  {: Returns the Second from a given date/time. }
  function Time2Sec (const DT: TDateTime): Word;

  {: Returns the Millisecond from a given date/time. }
  function Time2MSec (const DT: TDateTime): Word;

  {: Is given Year a Leap Year. Thanks to Dr John Stockton
     for suggesting a faster methodology. }
  function IsLeapYear(Year: Word): Boolean;

  {: Is given Date/Time in a Leap Year. Thanks to Dr John Stockton
     for suggesting a faster methodology. }
  function DateIsLeapYear(const DT: TDateTime): Boolean;

  {: Returns the number of days in the Month represented by the given Date. }
  function DaysInMonth(const DT: TDateTime): Byte;

  {: Returns the current Year - from Today's Date. }
  function ThisYear: Word;

  {: Returns the current Month - from Today's Date. }
  function ThisMonth: Word;

  {: Returns the current Day - from Today's Date. }
  function ThisDay: Word;

  {: Returns the current Hour - from the current Time. }
  function ThisHr: Word;

  {: Returns the current Minute - from the current Time. }
  function ThisMin: Word;

  {: Returns the current Second - from the current Time. }
  function ThisSec: Word;

  {: Returns the current Millisecond - from the current Time. }
  function ThisMSec: Word;

  {: Converts 'day of month' syntax to normal date. Set year and month to
     required values, set weekInMonth to required week (1-4, or 5 for last),
     set dayInWeek to required day of week (1 (Sunday) to 7 (Saturday) - Delphi
     style). }
  function DayOfMonth2Date(year, month, weekInMonth, dayInWeek: word): TDateTime;

  {: Converts TIME_ZONE_INFORMATION date to normal date. Time zone information
     can be returned in two formats by Windows API call GetTimeZoneInformation.
     Absolute format specifies an exact date and time when standard/DS time
     begins. In this form, the wYear, wMonth, wDay, wHour, wMinute , wSecond,
     and wMilliseconds members of the TSystemTime structure are used to specify
     an exact date. Year is left intact, if you want to change it, call
     ESBDates.AdjustDateYear (warning: this will clear the time part).
     Day-in-month format is specified by setting the wYear member to zero,
     setting the wDayOfWeek member to an appropriate weekday (0 to 6,
     0 = Sunday), and using a wDay value in the range 1 through 5 to select the
     correct day in the month. Year parameter is used to specify year for this
     date.
     Returns 0 if 'dstDate' is invalid or if it specifies "absolute date" for a
     year not equal to 'year' parameter. }
  function DSTDate2Date(const dstDate: TSystemTime; year: word): TDateTime;

  {: Returns daylight saving information for a specified time zone and year.
     Sets DaylightDate and StandardDate year to specified year if date is
     specified in day-in-month format (see above).
     DaylightDate and StandardDate are returned in local time. To convert them
     to UTC use DaylightDate+StandardBias/MINUTESPERDAY and
     StandardDate+DaylightBias/MINUTESPERDAY.
     Returns false if 'TZ' is invalid or if it specifies "absolute date" for a
     year not equal to 'year' parameter. }
  function GetTZDaylightSavingInfoForYear(
    const TZ: TTimeZoneInformation; year: word;
    var DaylightDate, StandardDate: TDateTime;
    var DaylightBias, StandardBias: longint): boolean;

  {: Returns daylight saving information for a specified time zone and current
     year. See GetTZDaylightSavingInfoForYear for more information. }
  function GetTZDaylightSavingInfo(
    const TZ: TTimeZoneInformation;
    var DaylightDate, StandardDate: TDateTime;
    var DaylightBias, StandardBias: longint): boolean;

  {: Returns daylight saving information for current time zone and specified
     year. See GetTZDaylightSavingInfoForYear for more information. }
  function GetDaylightSavingInfoForYear(
    year: word;
    var DaylightDate, StandardDate: TDateTime;
    var DaylightBias, StandardBias: longint): boolean;

  {: Returns daylight saving information for current time zone and year. See
     GetTZDaylightSavingInfoForYear for more information. }
  function GetDaylightSavingInfo(
    var DaylightDate, StandardDate: TDateTime;
    var DaylightBias, StandardBias: longint): boolean;

  {: Converts local time to UTC according to a given timezone rules. Takes into
     account daylight saving time as it was active at that time. This is not
     very safe as DST rules are always changing.
     Special processing is done for the times during the standard/daylight time
     switch.
     If the specified local time lies in the non-existing area (when clock is
     moved forward), function returns 0.
     If the specified local time lies in the ambigious area (when clock is moved
     backward), function takes into account value of preferDST parameter. If it
     is set to true, time is converted as if it belongs to the daylight time. If
     it is set to false, time is converted as if it belong to the standard time.
  }
  function TZLocalTimeToUTC(
    const TZ: TTimeZoneInformation;
    const loctime: TDateTime;
    preferDST: boolean): TDateTime;

  {: Converts local time to UTC according to a given timezone rules. Takes into
     account daylight saving time as it was active at that time. This is not
     very safe as DST rules are always changing.
     In Windows NT/2000 (but not in 95/98) you can use API function
     SystemTimeToTzSpecificLocalTime instead. }
  function UTCToTZLocalTime(
    const TZ: TTimeZoneInformation;
    const utctime: TDateTime): TDateTime;

  {: Converts local time to UTC according to a current time zone. See
     TzLocalTimeToUTC for more information. }
  function LocalTimeToUTC(const loctime: TDateTime; preferDST: boolean): TDateTime;

  {: Converts UTC time to local time according to a current time zone. See
     UTCToTZLocalTime for more information. }
  function UTCToLocalTime(const utctime: TDateTime): TDateTime;

  {: Returns current UTC date and time. }
  function NowUTC: TDateTime;

  {: Returns current UTC time. }
  function TimeUTC: TDateTime;

  {: Returns current UTC date. }
  function DateUTC: TDateTime;

  {: Compares two TSystemTime records. Returns -1 if st1 < st2, 1 is st1 > st2,
     and 0 if st1 = st2. }
  function CompareSysTime(st1, st2: TSystemTime): integer;

  {: Compares two TTimeZoneInformation records. }
  function IsEqualTZ(tz1, tz2: TTimeZoneInformation): boolean;

  // added by idw =========================================================== //

  {: Converts Windows API TSystemTime structure to Delphi's TDateTime. }
  function SystemTimeToDateTime(const ST: TSystemTime): TDateTime;

  {: Converts Windows API TSystemTime to UNIX formatted UTC. }
  function SystemTimeToUnixTime(const ST: TSystemTime): longint;

  {: Converts UNIX formatted UTC to local time. }
  function UnixTimeToLocalTime(unixtime: longint): TDateTime;

implementation

  function DateEQ(const date1, date2: TDateTime): boolean;
  begin
    Result := (Abs(date1-date2) < 1/(10*MSecsPerDay));
  end; { DateEQ }

  function FixDT(const date: TDateTime): TDateTime;
  var
    ye,mo,da,ho,mi,se,ms: word;
  begin
    try
      DecodeDate(date,ye,mo,da);
      DecodeTime(date,ho,mi,se,ms);
      Result := EncodeDate(ye,mo,da)+EncodeTime(ho,mi,se,ms);
    except
      on E: EConvertError do Result := date;
      else raise;
    end;
  end; { FixDT }

  function Date2Day(const DT: TDateTime): Word;
  var
    m,y: word;
  begin
    DecodeDate(dt,y,m,Result);
  end; { Date2Day }

  function Date2Month(const DT: TDateTime): Word;
  var
    d,y: word;
  begin
    DecodeDate(dt,y,Result,d);
  end; { Date2Month }

  function Date2Year(const DT: TDateTime): Word;
  var
    d,m: word;
  begin
    DecodeDate(dt,Result,m,d);
  end; { Date2Year }

  function Time2Hr(const DT: TDateTime): Word;
  var
    min,sec,msec: word;
  begin
    DecodeTime(dt,Result,min,sec,msec);
  end;

  function Time2Min(const DT: TDateTime): Word;
  var
    hr,sec,msec: word;
  begin
    DecodeTime(dt,hr,Result,sec,msec);
  end;

  function Time2Sec(const DT: TDateTime): Word;
  var
    hr,min,msec: word;
  begin
    DecodeTime(dt,hr,min,Result,msec);
  end;

  function Time2MSec(const DT: TDateTime): Word;
  var
    hr,min,sec: word;
  begin
    DecodeTime(dt,hr,min,sec,Result);
  end;

  function IsLeapYear(Year: Word): Boolean;
  begin
    Result := ((Year and 3) = 0) and ((Year mod 100 > 0) or (Year mod 400 = 0))
  end; { IsLeapYear }

  function DateIsLeapYear(const DT: TDateTime): Boolean;
  begin
    Result := IsLeapYear(Date2Year(DT));
  end; { DateIsLeapYear }

  function DaysInMonth(const DT: TDateTime): Byte;
  begin
    case Date2Month(DT) of
      2: if DateIsLeapYear(DT) then
	   Result := 29

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?