转换信号出现时间到"真正的"日期时间 [英] Converting epoch time to "real" date/time

查看:196
本文介绍了转换信号出现时间到"真正的"日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是转换一个划时代的时间(自午夜1/1/1970)为真正的时间(M / D / YH:M:S)

What I want to do is convert an epoch time (seconds since midnight 1/1/1970) to "real" time (m/d/y h:m:s)

到目前为止,我有以下的算法,它给我的感觉有点丑:

So far, I have the following algorithm, which to me feels a bit ugly:

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;
}

您可以假设成员小时所有存在的。

you can assume that the members seconds, minutes, hours, month, day, and year all exist.

使用循环修改原来的时候觉得有点过了,我在想,如果有一个更好的解决了这个。

Using the for loops to modify the original time feels a little off, and I was wondering if there is a "better" solution to this.

推荐答案

要小心闰年在daysInMonth功能。

Be careful about leap years in your daysInMonth function.

如果您希望非常高的性能,您可以precompute两人去月+年一步到位,然后计算出日/时/分/秒。

If you want very high performance, you can precompute the pair to get to month+year in one step, and then calculate the day/hour/min/sec.

一个很好的解决方案是一个在<一个href="http://www.google.com/$c$csearch/p?hl=en&sa=N&cd=2&ct=rc#4kyKlxPG9x4/current/l4minix-03-03-10/src/lib/l4mxc/ansi/gmtime.c&q=gmtime">gmtime来源$ C ​​$ C :

A good solution is the one in the gmtime source code:

/*
 * 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;
}

这篇关于转换信号出现时间到&QUOT;真正的&QUOT;日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆