相关疑难解决方法(0)

将纪元时间转换为"实际"日期/时间

我想要做的是将纪元时间(自1970年1月1日午夜以来的秒数)转换为"实际"时间(m/d/yh:m:s)

到目前为止,我有以下算法,对我来说感觉有点难看:

void DateTime::splitTicks(time_t time) {
    seconds = time % 60;
    time /= 60;
    minutes = time % 60;
    time /= 60;
    hours = time % 24;
    time /= 24;

    year = DateTime::reduceDaysToYear(time);
    month = DateTime::reduceDaysToMonths(time,year);
    day = int(time);
}

int DateTime::reduceDaysToYear(time_t &days) {
    int year;
    for (year=1970;days>daysInYear(year);year++) {
        days -= daysInYear(year);
    }
    return year;
}

int DateTime::reduceDaysToMonths(time_t &days,int year) {
    int month;
    for (month=0;days>daysInMonth(month,year);month++)
        days -= daysInMonth(month,year);
    return month;
}
Run Code Online (Sandbox Code Playgroud)

你可以假设成员seconds,minutes,hours,month, …

c++ algorithm datetime epoch

23
推荐指数
3
解决办法
6万
查看次数

标签 统计

algorithm ×1

c++ ×1

datetime ×1

epoch ×1