I have to store date values (TDateTime) in a string format. What is the best way to do this? I considered the following approaches:
FloatToStr : looses precision, depends on locale settings
`FloatToStr’ with format settings : looses precision
DateTimeToStr : depends on locale settings
DateTimeToStr with format settings : ?
Are there any other alternatives? How do they compare in terms of
- Size in memory
- Independence of locale settings
- Precision
Use ISO-8601 format, as detailed in http://en.wikipedia.org/wiki/ISO_8601
If you need to save storage space, you can use the “compact” layout, e.g. ‘20090621T054523’.
You can use e.g.
FormatDateTime('yyyymmddThhnnss',aDateTime)to produce it.About time zone and localisation (from wikipedia):
So you should better convert the time into UTC, then append ‘Z’ at the end of the timestamp. Or use +hh/-hh according to your local time zone. The following times all refer to the same moment: “18:30Z”, “22:30+04”, “1130-0700”, and “15:00-03:30”.
For a better resolution, you can add sub-second timing by adding a fraction after either a comma or a dot character: e.g. to denote “14 hours, 30 minutes, 10 seconds and 500 ms”, represent it as “14:30:10,5”, “143010,5”, “14:30:10.5”, or “143010.5”. You can add several decimals to increase resolution.
If you need fast Iso8601 conversion routines (working with UTF-8 content), take a look at the corresponding part in SynCommons.pas. It’s much faster than the default
SysUtilsfunctions.PS:
If your purpose is just to store TDateTime as text in a pure Delphi application, you can use a not standard but fast:
Using the
Int64binary layout ofTDateTime/doublememory structure will be faster than any other floating-point related conversion.