【问题标题】:How to convert ticks in unsigned long long type to readable time format?如何将 unsigned long long 类型的刻度转换为可读的时间格式?
【发布时间】:2016-02-19 08:13:20
【问题描述】:

我必须为 Linux 编写一个内核模块,它会写入所有进程的开始时间。 我使用struct task_struct 来获取有关进程的信息。

struct task_struct *task = get_current();
struct task_struct *head;
struct list_head *current_list;


struct rtc_time time;
list_for_each(current_list, &task->tasks)
{
    head = list_entry(current_list, struct task_struct, tasks);

    rtc_time64_to_tm(head->se.exec_start, &time);
    printk(KERN_INFO "%d:%d:%d %d/%d/%d", time.tm_hour, time.tm_min, time.tm_sec, 
        time.tm_year, time.tm_mon, time.tm_yday);
}

rtc_time64_to_tm() 接受参数long long,但head->se.exec_start 的类型为unsigned long long。这就是为什么我无法将其转换为可读的时间格式。

// Convert seconds since 01-01-1970 00:00:00 to Gregorian date.

void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)

【问题讨论】:

  • Hmmmm "rtc_time64_to_tm() 接受参数unsigned long" 你确定这是正确的吗?我认为需要time64_t
  • typedef 签名 long long __s64; typedef __s64 time64_t;所以 time64_t 很长,但是有符号。

标签: c linux time linux-kernel long-integer


【解决方案1】:

如果代码确实需要显示未来到目前为止的日期,请利用 YMD HMS 模式每 400 年重复一次的优势。

#define SEC_PER400YEAR (60LL*60*24*(365L*400+97))

unsigned long long start = head->se.exec_start;
unsigned long long year = 0;
if (start > LLONG_MAX) {
  year = (start/SEC_PER400YEAR)*400;
  start %= SEC_PER400YEAR;
}
rtc_time64_to_tm((long long) start, &time);

printk(KERN_INFO "%lld/%d/%d", time.tm_year +year, time.tm_mon, time.tm_yday);

IMO,超过 LLONG_MAX 的值 start 是可疑的。

注意:if (start > LLONG_MAX) 并不是真正需要的。

如果关注 TZ、DST,则该方法需要稍微修改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2017-01-04
    相关资源
    最近更新 更多