delphi XE内置时区功能的Unix系统时间格式与delphi系统时间格式的互转
一、不带毫秒的日期时间类型
直接用系统自带的即可:
uses System.DateUtils, System.StrUtils;
///<remarks>AValueUnix13Bit或delphi原生的10位AValue调用函数UnixBitToDateTime,
///AReturnUTC:均应设置-false,否则默认的UTC全世界同一时间与你有时差,
///只有世界集会公告等场景时才以AReturnUTC=True传入 </remarks>
function DateTimeToUnix(const AValue: TDateTime; AInputIsUTC: Boolean = True): Int64;
///<remarks>AValueUnix13Bit或delphi原生的10位AValue调用函数UnixBitToDateTime,
///AReturnUTC:均应设置-false,否则默认的UTC全世界同一时间与你有时差,
///只有世界集会公告等场景时才以AReturnUTC=True传入 </remarks>
function UnixToDateTime(const AValue: Int64; AReturnUTC: Boolean = True): TDateTime;
二、带毫秒的日期时间类型
代码如下:
//工程单元:
program YourProgram;
{$R *.dres}
uses
System.StartUpCopy,
System.SysUtils;
//..............
{$R *.res}
begin
ReportMemoryLeaksOnShutdown := True; //只要有内存泄漏就会报
//ReportMemoryLeaksOnShutdown := DebugHook<>0; //在真正的debug时,闭关窗口会报内存泄漏
// =========统一所有的时间日期格式 注意,在xp操作系统上不衩支持,使用之后,查询不出数据了
with System.SysUtils.FormatSettings do
begin
ShortDateFormat := 'yyyy-mm-dd';
LongDateFormat := 'yyyy-mm-dd';
ShortTimeFormat := 'hh:mm:ss.zzz';
LongTimeFormat := 'hh:mm:ss.zzz';
DateSeparator := '-';
TimeSeparator := ':';
end;
Application.Initialize;
Application.CreateForm(TfmxMain, fmxMain);
Application.Run;
end.
///<summary>将带毫秒格式长时间格式的日期时间类型转化为Unix下13位表达的Int64整数:</summary>
///<param name="ADateTime">:带毫秒格式长时间格式的日期时间类型:前提需要事先设置好全局FormatSettings记录的格式</param>
///<returns>Unix下13位表达的Int64整数</returns>
///<remarks>AValueUnix13Bit或delphi原生的10位AValue调用函数UnixBitToDateTime,
///AReturnUTC:均应设置-false,否则默认的UTC全世界同一时间与你有时差,
///只有世界集会公告等场景时才以AReturnUTC=True传入 </remarks>
function DateTimeToUnix13Bit(const ADateTime:TDateTime; AReturnUTC: Boolean = false):Int64;
begin
Result:=
DateTimeToUnix(StrToDatetime(FormatDateTime('yyyy-mm-dd hh:mm:ss.zzz',ADateTime)),AReturnUTC)*1000
+(
DateTimeToMilliseconds(StrToDatetime(FormatDateTime('yyyy-mm-dd hh:mm:ss.zzz',ADateTime)))
-DateTimeToMilliseconds(StrToDatetime(FormatDateTime('yyyy-mm-dd hh:mm:ss',ADateTime)))
);
end;
///<summary>将为Unix下13位表达的Int64整数转化位带毫秒格式长时间格式的日期时间类型的字符串:</summary>
///<param name="AValueUnix13Bit">:Unix下13位表达的Int64:前提需要事先设置好全局FormatSettings记录的格式</param>
///<returns>返回带毫秒长时间格式的日期时间字符串</returns>
///<remarks>AValueUnix13Bit或delphi原生的10位AValue调用函数UnixBitToDateTime,
///AReturnUTC:均应设置-false,否则默认的UTC全世界同一时间与你有时差,
///只有世界集会公告等场景时才以AReturnUTC=True传入 </remarks>
function Unix13BitToDateTimeStr(const AValueUnix13Bit: Int64; AReturnUTC: Boolean = false): string;
var LValueUnix13Bit: Int64;
begin
if Length(IntToStr(AValueUnix13Bit)) =10 then //10位Unix时间截
begin
LValueUnix13Bit:= StrToInt64( IntToStr(AValueUnix13Bit)+'000' )
end else
begin
LValueUnix13Bit :=AValueUnix13Bit;
end;
Result:=
FormatDateTime('yyyy-mm-dd hh:mm:ss',
UnixToDateTime(
LValueUnix13Bit.ToString.Substring(
0,Length(LValueUnix13Bit.ToString)-3 ).ToInt64 ,AReturnUTC )
)
+'.'+
(LValueUnix13Bit.ToString.Substring(
Length(LValueUnix13Bit.ToString)-3,Length(LValueUnix13Bit.ToString) ));
end;
///<summary>将为Unix下13位表达的Int64整数转化位带毫秒格式长时间格式的日期时间类型:</summary>
///<param name="AValueUnix13Bit">:Unix下13位表达的Int64:前提需要事先设置好全局FormatSettings记录的格式</param>
///<returns>返回带毫秒长时间格式的日期时间类型</returns>
///<remarks>AValueUnix13Bit或delphi原生的10位AValue调用函数UnixBitToDateTime,
///AReturnUTC:均应设置-false,否则默认的UTC全世界同一时间与你有时差,
///只有世界集会公告等场景时才以AReturnUTC=True传入 </remarks>
function Unix13BitToDateTime(const AValueUnix13Bit: Int64; AReturnUTC: Boolean = false): TDateTime;
var LValueUnix13Bit: Int64;
begin
if Length(IntToStr(AValueUnix13Bit)) =10 then //10位Unix时间截
begin
LValueUnix13Bit:= StrToInt64( IntToStr(AValueUnix13Bit)+'000' )
end else
begin
LValueUnix13Bit :=AValueUnix13Bit;
end;
Result:=
StrToDateTime(
FormatDateTime('yyyy-mm-dd hh:mm:ss',
UnixToDateTime(
LValueUnix13Bit.ToString.Substring(
0,Length(LValueUnix13Bit.ToString)-3 ).ToInt64 ,AReturnUTC )
)
+'.'+
(LValueUnix13Bit.ToString.Substring(
Length(LValueUnix13Bit.ToString)-3,Length(LValueUnix13Bit.ToString) ))
);
end;
三、调用示例:
//我的应用示例 : D:\DelphiProjs\PulledupUI\CarveoutManage\mySource\uZhuce.pas 下:
//: procedure TFormZhuce.Rect_VerificcodeClick(Sender: TObject); :
//引用上述 : uses uMyGY_RestClient.pas :
var LUnix13BitInt64,LUnix10BitInt64:string ;
begin
//{//在Unix下时间截整数(IOS及Android下):uses System.DateUtils;
LUnix10BitInt64
:=UIntToStr(
DateTimeToUnix(now,false) );
Memo1.Lines.Add(LUnix10BitInt64 );
Memo1.Lines.Add(
FormatDateTime('yyyy-mm-dd hh:nn:ss',
UnixToDateTime(LUnix10BitInt64.ToInt64,false)
)
);
//:返回10位整数的时间截:常用的
LUnix13BitInt64
:=UIntToStr(
DateTimeToUnix13Bit(now,false) );
Memo1.Lines.Add(LUnix13BitInt64);
//:返回13位Unix整数:代表存入Unix时用的带毫秒的日期时间格式
Memo1.Lines.Add(Unix13BitToDateTimeStr(LUnix13BitInt64.ToInt64,false));
//:写入13位整数的时间截:往Unix下写入或从手机媒体文件名等读取结果的时候用
end;
本博客相关文章:
https://blog.csdn.net/pulledup/article/details/108113159
喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享: