【发布时间】:2020-05-16 19:00:53
【问题描述】:
我有这个 C 代码的 sn-p,我正试图将其转换为 Go:
#include <sys/time.h>
#include <sys/types.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
struct timeval started;
int64_t ms;
gettimeofday(&started, NULL);
ms = (int64_t) started.tv_sec * 1000 + started.tv_usec / 1000;
printf("Time: %011"PRIx64"\n", ms);
return 0;
}
我发现了如何使用下面的 sn-p 将 unix 时间转换为毫秒:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Printf("Unix Time in ms: %d \n", time.Now().UnixNano() / 1000000)
}
但是,我很难将该数字转换为十六进制(这有点类似于我拥有的 C 代码的输出。)我确实找到了如何使用此 questions / answer 解码十六进制时间戳。谁能指出我正确的方向?
【问题讨论】:
-
您阅读过fmt package 的文档吗?
-
长度为 11 和零填充的小写十六进制表示为:
%011x。