time_t转换格式问题 [英] time_t conversion format question

查看:119
本文介绍了time_t转换格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个容易访问的TimeDate变量,但我有转换的问题。在time.h中,我如何将time_t(自1970年1月1日以来的秒数)转换为当前本地时区(如果适用,补偿夏令时),以便:

I am trying to make an easily accessible TimeDate variable, but am having problems with conversion. In time.h, how would I convert time_t (seconds since 1/1/1970), into the current local timezone (compensating for daylight savings time if applicable), so that:

time_t Seconds;

成为:

struct TimeDate
{
    short YYYY;
    unsigned char MM;
    unsigned char DD;

    unsigned char HH; //Non-DST, non-timezone, IE UTC (user has to add DST and TZO to get what they need)
    unsigned char MM;
    unsigned char S;

    char TZ[4]; //This can be optionally a larger array, null terminated preferably
    char TZO; //Timezone Offset from UTC        

    char DST; //Positive is DST (and amount of DST to apply), 0 is none, negative is unknown/error
};

在进程中不使用任何字符串(时区名称的字符串) ?这也考虑到闰年。如果TimeDate可以转换回time_t。

Without using any string literals (bar for the timezone name) in the process (to keep it efficient)? This is also taking into account leap years. Bonus if TimeDate can be converted back into time_t.

推荐答案

C标准库(可通过C ++使用 ctime )为此目的提供 localtime (或者UTC的 gmtime )。你可以把结果 struct tm 变成你自己的结构,如果有一些原因,为什么标准的一个不能满足你的需要。

The C standard library (accessible in C++ by using ctime) provides localtime for exactly that purpose (or gmtime for UTC). You could shoe-horn the resultant struct tm into your own structure after that if there's some reason why the standard one is not sufficient to your needs.

它不提供的一件事是时区本身,但你可以通过使用 strftime 得到它(和ISO 8601格式的偏移量)与%Z %z 格式字符串

The one thing it doesn't provide is the timezone itself but you can get that (and the offset in ISO 8601 format) by using strftime with the %Z and %z format strings

下面是一个演示这个操作的程序:

By way of example, here's a program that demonstrates this in action:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main(void) {
    time_t t;
    struct tm *tim;
    char tz[32];
    char ofs[32];

    std::system ("date");
    std::cout << std::endl;

    t = std::time (0);
    tim = std::localtime (&t);
    std::strftime (tz, sizeof (tz), "%Z", tim);
    std::strftime (ofs, sizeof (ofs), "%z", tim);

    std::cout << "Year:        " << (tim->tm_year + 1900) << std::endl;
    std::cout << "Month:       " << (tim->tm_mon + 1) << std::endl;
    std::cout << "Day:         " << tim->tm_mday << std::endl;
    std::cout << "Hour:        " << tim->tm_hour << std::endl;
    std::cout << "Minute:      " << tim->tm_min << std::endl;
    std::cout << "Second:      " << tim->tm_sec << std::endl;
    std::cout << "Day of week: " << tim->tm_wday << std::endl;
    std::cout << "Day of year: " << tim->tm_yday << std::endl;
    std::cout << "DST?:        " << tim->tm_isdst << std::endl;
    std::cout << "Timezone:    " << tz << std::endl;
    std::cout << "Offset:      " << ofs << std::endl;

    return 0;
}

当我在盒子上运行时,我看到:

When I run this on my box, I see:

Wed Sep 28 20:45:39 WST 2011

Year:        2011
Month:       9
Day:         28
Hour:        20
Minute:      45
Second:      39
Day of week: 3
Day of year: 270
DST?:        0
Timezone:    WST
Offset:      +0800

这篇关于time_t转换格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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