本文介绍了在Delphi XE2中如何将本地时间转换为UTC时间?以及如何将其从UTC转换为当地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用的是Delphi xe2,我正在使用UTC datetime在我的数据库中存储记录,然后在客户端读取本地日期时间后将其还原回来任何想法如何做这个回转换?

解决方案

这是我用来从UTC转换为本地的功能。 / p>

  function LocalDateTimeFromUTCDateTime(const UTCDateTime:TDateTime):TDateTime; 
var
LocalSystemTime:TSystemTime;
UTCSystemTime:TSystemTime;
LocalFileTime:TFileTime;
UTCFileTime:TFileTime;
begin
DateTimeToSystemTime(UTCDateTime,UTCSystemTime);
SystemTimeToFileTime(UTCSystemTime,UTCFileTime);
如果FileTimeToLocalFileTime(UTCFileTime,LocalFileTime)
和FileTimeToSystemTime(LocalFileTime,LocalSystemTime)然后开始
结果:= SystemTimeToDateTime(LocalSystemTime);
end else begin
结果:= UTCDateTime; //如果任何转换函数失败,则默认为UTC。
结束
结束

正如你可以看到函数转换UTC日期时间如下:




  • 日期时间 - >系统时间

  • 系统时间 - >文件时间

  • 文件时间 - >本地文件时间(这是从UTC到本地的转换)

  • 本地文件时间 - >系统时间

  • 系统时间 - >日期时间



应该明白如何扭转这一点。






请注意,此转换会将夏令时视为现在,而不是像转换时那样。 类型,在XE中引入,试图做到这一点。代码变成:

  LocalDateTime:= TTimeZone.Local.ToLocalTime(UniversalDateTime); 

另一方面使用 ToUniversalTime 。 / p>

这个类似乎是(松散地)在.net上建模的课程。



尝试将当时的夏令时计算为100%准确。这是不可能实现的。至少没有时间机器。这只是考虑未来的时代。过去的偶像很复杂。 Raymond Chen在这里讨论了这个问题:。


I'm using Delphi xe2 and I'm trying to store records using UTC datetime in my database and then restore it back when a client reads it in his local datetime ? any idea how to do this forth back conversion ?

解决方案

This is the function that I use to convert from UTC to local.

function LocalDateTimeFromUTCDateTime(const UTCDateTime: TDateTime): TDateTime;
var
  LocalSystemTime: TSystemTime;
  UTCSystemTime: TSystemTime;
  LocalFileTime: TFileTime;
  UTCFileTime: TFileTime;
begin
  DateTimeToSystemTime(UTCDateTime, UTCSystemTime);
  SystemTimeToFileTime(UTCSystemTime, UTCFileTime);
  if FileTimeToLocalFileTime(UTCFileTime, LocalFileTime)
  and FileTimeToSystemTime(LocalFileTime, LocalSystemTime) then begin
    Result := SystemTimeToDateTime(LocalSystemTime);
  end else begin
    Result := UTCDateTime;  // Default to UTC if any conversion function fails.
  end;
end;

As you can see the function transforms the UTC date time as follows:

  • Date time -> system time
  • System time -> file time
  • File time -> local file time (this is the conversion from UTC to local)
  • Local file time -> system time
  • System time -> date time

It should be obvious how to reverse this.


Note that this conversion treats daylight saving as it is now rather than as it is/was at the time being converted. The DateUtils.TTimeZone type, introduced in XE, attempts to do just that. The code becomes:

LocalDateTime := TTimeZone.Local.ToLocalTime(UniversalDateTime);

In the other direction use ToUniversalTime.

This class appears to be (loosely) modelled on the .net TimeZone class.

A word of warning. Do not expect the attempt to account for daylight savings at the time being converted to be 100% accurate. It is simply impossible to achieve that. At least without a time machine. And that's just considering times in the future. Even times in the past are complex. Raymond Chen discusses the issue here: Why Daylight Savings Time is nonintuitive.

这篇关于在Delphi XE2中如何将本地时间转换为UTC时间?以及如何将其从UTC转换为当地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:33