您如何用Java 8 Instant表示MS-DTYP`DATETIME`? [英] How do you represent MS-DTYP `DATETIME` in Java 8 Instant?

查看:99
本文介绍了您如何用Java 8 Instant表示MS-DTYP`DATETIME`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Microsoft规范中, DATETIME 表示为2个32位整数: low

In the Microsoft Spec, DATETIME is represented as 2 32-bit integers: low and high

参考: https://docs.microsoft.com/zh-CN/openspecs/windows_protocols/ms-dtyp/cca27429-5689-4a16-b2b4 -9325d93e4ba2


FILETIME结构是一个64位值,表示
的数量(100纳秒间隔)自1601年1月1日以来的时间,即
世界协调时间(UTC)。 typedef struct _FILETIME {DWORD
dwLowDateTime; DWORD dwHighDateTime; } FILETIME,
* PFILETIME,
* LPFILETIME; dwLowDateTime:32位无符号整数,包含文件时间的低位。 dwHighDateTime:32位无符号
整数,包含文件时间的高位。

The FILETIME structure is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since January 1, 1601, Coordinated Universal Time (UTC). typedef struct _FILETIME { DWORD dwLowDateTime; DWORD dwHighDateTime; } FILETIME, *PFILETIME, *LPFILETIME; dwLowDateTime: A 32-bit unsigned integer that contains the low-order bits of the file time. dwHighDateTime: A 32-bit unsigned integer that contains the high-order bits of the file time.

这是长的 130280867040000000

因此,高点和低点都是用

So the the high and low computed with

int high = (int)(fullval >> 32);
int low = (int)fullval;

如此之高= 30333378
和低= 552794112

如何将这些计算为Java 8 Instant?

How do I compute these to a Java 8 Instant?

推荐答案

对于1秒精度的转换,您自己的答案就可以了。万一您还需要转换秒数,这是一种方法。

For converting with 1 second precision your own answer is just fine. In case you also need to convert the fraction of second, here’s one way to do that.

    Instant msFiletimeEpoch = Instant.parse("1601-01-01T00:00:00Z");
    // a tick is 100 nanoseconds
    int nanosPerTick = 100;
    long ticksPerSecond = TimeUnit.SECONDS.toNanos(1) / nanosPerTick;

    long fullval = 130_280_867_040_000_000L;

    long seconds = fullval / ticksPerSecond;
    long nanos = fullval % ticksPerSecond * nanosPerTick;

    Instant answer = msFiletimeEpoch.plusSeconds(seconds).plusNanos(nanos);

    System.out.println(answer);

输出为:


2013-11-05T00:58:24Z

2013-11-05T00:58:24Z

让我们尝试在原始价值上再加上1个勾号;

Let’s try to put 1 more tick on your oroginal value; it should add 100 nanoseconds.

    long fullval = 130_280_867_040_000_001L;




2013-11-05T00:58:24.000000100Z

2013-11-05T00:58:24.000000100Z

是这样的。

未来很远的时间要注意:根据您的报价Microsoft整数都是无符号的。 Java 已签名。因此,在30828年的某个时候,我们将开始获得非常错误的结果。万一 long 的值为负,以防万一我们应该抛出异常。

Caveat for very far future dates: According to your quote the Microsoft integers are both unsigned. A Java long is signed. So some time in year 30828 we will start getting results that are very wrong. Just in case we ought to throw an exception if the long value is negative.

这篇关于您如何用Java 8 Instant表示MS-DTYP`DATETIME`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆