• System.DateUtils 4. IsValidDateTime... 有效时间判断


    编译版本:Delphi XE7

    function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
    function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
    function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean; inline;
    function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
    function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean; {ISO 8601}
    function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean; {ISO 8601x}

    implementation

    *注 Word类型取值范围:0--65535,故无需判断负数

    // 判断是否为有效日期

    function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
    begin
      Result := (AYear >= 1) and (AYear <= 9999) and
                (AMonth >= 1) and (AMonth <= 12) and
                (ADay >= 1) and (ADay <= DaysInAMonth(AYear, AMonth));
    end;

    // 获取当前月份日期数,因为存在闰年故需传入年份,引用单元 System.DateUtils

    function DaysInAMonth(const AYear, AMonth: Word): Word;
    begin
      Result := MonthDays[(AMonth = 2) and IsLeapYear(AYear), AMonth];
    end;

    // 月份日期数列表,引用单元 System.SysUtils

    const
      MonthDays: array [Boolean] of TDayTable =
        ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
         (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));

    // 判断是否为有效时间

    function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
    begin
      Result := ((AHour < HoursPerDay) and (AMinute < MinsPerHour) and (ASecond < SecsPerMin) and (AMilliSecond < MSecsPerSec)) or // 每个时间类型都在取值范围内
                ((AHour = 24) and (AMinute = 0) and (ASecond = 0) and (AMilliSecond = 0)); // midnight early next day // 日期切换时间点
    end;

    // 时间单位,引用单元 System.SysUtils
    HoursPerDay   = 24; // 每天的小时数
    MinsPerHour   = 60; // 每天的分钟数
    SecsPerMin    = 60; // 每分的秒数
    MSecsPerSec   = 1000; // 每秒的毫秒数

    // 判断是否为有效日期、时间

    function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
    begin
      Result := IsValidDate(AYear, AMonth, ADay) and
                IsValidTime(AHour, AMinute, ASecond, AMilliSecond);
    end;

    // 判断指定年份的日期数是否有效

    function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
    begin
      Result := (AYear >= 1) and (AYear <= 9999) and
                (ADayOfYear >= 1) and (ADayOfYear <= DaysInAYear(AYear));
    end;

    // 获取该年的日期数

    function DaysInAYear(const AYear: Word): Word;
    begin
      Result := DaysPerYear[IsLeapYear(AYear)];
    end;

    // 年份日期数列表,引用单元 System.DateUtils

    DaysPerYear: array [Boolean] of Word = (365, 366);

    // 判断指定年份的周数、周几在周范围是否有效

    function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
    begin
      Result := (AYear >= 1) and (AYear <= 9999) and
                (AWeekOfYear >= 1) and (AWeekOfYear <= WeeksInAYear(AYear)) and
                (ADayOfWeek >= DayMonday) and (ADayOfWeek <= DaySunday);
    end;

    // 获取一年有几周

    function WeeksInAYear(const AYear: Word): Word;
    var
      LDayOfWeek: Word;
    begin
      Result := 52;
      LDayOfWeek := DayOfTheWeek(EncodeDate(AYear, 1, 1));
      if (LDayOfWeek = DayThursday) or
         ((LDayOfWeek = DayWednesday) and IsLeapYear(AYear)) then
        Inc(Result);
    end;

    // 将指定的年、月、日转为日期、时间格式
    function EncodeDate(Year, Month, Day: Word): TDateTime;
    begin
      if not TryEncodeDate(Year, Month, Day, Result) then
        ConvertError(@SDateEncodeError);
    end;

    // 尝试将指定的年、月、日转为日期、时间格式

    function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
    var
      I: Integer;
      DayTable: PDayTable;
    begin
      Result := False;
      DayTable := @MonthDays[IsLeapYear(Year)];
      if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
        (Day >= 1) and (Day <= DayTable^[Month]) then
      begin
        for I := 1 to Month - 1 do Inc(Day, DayTable^[I]);
        I := Year - 1;
        Date := I * 365 + I div 4 - I div 100 + I div 400 + Day - DateDelta;
        Result := True;
      end;
    end;

    // 获取指定日期是周几

    function DayOfTheWeek(const AValue: TDateTime): Word;
    begin
      Result := (DateTimeToTimeStamp(AValue).Date - 1) mod 7 + 1;
    end;

    // 将日期、时间格式转换为时间戳格式

    function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
    {$IF defined(PUREPASCAL) or defined(MACOS)}
    var
      LTemp, LTemp2: Int64;
    begin
      LTemp := Round(DateTime * FMSecsPerDay);
      LTemp2 := (LTemp div IMSecsPerDay);
      Result.Date := DateDelta + LTemp2;
      Result.Time := Abs(LTemp) mod IMSecsPerDay;
    end;

    // 判断是否为有效的年、月、月内第几周、周几

    function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
    begin
      Result := (AYear >= 1) and (AYear <= 9999) and
                (AMonth >= 1) and (AMonth <= 12) and
                (AWeekOfMonth >= 1) and (AWeekOfMonth <= 5) and // 特殊情况下,最大周应该为6周;除非计算方式为满周,即无论1号为周几,均满7天算一周
                (ADayOfWeek >= DayMonday) and (ADayOfWeek <= DaySunday);
    end;

    // 周几,引用单元 System.DateUtils

    DayMonday = 1;
    DayTuesday = 2;
    DayWednesday = 3;
    DayThursday = 4;
    DayFriday = 5;
    DaySaturday = 6;
    DaySunday = 7;

  • 相关阅读:
    Atitti. 语法树AST、后缀表达式、DAG、三地址代码
    Atitit.antlr实现词法分析
    Atitit.antlr实现词法分析
    Atitit.词法分析的理论原理 part2
    Atitit.词法分析的理论原理 part2
    atitit.词法分析原理 词法分析器 (Lexer)
    atitit.词法分析原理 词法分析器 (Lexer)
    Atitti.数据操作crud js sdk dataServiceV3设计说明
    Atitti.数据操作crud js sdk dataServiceV3设计说明
    Atitit.http代理的实现 代码java php c# python
  • 原文地址:https://www.cnblogs.com/BlackList-Sakura/p/4760722.html
Copyright © 2020-2023  润新知