简体   繁体   English

time_t转换格式问题

[英]time_t conversion format question

I am trying to make an easily accessible TimeDate variable, but am having problems with conversion. 我正在尝试创建一个易于访问的TimeDate变量,但我遇到了转换问题。 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.h中,如何将time_t(自1970年1月1日以来的秒数)转换为当前本地时区(如果适用则补偿夏令时),以便:

time_t Seconds;

Becomes: 变为:

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

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. 如果TimeDate可以转换回time_t,则可以获得奖励。

The C standard library (accessible in C++ by using ctime ) provides localtime for exactly that purpose (or gmtime for UTC). (在C ++中,通过使用可访问的C标准库ctime )提供localtime为正是目的(或gmtime对于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. 如果有一些原因导致标准的结构不足以满足您的需求,那么您可以将结果struct tm转换成您自己的结构。

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 它没有提供的一件事是时区本身但你可以通过使用带有%Z%z格式字符串的strftime来获得它(以及ISO 8601格式的偏移量)


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM