ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)
Recently I needed a way of concerting back and forth ISO 8601 DateTime values used in XML from Delphi.
Thoug the Delphi DateUtils unit has some ISO 8601 features for calculating week numbers, you actually need to the XSBuiltIns unit for converting back and forth to ISO 8601 text representation of a DateTime.
I remembered answering the related In Delphi is there a function to convert XML date and time to TDateTime question on StackOverflow a while ago (and forgot that this was back in 2009 <g>).
ISO 8601 dates can either include a time-zone, or be in UTC, which is not part of that answer. So lets elaborate on that answer a bit now:
UTC times in ISO 8601 format end in a Z time zone designator like this:
<Created>2011-06-29T17:01:45.505Z</Created>
The Z UTC indicator is basically a shortcut for a timezone offset of +00:00 or -00:00, effectively being a zero (or zulu) timezone.
Nonzero timezones start with an optional + or -, followed by the hours and minutes offset from UTC, for instance +01:00 for the Central European Time zone.
<Created>2011-06-29T17:01:45.505+01:00</Created>
When you want historical time zones, then you need the Delphi TZDB interface to the historical TZ database.
To do the timezone calculations, I used the TimeZoneBias function from Indy, which is either in the IdGlobal unit (Indy <= version 9) or the IdGlobalProtocols unit (Indy 10 and up).
Conversion is done by using the TXSDateTime (that like all the XS conversion classes descends from TRemotableXS in the InvokeRegistry unit).
Most of the classes descending from TRemotableXS contain two methods: NativeToXS and XSToNative doing the underlying conversions.
Since I didn’t need the historical reference in the TZDB, this is the code that I came up with:
unit Iso8601Unit; interface type
TIso8601 = class(TObject)
public
class function DateTimeFromIso8601(const Value: string): TDateTime; static;
class function UtcDateTimeToIso8601(const Value: TDateTime): string; static;
class function DateTimeToIso8601(const Value: TDateTime): string; static;
class function UtcNow: TDateTime; static;
class function ToUtc(const Value: TDateTime): TDateTime; static;
class function FromUtc(const Value: TDateTime): TDateTime; static;
end; implementation uses
IdGlobalProtocols, {IdGlobal for Index SysUtils,
XSBuiltIns; class function TIso8601.DateTimeFromIso8601(const Value: string): TDateTime;
begin
with TXSDateTime.Create() do
try
XSToNative(value); // convert from WideString
Result := AsDateTime; // convert to TDateTime finally
finally
Free();
end;
end; class function TIso8601.UtcDateTimeToIso8601(const Value: TDateTime): string;
begin
with TXSDateTime.Create() do
try
AsUTCDateTime := Value;
Result := NativeToXS; // convert to WideString
finally
Free();
end;
end; class function TIso8601.DateTimeToIso8601(const Value: TDateTime): string;
begin
with TXSDateTime.Create() do
try
AsDateTime := Value; // convert from TDateTime
Result := NativeToXS; // convert to WideString
finally
Free();
end;
end; class function TIso8601.UtcNow: TDateTime;
begin
Result := ToUtc(Now);
end; class function TIso8601.ToUtc(const Value: TDateTime): TDateTime;
var
Bias: TDateTime;
begin
Bias := TimeZoneBias;
Result := Value + TimeZoneBias;
end; class function TIso8601.FromUtc(const Value: TDateTime): TDateTime;
var
Bias: TDateTime;
begin
Bias := TimeZoneBias;
Result := Value - TimeZoneBias;
end; end.
–jeroen
via In Delphi is there a function to convert XML date and time to TDateTime – Stack Overflow.
ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)的更多相关文章
- ionic3报Please provide a valid ISO 8601 datetime format的错误
对于ionic的ion-datetime控件,初始化值的时候,如果指定为new Date()的话,会提示Please provide a valid ISO 8601 datetime format ...
- 一起Polyfill系列:让Date识别ISO 8601日期时间格式
一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...
- atitit.日期,星期,时候的显示方法ISO 8601标准
atitit.日期,星期,时候的显示方法ISO 8601标准 1. ISO 86011 2. DAte日期的显示1 2.1. Normal1 2.2. 顺序日期表示法(可以将一年内的天数直接表示)1 ...
- Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示
Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示 在 开发中遇到应用c#及asp.net的在的webservice 保存图片并以xml文件形式现实出来 ...
- 日期和时间格式(ISO 8601)
参考 ISO 8601 - Wikipedia ISO 8601 Date and time format
- Unable to convert MySQL date/time value to System.DateTime 错误
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- ISO 8601
ISO 8601 國際標準化組織的国际标准ISO 8601是日期和时间的表示方法,全称为<数据存储和交换形式·信息交换·日期和时间的表示方法>.目前是第三版ISO8601:2004以替代第 ...
- How to Convert a Date Time to “X minutes ago” in C# z
http://www.codeproject.com/Articles/770323/How-to-Convert-a-Date-Time-to-X-minutes-ago-in-Csh In one ...
- Delphi使用NativeXml访问XML文件
Delphi使用NativeXml访问XML文件 1.创建XML文件var Doc: TNativeXml;//声明上下文对象var filepath:string;//文件路径DOC:=TNativ ...
随机推荐
- C++获取文件夹下所有文件名
查找文件需要一个结构体和几个函数.结构体为struct _finddata_t,函数为_findfirst.findnext和_findclose. struct _finddata_t 这个结构体是 ...
- javaweb笔记五
JSP:java server page服务器脚本语言.(脚本===插件),是一种在html代码中,嵌入java代码的方式.解决servlet产生动态页面缺陷而产生的一门技术.js:客户端脚本语言js ...
- Python学习笔记:算法的重要性
今日看了一个基础的教程<8分钟学会一个算法>,偶然间看到一个很简单的例子,仅当记录一下. 题目:已知a+b+c=1000,且a^2+b^2=c^2,求a,b,c的所有自然数解? #### ...
- 【BZOJ2059】Buying Feed 购买饲料
题面 约翰开车来到镇上,他要带V吨饲料回家.如果他的车上有X吨饲料,每公里就要花费X^2元,开车D公里就需要D* X^2元.约翰可以从N家商店购买饲料,所有商店都在一个坐标轴上,第i家店的位置是Xi, ...
- ElastAlert告警
ElastAlert告警 https://blog.csdn.net/qq_38369069/article/details/80842432
- 解决 Delphi XE5 写Android程序的No resource identifier found for attribute... 错误【转】
原文:http://www.hxhlb.cn/article/32142aaeb67bbc05379369c3.html 那一天,我装上了RAD Studio XE5. 当天晚上,我就写了一个小小的A ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- 局域网内其他主机如何访问运行在宿主机的虚拟机中的Django项目(转)
局域网内其他主机如何访问运行在宿主机的虚拟机中的Django项目 1.在宿主机cmd中查看宿主机的ip(注意区分主机中虚拟机的ip) 我连的是无线,IP如下 2.在Django项目的mysit ...
- python常用库安装网址
python常用库安装网址如下: http://pypi.python.org/pypi
- java 线程池(ExecutorService与Spring配置threadPoolTaskExecutor)
一.java ExecutorService实现 创建ExecutorService变量private ExecutorService executor = null 2.执行对应任务时,首先生成线程 ...