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 ...
随机推荐
- DM816X 实现 USB HID Gadget 鼠标键盘功能【转】
转自:https://my.oschina.net/renyongke/blog/410695 开发环境: 平台: DM8168 内核 :linux 2.6.32 RDK:DVRRDK_04.00.0 ...
- android休眠唤醒驱动流程分析【转】
转自:http://blog.csdn.net/hanmengaidudu/article/details/11777501 标准linux休眠过程: l power managemen ...
- MAC系统下Sublime Text3 配置Python3详细教程
MAC系统下Sublime Text3 配置Python3详细教程(亲测有效) https://blog.csdn.net/weixin_41768008/article/details/798590 ...
- 常用SQL函数(字符串分隔转表、自增长转编号)
字符串分隔转表 -- ============================================= -- Author: -- Create date: -- Description: ...
- SendMessage原理初探
今天跟踪一下SendMessage的实现. 用向导先创建一个Windows application. 向导生成了一个简单的窗口,如下. 在File菜单添加SendMessage,顺便添加一个PostM ...
- hdu 1272 判断所给的图是不是生成树 (并查集)
判断所给的图是不是生成树,如果有环就不是,如果没环但连通分量大于1也不是 find函数 用递归写的话 会无限栈溢出 Orz要加上那一串 手动扩栈 Sample Input6 8 5 3 5 2 6 4 ...
- svn导入项目和部署方面的相关问题
前一阵子忙于部署项目的事情,在这个过程之中遇到了一些问题,查阅了相关资料解决了问题于是就决定分享给大家,可能会对大家有一定的帮助.我在下面中可能会提到dubbo的一些问题,dubbo是用于分布式的系统 ...
- 入门NodeJS
入门NodeJS https://www.cnblogs.com/dotnetcrazy/p/10118756.html NodeJS 1.环境配置 之前讲ES6的时候有提过一部分Node的知识,简单 ...
- Webpack按需加载一切皆模块
前言 在学习 Webpack 之前,我们需要了解一个概念:模块. 何为模块? 如果你曾学过 Java , C# 之类的语言,一定会知道 Java 中的 import 或 C# 中的 using 吧? ...
- 【AtCoder】ARC092
C - 2D Plane 2N Points 把能连边的点找到然后跑二分图匹配即可 #include <bits/stdc++.h> #define fi first #define se ...