当前位置: 首页 > news >正文

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
}

http://www.lryc.cn/news/409359.html

相关文章:

  • Vue.js 搭建大屏可视化项目
  • Linux:进程信号(二.信号的保存与处理、递达、volatile关键字、SIGCHLD信号)
  • 最值得推荐的5个AI大模型API
  • PyTest+Allure生成测试报告
  • ROS2教程(10) - 编写接收程序、添加frame - Linux
  • Arraylist与LinkedList的区别
  • Nestjs使用Redis的最佳实践
  • Cadence23学习笔记(十四)
  • socket 编程
  • 如何使用 HTTPie 进行高效的 HTTP 请求
  • Lingo求解器百度云下载 ling 8.0/lingo 18安装包资源分享
  • 文献综述如何为研究的理论框架做出贡献
  • FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
  • 【赛事推荐】2024中国高校计算机大赛人工智能创意赛
  • C++沉思:预处理和编译
  • 交通数据处理-计算途径某些路段的车辆数
  • 从0到1入门系列 | 崖山公开课再加码,三小时带你入门崖山数据库!
  • Powershell自定义带参数的别名
  • 文件操作相关的精讲
  • 05 循环神经网络
  • C#初级——条件判断语句、循环语句和运算符
  • Laravel路由模型绑定:简化依赖注入的艺术
  • 【vue前端项目实战案例】之Vue仿饿了么App
  • 冷热分离——Java全栈知识(36)
  • 了解Selenium中的WebElement
  • OpenCV facedetect 人脸检测官方示例项目配置
  • 自定义Laravel Artisan风格:打造个性化命令行体验
  • CTF之网站被黑
  • Electron学习笔记(一)基础环境
  • 【C语言】栈的实现(数据结构)