admin管理员组文章数量:1206262
I'm trying to convert the current date to an Uint8 array of bytes in little endian format.
What I tried is this :
const epochSeconds = new Date().valueOf();
This works and returns the current date in epoch timestamp format.
I then create an array buffer of 8 bytes and a dataview and try to add the bytes to it, but it seems incorrect
const byteArray = this.longToByteArray(epochSeconds);
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
for (let i = 0; i < 8; i++) {
dataView.setUint8(i, byteArray[i]);
}
I tried to verify that value with a python program
import datetime
uint8_array = [25, 15, 142, 103, 0, 0, 0, 0]
#uint8_array = [72, 224, 87, 131, 148, 1, 0, 0]
timestamp_little_endian = int.from_bytes(uint8_array, byteorder='little')
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
print(timestamp_little_endian, human_readable_date_little_endian)
With the first uint8_array i get 1737363225 2025-01-20 08:53:45
and with the second
File "a.py", line 8, in <module>
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
OSError: [Errno 22] Invalid argument
What am I doing wrong ?
I'm trying to convert the current date to an Uint8 array of bytes in little endian format.
What I tried is this :
const epochSeconds = new Date().valueOf();
This works and returns the current date in epoch timestamp format.
I then create an array buffer of 8 bytes and a dataview and try to add the bytes to it, but it seems incorrect
const byteArray = this.longToByteArray(epochSeconds);
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
for (let i = 0; i < 8; i++) {
dataView.setUint8(i, byteArray[i]);
}
I tried to verify that value with a python program
import datetime
uint8_array = [25, 15, 142, 103, 0, 0, 0, 0]
#uint8_array = [72, 224, 87, 131, 148, 1, 0, 0]
timestamp_little_endian = int.from_bytes(uint8_array, byteorder='little')
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
print(timestamp_little_endian, human_readable_date_little_endian)
With the first uint8_array i get 1737363225 2025-01-20 08:53:45
and with the second
File "a.py", line 8, in <module>
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
OSError: [Errno 22] Invalid argument
What am I doing wrong ?
Share Improve this question asked Jan 20 at 14:11 aleksshrvaleksshrv 11 silver badge 1- @robertklep has your answer. Just divide the ECMAScript time value by 1e3 (or just remove the last 3 digits). – RobG Commented Jan 23 at 3:41
1 Answer
Reset to default 1The first value is a timestamp in seconds, the second one in milliseconds, which is the resolution that Javascript uses for timestamps.
More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf
本文标签: javascriptConvert current date to an Uint8 array of bytes in little endian formatStack Overflow
版权声明:本文标题:javascript - Convert current date to an Uint8 array of bytes in little endian format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738691920a2107145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论