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 ...
随机推荐
- find中的-print0和xargs中-0的奥妙【转】
find cygnus/firmware_cygnus/target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*. ...
- Linux 串口、usb转串口驱动分析(2-2) 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186852 Linux 串口.usb转 ...
- Apache+jboss群集优化
故障现象: 俩台服务器jboss做的Apache群集,之前优先访问A,造成大量session都在A上有报警. 调整 调整Apache 配置jboss集群参数,将Node2的worker.node2.l ...
- oracle数据库如何创建用户和角色,并给其赋权?
一.创建用户并赋予权限 1.创建用户 create user wangxiangyu identified by wangxiangyu; 2.赋权 grant dba to wangxiangyu; ...
- springcloud搭建服务注册中心与服务发现
1.创建服务注册中心 创建一个普通的Spring Boot工程 首先我们需要创建一个普通的Spring Boot工程,命名为eureka-server,普通到什么程度呢?就是一个starter都不需要 ...
- 微信小程序《沈航二手书》
微信小程序<沈航二手书> 0x01. 利益相关者 利益相关者:是指与客户有一定利益关系的个人或组织群体,可能是客户内部的(如雇员),也可能是客户外部的(如供应商或压力群体). 根据相关利 ...
- ddmlib问题总结——同步获取设备信息
通过IDevice.getProperty(String name)得到响应的设备属性.在实际的使用过程中发现,我的manufacturer总是获取不到,为null(获取代码如下),而剩下的属性都可以 ...
- 【算法】后缀自动机(SAM) 初探
[自动机] 有限状态自动机的功能是识别字符串,自动机A能识别字符串S,就记为$A(S)$=true,否则$A(S)$=false. 自动机由$alpha$(字符集),$state$(状态集合),$in ...
- Ansible专题整理
Ansible 专题文章总览 Ansible小手册,仅供参考 文章如未明确说明实验环境,默认如下: OS:Centos 6.7 x86_64 Ansible: 2.1.2.0 Python: 2.6. ...
- 搭建基于MyEclipse的Hadoop开发环境
不多说,直接上干货! 前面我们已经搭建了一个伪分布模式的Hadoop运行环境.请移步, hadoop-2.2.0.tar.gz的伪分布集群环境搭建(单节点) 我们绝大多数都习惯在Eclipse或MyE ...