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

Aus*_*yde 23 c++ algorithm datetime epoch

我想要做的是将纪元时间(自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,day,和year所有的存在.

使用for循环修改原始时间感觉有点偏,我想知道是否有一个"更好"的解决方案.

rxi*_*xin 18

在daysInMonth函数中注意闰年.

如果您想要非常高的性能,您可以预先计算该对以一步到达月份+年,然后计算日/小时/分钟/秒.

一个很好的解决方案是gmtime源代码中的一个:

/*
 * gmtime - convert the calendar time into broken down time
 */
/* $Header: gmtime.c,v 1.4 91/04/22 13:20:27 ceriel Exp $ */

#include        <time.h>
#include        <limits.h>
#include        "loc_time.h"

struct tm *
gmtime(register const time_t *timer)
{
        static struct tm br_time;
        register struct tm *timep = &br_time;
        time_t time = *timer;
        register unsigned long dayclock, dayno;
        int year = EPOCH_YR;

        dayclock = (unsigned long)time % SECS_DAY;
        dayno = (unsigned long)time / SECS_DAY;

        timep->tm_sec = dayclock % 60;
        timep->tm_min = (dayclock % 3600) / 60;
        timep->tm_hour = dayclock / 3600;
        timep->tm_wday = (dayno + 4) % 7;       /* day 0 was a thursday */
        while (dayno >= YEARSIZE(year)) {
                dayno -= YEARSIZE(year);
                year++;
        }
        timep->tm_year = year - YEAR0;
        timep->tm_yday = dayno;
        timep->tm_mon = 0;
        while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
                dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
                timep->tm_mon++;
        }
        timep->tm_mday = dayno + 1;
        timep->tm_isdst = 0;

        return timep;
}
Run Code Online (Sandbox Code Playgroud)

  • gmtime 源代码的链接已损坏。 (3认同)

sdt*_*tom 14

标准库提供了执行此操作的功能.gmtime()或者localtime()将a time_t(自纪元以来的秒数,即1970年1月1日00:00:00)转换成a struct tm.strftime()然后可以根据您指定的格式将a struct tm转换为string(char*).

请参阅:http://www.cplusplus.com/reference/clibrary/ctime/

日期/时间计算可能会变得棘手.除非你有充分的理由,否则你最好不要使用现有的解决方案而不是尝试自己的解决方案.

  • 这开始是一段时间的作业,但现在我有兴趣让它变得更好.通常情况下,我会选择库解决方案,但1)我不知道函数的内置时间和2)我想完善我的实现. (3认同)

brk*_*yal 5

一个简单的方法(尽管与您想要的格式不同):

std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result));
Run Code Online (Sandbox Code Playgroud)

输出:2011 年 9 月 21 日星期三 10:27:52

请注意,返回的结果将自动与“\n”连接。您可以使用以下方法将其删除:

std::string::size_type i = res.find("\n");
if (i != std::string::npos)
    res.erase(i, res.length());
Run Code Online (Sandbox Code Playgroud)

摘自: http: //en.cppreference.com/w/cpp/chrono/c/time