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 ?
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
- How to account for clock offsets in a distributed
相关文章
- How to truncate seconds in TSQL?
- How to remove seconds from datetime?
- OLS with pandas: datetime index as predictor
- Calculate number of working days in a month [dupli
- How can I convert a OLE Automation Date value to a
- Best way to implement MVVM bindings (View <-> V
- Windows EventLog: How fast are operations with it?
- Out of bounds nanosecond timestamp
If your client uses a local Delphi application, this can be done with the System date functions.
However if you are in a client/server environment (for example the Delphi app is a web server, and the client only receives the HTML pages), you need to convert to the user's local time differently. The server needs to know the user's time zone, and convert appropriately.
Also daylight saving time can cause headache if the application needs to convert historical data - you need to know if there was DST in effect for the user's region.
In these use cases, the Time Zone Database for Delphi can be helpful.
I did not know that there is SystemTimeToTzSpecificLocalTime but just read that Jon Skeet prefers TZDB for time zone handling.
As you are using XE2, you can use the
System.DateUtils.TTimeZone
.You can check this good post explaining the methods and how it works and examples : http://alex.ciobanu.org/?p=373
This is the function that I use to convert from UTC to local.
As you can see the function transforms the UTC date time as follows:
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: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.
you can use TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime from kernel32.