⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcldatetime.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 3 页
字号:

{$ENDIF MSWINDOWS}

function FileTimeToStr(const FileTime: TFileTime): string;
var
  DateTime: TDateTime;
begin
  DateTime := FileTimeToDateTime(FileTime);
  Result := DateTimeToStr(DateTime);
end;

function DosDateTimeToStr(DateTime: Integer): string;
begin
  Result := DateTimeToStr(DosDateTimeToDateTime(DateTime));
end;

{$IFDEF MSWINDOWS}

// we can't do this better without copying Borland-owned code from the Delphi VCL,
// as the straight forward conversion doing exactly this task is hidden
// deeply inside SysUtils.pas.
// So the date is converted forth and back to/from Julian date
// If someone needs a faster version please take a look at SysUtils.pas->DateTimeToStr.

function SystemTimeToStr(const SystemTime: TSystemTime): string;
begin
  Result := DateTimeToStr(SystemTimeToDateTime(SystemTime));
end;

function CreationDateTimeOfFile(const Sr: TSearchRec): TDateTime;
begin
  Result := FileTimeToDateTime(Sr.FindData.ftCreationTime);
end;

function LastAccessDateTimeOfFile(const Sr: TSearchRec): TDateTime;
begin
  Result := FileTimeToDateTime(Sr.FindData.ftLastAccessTime);
end;

function LastWriteDateTimeOfFile(const Sr: TSearchRec): TDateTime;
begin
  Result := FileTimeToDateTime(Sr.FindData.ftLastWriteTime);
end;

{$ENDIF MSWINDOWS}

// Additional format tokens (also available in upper case):
// w: Week no according to ISO
// ww: Week no according to ISO forced two digits
// i: Year of the ISO-week denoted by w (4 digits for 1000..9999)
// ii: Year of the ISO-week denoted by w forced two digits
// e: Number of the Day in the ISO-week denoted by w (ISO-Notation 1=Monday...)
// f: Number of the Day in the year denoted by y
// fff: Number of the Day in the year denoted by y forced three digits

function FormatDateTime(Form: string; DateTime: TDateTime): string;
var
  N: Integer;
  ISODay, ISOWeek, ISOYear, DayOfYear, YY: Integer;

  procedure Digest;
  begin
    if N > 1 then
    begin
      Result := Result + Copy(Form, 1, N - 1);
      System.Delete(Form, 1, N - 1);
      N := 1;
    end;
  end;

begin
  ISOWeek := 0;
  DayOfYear := 0;
  Result := '';
  N := 1;
  while N <= Length(Form) do
  begin
    case Form[N] of
      '"':
        begin
          Inc(N);
          Digest;
          N := Pos('"', Form);
          if N = 0 then
          begin
            Result := Result + Form;
            Form := '';
            N := 1;
          end
          else
          begin
            Inc(N);
            Digest;
          end;
        end;
      '''':
        begin
          Inc(N);
          Digest;
          N := Pos('''', Form);
          if N = 0 then
          begin
            Result := Result + Form;
            Form := '';
            N := 1;
          end
          else
          begin
            Inc(N);
            Digest;
          end;
        end;
      'i', 'I':             //ISO Week Year
        begin
          Digest;
          if ISOWeek = 0 then
            ISOWeek := ISOWeekNumber(DateTime, ISOYear, ISoDay);
          if (Length(Form) > 1) and ((Form[2] = 'i') or (Form[2] = 'I')) then
          begin              // <ii>
            if (Length(Form) > 2) and ((Form[3] = 'i') or (Form[3] = 'I')) then
            begin
              if (Length(Form) > 3) and ((Form[4] = 'i') or (Form[4] = 'I')) then
              begin        // <iiii>
                Delete(Form, 1, 4);
                Result := Result + '"' + IntToStr(ISOYear) + '"';
              end
              else
              begin        // <iii>
                Delete(Form, 1, 3);
                Result := Result + '"' + IntToStr(ISOYear) + '"';
              end;
            end
            else
            begin           // <ii>
              Delete(Form, 1, 2);
              Result := Result + '"';
              if ISOYear < 10 then
                Result := Result + '0';
              YY := ISOYear mod 100;
              if YY < 10 then
                Result := Result + '0';
              Result := Result + IntToStr(YY) + '"';
            end;
          end
          else
          begin               // <i>
            Delete(Form, 1, 1);
            Result := Result + '"' + IntToStr(ISOYear) + '"';
          end;
        end;
      'w', 'W':              // ISO Week
        begin
          Digest;
          if ISOWeek = 0 then
            ISOWeek := ISOWeekNumber(DateTime, ISOYear, ISoDay);
          if (Length(Form) > 1) and ((Form[2] = 'w') or (Form[2] = 'W')) then
          begin               // <ww>
            Delete(Form, 1, 2);
            Result := Result + '"';
            if ISOWeek < 10 then
              Result := Result + '0';
            Result := Result + IntToStr(ISOWeek) + '"';
          end
          else
          begin               // <w>
            Delete(Form, 1, 1);
            Result := Result + '"' + IntToStr(ISOWeek) + '"';
          end;
        end;
      'e', 'E':   // ISO Week Day
        begin
          Digest;
          if ISOWeek = 0 then
            ISOWeek := ISOWeekNumber(DateTime, ISOYear, ISODay);
          Delete(Form, 1, 1);
          Result := Result + '"' + IntToStr(ISODay) + '"';
        end;
      'f', 'F':   // Day of the Year
        begin
          Digest;
          if DayOfYear = 0 then
            DayOfYear := DayOfTheYear(DateTime);
          if (Length(Form) > 1) and ((Form[2] = 'f') or (Form[2] = 'F')) then
          begin
            if (Length(Form) > 2) and ((Form[3] = 'f') or (Form[3] = 'F')) then
            begin            // <fff>
              Delete(Form, 1, 3);
              Result := Result + '"';
              if DayOfYear < 10 then
                Result := Result + '0';
              if DayOfYear < 100 then
                Result := Result + '0';
              Result := Result + IntToStr(DayOfYear) + '"';
            end
            else
            begin            // <ff>
              Delete(Form, 1, 2);
              Result := Result + '"';
              if DayOfYear < 10 then
                Result := Result + '0';
              Result := Result + IntToStr(DayOfYear) + '"';
            end;
          end
          else
          begin               // <f>
            Delete(Form, 1, 1);
            Result := Result + '"' + IntToStr(DayOfYear) + '"';
          end
        end;
    else
      Inc(N);
    end;
  end;
  Result := SysUtils.FormatDateTime(Result + Form, DateTime);
end;

// FAT has a granularity of 2 seconds
// The intervals are 1/10 of a second

function FATDatesEqual(const FileTime1, FileTime2: Int64): Boolean;
const
  ALLOWED_FAT_FILE_TIME_VARIATION = 20;
begin
  Result := Abs(FileTime1 - FileTime2) <= ALLOWED_FAT_FILE_TIME_VARIATION;
end;

function FATDatesEqual(const FileTime1, FileTime2: TFileTime): Boolean;
begin
  Result := FATDatesEqual(Int64(FileTime1), Int64(FileTime2));
end;

// Conversion Unix time <--> TDateTime / FileTime, constants

{$IFDEF MSWINDOWS}
const
  // 1 second in FileTime resolution
  FileTimeSecond = 1000 * 1000 * 10;
  // 1 day in FileTime resolution: 24 * 60 * 60 * 1000 * 1000 * 10;
  FileTimeDay = 864000000000;

  // 1601-01-01T00:00:00 in TDateTime
  FileTimeStart = -109205;
  // Time between 1601-01-01 and 1970-01-01 in FileTime resolution
  FileTimeUnixStart = (UnixTimeStart - FileTimeStart) * FileTimeDay;
{$ENDIF MSWINDOWS}

// Conversion Unix time <--> TDateTime

function DateTimeToUnixTime(DateTime : TDateTime) : TJclUnixTime32;
begin
  Result := Trunc((DateTime-UnixTimeStart) * SecondsPerDay);
end;

function UnixTimeToDateTime(const UnixTime: TJclUnixTime32): TDateTime;
begin
  Result:= UnixTimeStart + (UnixTime / SecondsPerDay);
end;

// Conversion Unix time <--> FileTime

{$IFDEF MSWINDOWS}

function UnixTimeToFileTime(const AValue: TJclUnixTime32): TFileTime;
begin
  Result := DateTimeToFileTime(UnixTimeToDateTime(AValue));
end;

function FileTimeToUnixTime(const AValue: TFileTime): TJclUnixTime32;
begin
 Result := DateTimeToUnixTime(FileTimeToDateTime(AValue));
end;

{$ENDIF MSWINDOWS}

// History:

// $Log: JclDateTime.pas,v $
// Revision 1.20  2005/03/09 23:09:01  rrossmair
// - published UnixTimeStart constant
//
// Revision 1.19  2005/03/08 08:33:16  marquardt
// overhaul of exceptions and resourcestrings, minor style cleaning
//
// Revision 1.18  2005/02/24 16:34:39  marquardt
// remove divider lines, add section lines (unfinished)
//
// Revision 1.17  2005/02/12 16:29:53  mthoma
// Linux version of DateTimeToLocalDateTime and LocalDateTimeToDateTime  added.
//
// Fixed #0002500 JclDateTime.FormatDateTime returns incorrect week result
//
// Revision 1.16  2004/10/19 06:26:48  marquardt
// JclRegistry extended, JclNTFS made compiling, JclDateTime style cleaned
//
// Revision 1.15  2004/10/17 20:05:31  mthoma
// style cleaned.
//
// Revision 1.14  2004/10/17 19:43:44  mthoma
// Wrote "placeholders" for FileTimeToUnixTime, UnixTimeToFileTime until someone writes a better cleanroom solution. Rewrote ISOWeekNumber and ISOWeekToDateTime. Introduced new functions: GetISOYearNumberOfDays,
// IsISOLongYear, ISODayOfWeek.
//
// Revision 1.13  2004/10/15 14:41:00  rrossmair
// restored Kylix compatibility
//
// Revision 1.12  2004/10/15 03:36:46  rrossmair
// - rearranged pre-CVS history
//
// Revision 1.11  2004/10/14 14:38:50  rikbarker
// Added DateTimeToUnixTime
// Rewrote UnixTimeToDateTime to remove PH Code
// Removed unnecessary constants and moved the relevant ones to the top of the unit
//
// Revision 1.10  2004/07/29 07:58:20  marquardt
// inc files updated
//
// Revision 1.9  2004/07/28 18:00:49  marquardt
// various style cleanings, some minor fixes
//
// Revision 1.8  2004/06/14 13:05:16  marquardt
// style cleaning ENDIF, Tabs
//
// Revision 1.7  2004/05/31 01:54:38  rrossmair
// $IFDEF LINUX replaced by $IFDEF HAS_UNIT_LIBC
//
// Revision 1.6  2004/05/05 00:04:10  mthoma
// Updated headers: Added donors as contributors, adjusted the initial authors, added cvs names when they were not obvious. Changed $data to $date where necessary,
//
// Revision 1.5  2004/04/08 18:14:00  mthoma
// Fixed 402, 403, 1045, 236 (all DateTimeToLocalDateTime and vice versa problems),
// changed $data$ to $date$, removed the todoc statements, changed function prototypes from Value
// to a more JclDateTime like naming.
//
// 2001-02-10, Michael Schnell
//  added overload procedures for compatibility:
//    DateTimeToSystemTime, DosDateTimeToFileTime, FileTimeToDosDateTime,
//    FileTimeToSystemTime, SystemTimeToFileTime
//
// 2000-10-19, Michael Schnell
//  changed EasterSunday to the code by Marc Convents (marc dott convents att progen dott be)
//
// 2000-10-15, Michael Schnell
//  avoiding "absolute" (in locations where stated)
//  extended functionality for MakeYear4Digit: can pass Result unchanged if appropriate
//  added function FATDatesEqual
//
// 2000-09-18, Michael Schnell
//  added function FormatDateTime
//
// 2000-09-14, Michael Schnell
//  added functions DayOfTheYear and DayOfTheYearToDateTime
//
// 2000-09-12, Michael Schnell:
//  more elegant code for ISOWeekNumber
//  added ISOWeekToDateTime
//  added overload for ISOWeekNumber with three integer parameters
//
// 2000-09-07, Michael Schnell:
//  added ISOWeekNumber with 1 and 3 parameters
//
// 2000-08-28, Michael Schnell:
//  added function MakeYear4Digit
//
// 2000-08-09, Michael Schnell:
//  added functions
//  CreationDateTimeOfFile, LastAccessDateTimeOfFile and LastWriteDateTimeOfFile
//
// 2000-07-16, Michael Schnell:
//  Support for negative dates and Year >= 10000 added for DecodeDate and EncodeDate
//
// 2000-07-08, Michael Schnell:
//  Swapped function names CenturyOfDate and CenturyBaseYear
//  those were obviously called wrong before
//  Attention: must be done in the Help, too
//
// 2000-07-06, Michael Schnell:
//  Formatted according to style rules
//
// 2000-06-25, Michael Schnell:
//  Added function SystemTimeToFileTime
//  Added function FieTimeToSystemTime
//  Added function Datetimetosystemtime
//  Added function DosDateTimeToFileTime
//  Added function FileTimeToDosDateTime
//  Added function SystemTimeToStr
//
// 2000-06-24, Michael Schnell:
//  DateTimeToDosDateTime performs the same action as SysUtils.DateTimeToFileDate
//  so let's have Delphi do the work here
//  DosDateTimeToDateTime performs the same action as SysUtils.FileDateToDateTime
//  so let's have Delphi do the work here
//
//  DosDateTimeToStr does not use FileTime any more
//
//  Added function DateTimeToFileTime
//  Added function LocalDateTimeToFileTime
//  Changed function  FileTimeToDateTime
//    not using TSystemDate and avoid systemcalls
//  Changed function  FileTimeToLocalDateTime
//    not using TSystemDate and avoid systemcalls
//
// 2000-06-22, Michael Schnell:
//  Name changed GetCenturyOfDate -> CenturyOfDate
//  Name changed GetCenturyBaseYear -> CenturyBaseYear
//
//  function GetWeekNumber(Today: TDateTime): string;  ->
//  function ISOWeekNumber(DateTime: TDateTime; var YearOfWeekDay: Integer): Integer;
//
//  Added overload function IsLeapYear(Year: Integer): Boolean;
//  to avoid wrong results if the user thinks he calls SysUtils.IsLeapYear
//  IsLeapYear is now using SysUtils.IsLeapYear
//
//  Changed function DateTimeToSeconds(DateTime: TDateTime): extended; ->
//  function TimeOfDateTimeToSeconds(DateTime: TDateTime): Integer;
//  now not calling DecodeTime any more
//
//  Added function TimeOfDateTimeToMSecs(DateTime: TDateTime): Integer

end.

⌨️ 快捷键说明

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