linux timestamp
创始人
2024-11-17 18:06:38
0次

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

#include
#include
#include
#include
#include

#if 0
#include
/* 内核驱动中获取时间戳 */
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
}

相关内容

热门资讯

微扑克辅助器!德州平台有没有挂... 您好:德州平台有没有挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
AApoker透视挂!wepo... 很多人使用云端 AI 时,都担心wepoker透视挂是真的吗、内部资料上传带来隐私风险。这款本地运行...
WePoKer透视!wepok... WePoKer透视!wepoker如何拿到好牌,分享透视多功能软件APP模板;近期一款 AI 黑科技...
红龙扑克透视挂!德扑之星规律,... 红龙扑克透视挂!德扑之星规律,解迷透视挂多功能插件APP法门是一款可以让一直输的玩家,快速成为一个“...
aapoker透视!WePoK... 您好,WePoKer有没有透视软件挂这款游戏可以开挂的,确实是有挂的,需要了解加微【48527505...
红龙扑克开挂!微扑克的辅助工具... 很多人使用云端 AI 时,都担心微扑克的辅助工具有哪些、内部资料上传带来隐私风险。这款本地运行的 A...
aapoker辅助!wepok... 您好:wepoker私人局怎么设置抽水这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会...
aapoker辅助挂!wepo... aapoker辅助挂!wepoker透视挂安全吗,普及辅助挂多功能方法APP策略AI 智能体黑科技再...
红龙PoKer辅助挂!wepo... 您好:wepokerplus透视挂真的假的这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌...
德扑之星辅助挂!德州ai辅助有... 德扑之星辅助挂!德州ai辅助有用吗,详细辅助挂多功能下载APP指南书是一款可以让一直输的玩家,快速成...