linux timestamp

驱动或应用中获取时间戳的接口。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#include <sys/time.h>

#if 0

#include <linux/ktime.h>

/* 内核驱动中获取时间戳 */

static ktime_t get_kernel_timestamp(void)

{

ktime_t ktime = ktime_get(); // 获取当前时间

u64 timestamp = ktime_to_ns(ktime); // 将ktime转换为纳秒时间戳

printk(KERN_INFO "Current timestamp: %llu nanoseconds\n", timestamp);

return ktime;

}

#endif

int convert_utc_time( unsigned long long utc, char * ptime)

{

struct tm *tm;

time_t t = 0;

if (utc == 0) {

strncpy(ptime, "no time", 8);

return -1;

}

t = utc / 1000 + 8 * 3600;

tm = gmtime(&t);

sprintf(ptime, "%04d:%02d:%02d %02d:%02d:%02d.%03d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,

tm->tm_hour, tm->tm_min, tm->tm_sec, (int)utc%1000);

printf("utc time for ac debug : [%s]\n", ptime);

return 0;

}

static unsigned long long GetSystemClock_ns()

{

struct timespec time_start, time_end;

clock_gettime(CLOCK_REALTIME, &time_start);

return (long long unsigned)time_start.tv_sec * 1000000000 + (long long unsigned)time_start.tv_nsec;

}

static inline struct timeval AC_TIME_COMMON_GetMonotonic()

{

struct timespec stTime;

struct timeval stRet;

clock_gettime(CLOCK_REALTIME, &stTime);

stRet.tv_sec = stTime.tv_sec - timezone;

stRet.tv_usec = stTime.tv_nsec/1000;

return stRet;

}

static unsigned long long GetSystemClock()

{

struct timeval tv;

unsigned long long tv_sen;

unsigned long long gltime;

gettimeofday(&tv, NULL);

tv_sen = tv.tv_sec;

gltime = tv_sen * 1000 + tv.tv_usec / 1000;

return gltime;

}

int main()

{

char u_time[32] = {0};

unsigned long long system_time = 0;

struct timeval tmData = AC_TIME_COMMON_GetMonotonic();

system_time = tmData.tv_sec*1000 + tmData.tv_usec / 1000;

printf("get time %lld\n", system_time); // 1655445920614 : 2022:06:17 14:05:20.654

system_time = 0;

system_time = GetSystemClock();

printf("get time %lld\n", system_time); // 1655446828225 : 2022:06:17 14:20:28.265

system_time = 0;

system_time = GetSystemClock_ns(); // 1721875113493839086

printf("get time %lld\n", system_time);

convert_utc_time(1655356228, u_time);

convert_utc_time(1823430235, u_time);

convert_utc_time(1918628771, u_time);

convert_utc_time(1655445920614, u_time); // 2022:06:17 14:05:20.654

convert_utc_time(1655446828225, u_time); // 2022:06:17 14:20:28.265

}

相关推荐
璞致电子16 小时前
fpga开发板ZYNQ 璞致 PZ7010/7020 邮票孔核心板简介-ZYNQ7000系列小系统学习板
linux·嵌入式硬件·学习·fpga开发·fpga·fpga开发板·xilinx开发板
第四维度416 小时前
【全志V821_FoxPi】9-2 Linux IIC驱动MPU6050
linux·传感器·tina·mpu6050·v821
mjx653017 小时前
PyTorch快速入门
linux
DeeplyMind18 小时前
AMD KFD的BO设计分析系列6-1: VRAM BO的显存分配分析
linux·驱动开发·amdgpu·rocm·kfd
我言秋日胜春朝★18 小时前
【Linux网络编程】多路转接reactor——ET模式的epoll
linux·服务器·网络
Hell_Yrz19 小时前
Golang信号处理实战
linux
四桑.19 小时前
uniapp运行微信小程序uni为什么是undefined
linux
用户311879455921820 小时前
perl-Test-Simple-1.302195-5.fc39.noarch.rpm 怎么安装?Fedora 39 安装步骤讲解
linux
焦思懿--19期--工职大20 小时前
VMWare和物理机之间文件传输
linux·服务器·网络·电脑
四桑.20 小时前
echarts6.0.0版本,平行坐标图形,series为多组时,横线溢出绘图区域,如何解决
linux