6字节时间戳记到DateTime [英] 6 bytes timestamp to DateTime

查看:124
本文介绍了6字节时间戳记到DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用第三方API。根据其规范,以下

I use 3rd party API. According to its specification the following

  byte[] timestamp = new byte[] {185, 253, 177, 161, 51, 1}

表示自1970年1月1日消息
以来的毫秒数是生成用于传输的

问题是我不知道如何将其转换为DateTime。

The issue is that I don't know how it could be translated into DateTime.

我已经尝试过

DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long milliseconds = BitConverter.ToUInt32(timestamp, 0);
var result =  Epoch + TimeSpan.FromMilliseconds(milliseconds);

结果为{2/1/1970 12:00:00 AM},但2012年为

The result is {2/1/1970 12:00:00 AM}, but year 2012 is expected.

推荐答案

我假设 timestamp 使用小端格式。我还省略了参数验证。

I assume timestamp uses little endian format. I also left out parameter validation.

long GetLongLE(byte[] buffer,int startIndex,int count)
{
  long result=0;
  long multiplier=1;
  for(int i=0;i<count;i++)
  {
    result += buffer[startIndex+i]*multiplier;
    multiplier *= 256;
  }
  return result;
}

long milliseconds = GetLongLE(timestamp, 0, 6);

这篇关于6字节时间戳记到DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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