注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

珠海渔郎之电子网档

项目管理 + 程序开发 + Delphi + 电脑应用 + 数码 + 进化感悟

 
 
 

日志

 
 

日期和时间函数  

2009-11-25 13:22:42|  分类: Delphi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
 ////////////////////////////////////////////////////////////////////////////// // Date/Time function & procedure //////////////////////////////////////////////////////////////////////////////

function GMTNow: TDateTime; { 返回当前的系统的GMT/UTC时间 } begin Result := LocaleToGMT(Now); end;

const MinsPerDay = 24 * 60;

function GetGMTBias: Integer; var inf TTimeZoneInformation; Mode: DWord; begin Mode := GetTimeZoneInformation(info); Result := info.Bias; case Mode of TIME_ZONE_ID_INVALID: RaiseLastOSError; TIME_ZONE_ID_STANDARD: Result := Result + info.StandardBias; TIME_ZONE_ID_DAYLIGHT: Result := Result + info.DaylightBias; end; end;

function LocaleToGMT(const Value: TDateTime): TDateTime; { 把本地时间Value转换成GMT/UTC时间 } begin Result := Value + (GetGMTBias / MinsPerDay); end;

function GMTToLocale(const Value: TDateTime): TDateTime; { 把GMT/UTC时间Value转换成本地时间 } begin Result := Value - (GetGMTBias / MinsPerDay); end;

function SubTimeToMinutes(StartT, EndT: TDateTime): integer; { 返回两个时间分钟之差,StartT应该少于EndT,否则返回负数值 } begin Result := Round((EndT - StartT) * 24 * 60); end;

function SubTimeToSeconds(StartT, EndT: TDateTime): integer; { 返回两个时间的秒数之差,StartT应该小于Endt,否则返回负数值 } begin Result := Round((EndT - StartT) * 24 * 60 * 60); end;

function GetAge(Birthday: TDateTime): Integer; { 返回Birthday相对于当前日期的年龄值 } var Month, Day, Year, CurrentYear, CurrentMonth, CurrentDay: Word; begin DecodeDate(Birthday, Year, Month, Day); DecodeDate(Now, CurrentYear, CurrentMonth, CurrentDay);

if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then begin Result := 0; end else begin Result := CurrentYear - Year; if (Month > CurrentMonth) then Dec(Result) else begin if Month = CurrentMonth then if (Day > CurrentDay) then Dec(Result); end; end; end;

function SameTime(const Time1, Time2: TDateTime): Boolean; { 比较两个时间是否相等,Delphi 6本身有这个函数,精确到毫秒 } begin Result := Abs(Time - Time2) < 1E-9; end;

function NearTime(const Time1, Time2: TDateTime): Boolean; stdcall; { 检查两个时间是否相近似,精确到秒 } begin Result := Abs(Time - Time2) < 1E-7; end;

function MSecondsToString(MSeconds: int64): string; stdcall; { 毫秒转换成XX天XX小时XX分钟XX秒的格式 } const MSecPerDay: Integer = 1000 * 60 * 60 * 24; MSecPerHour: Integer = 1000 * 60 * 60; MSecPerMinute: Integer = 1000 * 60; MSecPerSecond: integer = 1000; var D, H, M, S: integer; begin D := MSeconds div MSecPerDay; MSeconds := MSeconds mod MSecPerDay; if D > 0 then Result := IntToStr(D) + ' Days ';

H := MSeconds div MSecPerHour; MSeconds := MSeconds mod MSecPerHour; if H > 0 then Result := Result + IntToStr(H) + ' Hours ';

M := MSeconds div MSecPerMinute; MSeconds := MSeconds mod MSecPerMinute; if M > 0 then Result := Result + IntToStr(M) + ' Mintues ';

S := MSeconds div MSecPerSecond; if S > 0 then Result := Result + IntToStr(S) + ' Seconds'; end;

function DateTimeToStringEx(Value: TDateTime): string; stdcall; { DateTime转换成X天X小时X分钟X秒的格式 } var D, H, M, S: integer; begin D := Trunc(Value); Value := Value - D; if D > 0 then Result := IntToStr(D) + ' Days ';

H := Trunc(Value * 24); Value := Value - 1 / 24 * H; if H > 0 then Result := Result + IntToStr(H) + ' Hours ';

M := Trunc(Value * 24 * 60); Value := Value - 1 / (24 * 60) * M; if M > 0 then Result := Result + IntToStr(M) + ' Mintues ';

S := Trunc(Value * 24 * 60 * 60); // Value := Value - 1 / (24 * 60 * 60) * S; if S > 0 then Result := Result + IntToStr(S) + ' Seconds '; end;

function SecondsToString(Seconds: integer): string; stdcall; { 秒数转换成mm:ss的格式 } begin Result := TimeToStr(Seconds / 86400); end;

function FileTimeToString(Value: _FILETIME): string; { Convert File Time To display string } var SysTime: _SYSTEMTIME; begin FileTimeToSystemTime(Value, SysTime); with SysTime do begin Dec(wYear, 1601); Dec(wMonth, 1); Dec(wDay, 1); if wYear > 0 then if wYear > 1 then Result := IntToStr(wYear) + ' years ' else Result := IntToStr(wYear) + ' years ';

if wMonth > 0 then if wMonth > 1 then Result := Result + IntToStr(wMonth) + ' months ' else Result := Result + IntToStr(wMonth) + ' month ';

if whour > 0 then if whour > 1 then Result := Result + IntToStr(whour) + ' hours ' else Result := Result + IntToStr(wHour) + ' hour ';

if wminute > 0 then if wminute > 1 then Result := Result + IntToStr(wminute) + ' minutes ' else Result := Result + IntToStr(wYear) + ' minute ';

if wsecond > 0 then if wsecond > 1 then Result := Result + IntToStr(wsecond) + ' seconds ' else Result := Result + IntToStr(wsecond) + ' second ';

if wMilliseconds > 0 then if wMilliseconds > 1 then Result := Result + IntToStr(wMilliseconds) + ' Milliseconds ' else Result := Result + IntToStr(wMilliseconds) + ' Milliseconds '; end; end;

  评论这张
 
阅读(396)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017