Delphi10时间、计时相关方法之 -软件的基本是要处理好”算法“及其基础(四)

目录

Delphi10时间、计时相关方法之 -软件的基本是要处理好”算法“及其基础(四)

一、计时

1.1、粗犷计时类TTimer

1.2、精确计时类TStopwatch

1.3、时间跨度类TTimeSpan

二、时间

2.1、TDateTime

2.2、DateTimeToOleAutoString

2.3、VarToDateTime

2.4、VarFromDateTime

2.5、DateTimeToWinFileDate

2.6、系统工具单元SysUtils中相关日期时间

2.6.1、方法(函数、过程)

2.6.2、日期时间格式化记录(结构体)TFormatSettings的格式化字符串Format: 

2.6.3、Unix日期时间格式与Delphi格式互转

喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享:


Delphi10时间、计时相关方法之 -软件的基本是要处理好”算法“及其基础(四)

一、计时

1.1、粗犷计时类TTimer

      uses FMX.Types;  //: for FMX

      uses Vcl.ExtCtrls;  //: for VCL

1.2、精确计时类TStopwatch

      uses System.Diagnostics;

1.3、时间跨度类TTimeSpan

      uses System.TimeSpan;

二、时间

2.1、TDateTime

  TDateTime = type Double;
  PDateTime = ^TDateTime;

  TDate = type TDateTime;
  TTime = type TDateTime;

      uses System ;

2.2、DateTimeToOleAutoString

  function DateTimeToOleAutoString(const DateTime: TDateTime): string; inline;

      uses System.VarUtils;  //: Variant Utilities Unit

2.3、VarToDateTime

  function VarToDateTime(const V: Variant): TDateTime;

      uses System.Variants;

2.4、VarFromDateTime

function VarFromDateTime(const DateTime: TDateTime): Variant;

      uses System.Variants;

2.5、DateTimeToWinFileDate

  function DateTimeToWinFileDate(DateTime: TDateTime): UInt32;

  function WinFileDateToDateTime(FileDate: UInt32; out DateTime: TDateTime): Boolean;

      uses System.Zip;

2.6、系统工具单元SysUtils中相关日期时间

2.6.1、方法(函数、过程)

{ Date and time record 天、时间的记录}

  TTimeStamp = record
    Time: Integer;      { Number of milliseconds since midnight }
    Date: Integer;      { One plus number of days since 1/1/0001 }
  end;

      uses System.SysUtils;

{ Date encoding and decoding日期编解码 }

  function EncodeDate(Year, Month, Day: Word): TDateTime;

  function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;

  procedure DecodeDate(const DateTime: TDateTime; var Year, Month, Day: Word);

  function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;  //:含闰年处理

  function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;

  function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;

{ Time encoding and decoding时间编解码 }

  function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;

  function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;

  function TimeStampToMSecs(const TimeStamp: TTimeStamp): Comp;  // : Comp  :  uses System ;  // : 时间截转毫秒数

  function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;                   // : Comp  :  uses System ;  // : 毫秒数转时间截

  procedure DecodeTime(const DateTime: TDateTime; var Hour, Min, Sec, MSec: Word);

{$IFDEF MSWINDOWS}

  procedure DateTimeToSystemTime(const DateTime: TDateTime; var SystemTime: TSystemTime);

  function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;

  function TrySystemTimeToDateTime(const SystemTime: TSystemTime; out DateTime: TDateTime): Boolean;

{$ENDIF MSWINDOW}

  function DayOfWeek(const DateTime: TDateTime): Word;

  function Date: TDateTime;

  function Time:TDateTime;

  function Now: TDateTime;  //:  corresponding to 当前Date + Time

  function CurrentYear: Word;     //:  corresponding to 当前时间Now的年份

  function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1): TDateTime;

  procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);

  procedure ReplaceTime(var DateTime: TDateTime; const NewTime: TDateTime);

  procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);

  function IsLeapYear(Year: Word): Boolean;  //:某年是否闰年

  function DateToStr(const DateTime: TDateTime): string; overload; inline;
  function DateToStr(const DateTime: TDateTime;  const AFormatSettings: TFormatSettings): string; overload; inline;

  function TimeToStr(const DateTime: TDateTime): string; overload; inline;
  function TimeToStr(const DateTime: TDateTime;  const AFormatSettings: TFormatSettings): string; overload; inline;

  function DateTimeToStr(const DateTime: TDateTime): string; overload; inline;
  function DateTimeToStr(const DateTime: TDateTime;  const AFormatSettings: TFormatSettings): string; overload; inline;

  function StrToDate(const S: string): TDateTime; overload; inline;
  function StrToDate(const S: string;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function StrToDateDef(const S: string;  const Default: TDateTime): TDateTime; overload; inline;
  function StrToDateDef(const S: string; const Default: TDateTime;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function TryStrToDate(const S: string; out Value: TDateTime): Boolean; overload; inline;
  function TryStrToDate(const S: string; out Value: TDateTime;  const AFormatSettings: TFormatSettings): Boolean; overload;

  function StrToTime(const S: string): TDateTime; overload; inline;
  function StrToTime(const S: string;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function StrToTimeDef(const S: string;  const Default: TDateTime): TDateTime; overload; inline;
  function StrToTimeDef(const S: string; const Default: TDateTime;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function TryStrToTime(const S: string; out Value: TDateTime): Boolean; overload; inline;
  function TryStrToTime(const S: string; out Value: TDateTime;  const AFormatSettings: TFormatSettings): Boolean; overload;

  function StrToDateTime(const S: string): TDateTime; overload; inline;
  function StrToDateTime(const S: string;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function StrToDateTimeDef(const S: string;  const Default: TDateTime): TDateTime; overload; inline;
  function StrToDateTimeDef(const S: string; const Default: TDateTime;  const AFormatSettings: TFormatSettings): TDateTime; overload;

  function TryStrToDateTime(const S: string;  out Value: TDateTime): Boolean; overload; inline;
  function TryStrToDateTime(const S: string; out Value: TDateTime;  const AFormatSettings: TFormatSettings): Boolean; overload;

    //:::在使用TFormatSettings时,应当注意考虑其全局变量和局部变量使用的线程安全问题!!!:::

  procedure DateTimeToString(var Result: string; const Format: string;  DateTime: TDateTime); overload; inline;
  procedure DateTimeToString(var Result: string; const Format: string;  DateTime: TDateTime; const AFormatSettings: TFormatSettings); overload; 

  const
      MinDateTime: TDateTime = -657434.0;      { 01/01/0100 12:00:00.000 AM }
      MaxDateTime: TDateTime =  2958465.99999; { 12/31/9999 11:59:59.999 PM }

  function FloatToDateTime(const Value: Extended): TDateTime;
  function TryFloatToDateTime(const Value: Extended; out AResult: TDateTime): Boolean;

2.6.2、日期时间格式化记录(结构体)TFormatSettings的格式化字符串Format: 

{ FormatDateTime formats the date-and-time value given by DateTime using the
  format given by Format. The following format specifiers are supported:

  c       Displays the date using the format given by the ShortDateFormat
          global variable, followed by the time using the format given by
          the LongTimeFormat global variable. The time is not displayed if
          the fractional part of the DateTime value is zero.

  d       Displays the day as a number without a leading zero (1-31).

  dd      Displays the day as a number with a leading zero (01-31).

  ddd     Displays the day as an abbreviation (Sun-Sat) using the strings
          given by the ShortDayNames global variable.

  dddd    Displays the day as a full name (Sunday-Saturday) using the strings
          given by the LongDayNames global variable.

  ddddd   Displays the date using the format given by the ShortDateFormat
          global variable.

  dddddd  Displays the date using the format given by the LongDateFormat
          global variable.

  g       Displays the period/era as an abbreviation (Japanese and
          Taiwanese locales only).

  gg      Displays the period/era as a full name.

  e       Displays the year in the current period/era as a number without
          a leading zero (Japanese, Korean and Taiwanese locales only).

  ee      Displays the year in the current period/era as a number with
          a leading zero (Japanese, Korean and Taiwanese locales only).

  m       Displays the month as a number without a leading zero (1-12). If
          the m specifier immediately follows an h or hh specifier, the
          minute rather than the month is displayed.

  mm      Displays the month as a number with a leading zero (01-12). If
          the mm specifier immediately follows an h or hh specifier, the
          minute rather than the month is displayed.

  mmm     Displays the month as an abbreviation (Jan-Dec) using the strings
          given by the ShortMonthNames global variable.

  mmmm    Displays the month as a full name (January-December) using the
          strings given by the LongMonthNames global variable.

  yy      Displays the year as a two-digit number (00-99).

  yyyy    Displays the year as a four-digit number (0000-9999).

  h       Displays the hour without a leading zero (0-23).

  hh      Displays the hour with a leading zero (00-23).

  n       Displays the minute without a leading zero (0-59).

  nn      Displays the minute with a leading zero (00-59).

  s       Displays the second without a leading zero (0-59).

  ss      Displays the second with a leading zero (00-59).

  z       Displays the millisecond without a leading zero (0-999).

  zzz     Displays the millisecond with a leading zero (000-999).

  t       Displays the time using the format given by the ShortTimeFormat
          global variable.

  tt      Displays the time using the format given by the LongTimeFormat
          global variable.

  am/pm   Uses the 12-hour clock for the preceding h or hh specifier, and
          displays 'am' for any hour before noon, and 'pm' for any hour
          after noon. The am/pm specifier can use lower, upper, or mixed
          case, and the result is displayed accordingly.

  a/p     Uses the 12-hour clock for the preceding h or hh specifier, and
          displays 'a' for any hour before noon, and 'p' for any hour after
          noon. The a/p specifier can use lower, upper, or mixed case, and
          the result is displayed accordingly.

  ampm    Uses the 12-hour clock for the preceding h or hh specifier, and
          displays the contents of the TimeAMString global variable for any
          hour before noon, and the contents of the TimePMString global
          variable for any hour after noon.

  /       Displays the date separator character given by the DateSeparator
          global variable.

  :       Displays the time separator character given by the TimeSeparator
          global variable.

  'xx'    Characters enclosed in single or double quotes are displayed as-is,
  "xx"    and do not affect formatting.

  Format specifiers may be written in upper case as well as in lower case
  letters--both produce the same result.

  If the string given by the Format parameter is empty, the date and time
  value is formatted as if a 'c' format specifier had been given.

  The following example:

    S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' +
      '"at" hh:mm AM/PM', StrToDateTime('2/15/95 10:30am'));

  assigns 'The meeting is on Wednesday, February 15, 1995 at 10:30 AM' to
  the string variable S. }

  function FormatDateTime(const Format: string;  DateTime: TDateTime): string; overload; inline;
  function FormatDateTime(const Format: string; DateTime: TDateTime;  const AFormatSettings: TFormatSettings): string; overload;

  { System Locale information record }
      TSysLocale = packed record
          DefaultLCID: TLocaleID;  //:本地语言的ID
          PriLangID: Integer;
          SubLangID: Integer;
          FarEast: Boolean;
          MiddleEast: Boolean;
      end;

  { This is used by TLanguages }
       TLangRec = packed record
             FName: string;        //:本地语言的名称
             FLCID: TLocaleID; //:本地语言的ID
             FExt: string;
             FLocaleName: string;
        end;

         TLanguages = class  //:语言类

             //.............(内部方法略)

  procedure GetFormatSettings;  //:特别注意使用时的线程安全性:它获得了SysLocale.DefaultLCID,并通过SysLocale.DefaultLCID改变了全局变量TFormatSettings的值。

  function LCIDToCodePage(const ALCID: LCID): Integer;   //:通过LCID取得“代码页”

      uses System.SysUtils;

2.6.3、Unix日期时间格式与Delphi格式互转

Unix系统时间格式与delphi系统时间格式的互转》见本博客:https://blog.csdn.net/pulledup/article/details/105715485

本客户相关博文:

      1.《软件的基本是要处理好”算法“及其基础(一)流-字-字符(包括某个数字、字母、符号和某个汉字等)-字符串-字节动态数组-字节-整数之间的转化关系和算法

      2.《软件的基本是要处理好”算法“及其基础(二)delphi系统原子函数及方法》

      3.《软件的基本是要处理好”算法“及其基础(三)delphi 10.4.1字符串工具单元及其函数

喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享:

猜你喜欢

转载自blog.csdn.net/pulledup/article/details/114005367
今日推荐