- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要函数 DateTimeToUnix
和 UnixToDateTime
的 UTC 变体,以便中国客户能够与德国的服务器进行交互。双方应该能够交换 Unix 时间戳(UTC 格式,无 DST)并能够通过这种方式进行通信。
在bugreport of HeidiSQL中,用户讨论 DateTimeToUnix
和 UnixToDateTime
不关心时区,我发现了以下代码:
function DateTimeToUTC(dt: TDateTime): Int64;
var
tzi: TTimeZoneInformation;
begin
Result := DateTimeToUnix(dt);
GetTimeZoneInformation(tzi);
Result := Result + tzi.Bias * 60;
end;
MSDN explains twi.Bias
如下:
All translations between UTC time and local time are based on the following formula:
UTC = local time + bias
The bias is the difference, in minutes, between UTC time and local time.
这听起来合乎逻辑,但由于我不确定上面的代码是否正确,所以我编写了以下程序来检查它:
// A date in summer time (DST)
Memo1.Lines.add('1401494400'); // 31 May 2014 00:00:00 GMT according to http://www.epochconverter.com/
Memo1.Lines.add(inttostr(DateTimeToUnixUTC(StrToDate('31.05.2014'))));
// A date in winter time
Memo1.Lines.add('567302400'); // 24 Dec 1987 00:00:00 GMT according to http://www.epochconverter.com/
Memo1.Lines.add(inttostr(DateTimeToUnixUTC(StrToDate('24.12.1987'))));
德国当前的输出(GMT+1+DST)为:
1401494400
1401490800
567302400
567298800
我期望的输出是:
1401494400
1401494400
567302400
567302400
我做错了什么?
PS:对于这个项目,我绑定(bind)了 Delphi 6。
最佳答案
您已经找到了 DateTimeToUnix
和 UnixToDateTime
。因此,这部分转换已得到处理。
您现在需要做的就是在本地时间和 UTC 时间之间进行转换。您可以使用 DateUtils.TTimeZone
来做到这一点类(class)。特别是 DateUtils.TTimeZone.ToUniversalTime
和 DateUtils.TTimeZone.ToLocalTime
。
这四个功能可以满足您的所有需求。
关于delphi - UTC 格式的 DateTimeToUnix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23968919/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
当我使用 UTC() 函数获取纪元时间(1970 年 1 月 1 日)的总秒数时。但再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC d
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个关于 Swift 类型 TimeZone 的问题。我需要一个 UTC0 时区来格式化我的 Date 对象。我正在像这样创建 TimeZone 对象。 let utcTimeZone = Tim
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 5 年前。 Improve t
我遇到了以下代码: datetime.datetime.utcnow().replace(tzinfo=tzutc()) 我看不到 Replace() 调用正在做什么,从阅读文档来看,它似乎将其转换为
我在应用程序中将时区设置为 UTC,如下所示: // set default time zone to UTC TimeZone.setDefault(TimeZone.getTimeZone(Zon
我需要两件事: 将当前时间转换为 UTC(这样我就可以以 UTC 格式存储日期)--> result = java.util.Date. 将加载日期(UTC 格式)转换为任何时区 --> result
我想将日期从本地转换为 UTC,再将 UTC 转换为本地。 例如。 2015 年 4 月 1 日,12:00 PM 然后是 UTC 2015 年 4 月 1 日下午 5:30 对我来说是本地时间,但我
嗨,我有一个长度为几百万的字符向量( rr ),它以 %Y-%m-%d %H:%M:%S 格式表示时间和日期戳。记录在澳大利亚/悉尼。 如何获得代表这一点的 POSIXct 对象(快速)。 我找到了
static String createUTCTime() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")
我正在尝试将语言环境时间转换为 UTC,然后将 UTC 转换为语言环境时间。但我没有得到结果。 public class DateDemo { public static void main(Stri
我正在寻找用纯 Javascript 替换 moment js 功能 - 我需要重新工作的项目将来不会有 moment.js 可用。我已经有一段时间没有使用 javascript Date 了,所以需
我想获取与 UTC 相关的用户时区,然后将其显示为 UTC +/- 。例如,加利福尼亚用户应显示 UTC -8(或 -7,视情况而定),而巴林用户应显示 UTC +3,等等。 下面的代码并没有告诉我它
我正在尝试将毫秒转换为 UTC 日期对象,如下所示 - var tempDate = new Date(1465171200000); // --> tempDate = Mon Jun 06 201
我一直很困惑为什么下面的代码会导致我的日期从 25 日更改为 24 日 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy"); DateTi
我需要将字符串转换为 UTC 日期,然后将 UTC 日期转换为本地日期。 这是我的代码: var dateStr = "9/8/2015 12:44:00 PM"; console.log(strto
我正在用 PHP 编写一个 Twitter 网络服务。当用户登录时,我收到此节点: -18000 我必须更改脚本的时区,以便它适应用户的实际时区。我为此找到的唯一 php 函数是: date_defa
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
我是一名优秀的程序员,十分优秀!