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
}

相关内容

热门资讯

wepower有外挂!hhpo... wepower有外挂!hhpoker德州有挂(透视)存在挂教程(有挂猫腻)-哔哩哔哩;1、超多福利:...
wepokeai机器人!wej... wepokeai机器人!wejoker透视方法(透视)细节揭秘(确实有挂)-哔哩哔哩;亲真的是有正版...
wepokeai代打的胜率!p... wepokeai代打的胜率!pokermaster脚本(透视)黑科技教程(有挂秘笈)-哔哩哔哩是一款...
wepoke黑科技!德普之星透... wepoke黑科技!德普之星透视辅助软件是真的(透视)教你攻略(有挂方略)-哔哩哔哩是一款可以让一直...
wepoke是真的有挂!hh ... wepoke是真的有挂!hh poker辅助有用(透视)细节方法(有挂功能)-哔哩哔哩;最新版202...
wepokeai代打逻辑!po... wepokeai代打逻辑!pokemmo手机版脚本免费(透视)透明教程(真的有挂)-哔哩哔哩;是一款...
wepoke有app软件!we... wepoke有app软件!wepoker辅助工具(透视)透牌教程(有挂分析)-哔哩哔哩;wepoke...
wepok软件透明挂!哈糖大菠... 【福星临门,好运相随】;wepok软件透明挂!哈糖大菠萝软件下载(透视)2025新版教程(揭秘有挂)...
七分钟了解!新畅游互娱科技(辅... 七分钟了解!新畅游互娱科技(辅助挂)详细透视开挂辅助新2025版(切实真的有挂)-哔哩哔哩;1、首先...
wepokeai代打逻辑!德州... wepokeai代打逻辑!德州hhpoker脚本(透视)辅助教程(有挂秘笈)-哔哩哔哩;是一款可以让...