简体   繁体   English

如何从C ++中的tm对象获取星期几?

[英]How to get the day of the week from a tm object in C++?

I'm trying to write a program which parses a string representing a date formatted as YYYYMMDD (using strptime() ) and prints it in the form of dayOfWeek, Month Day, Year (using put_time() ). 我正在尝试编写一个程序,该程序解析一个表示日期格式为YYYYMMDD的字符串(使用strptime() ),并以dayOfWeek,Month Day,Year(使用put_time() )的形式打印它。 Here's what I have so far: 这是我到目前为止的内容:

#include <iostream>
#include <sstream>
#include <ctime>
#include <iomanip>

using namespace std;

int main() {
    struct tm tm;
    string s("20131224");
    if (strptime(s.c_str(), "%Y%m%e", &tm)) {
        cout << put_time(&tm, "%A, %B %e, %Y") << endl;
    }
}

The problem is that the day of the week is always a Sunday, regardless of the date. 问题在于,星期几始终是星期日,而不考虑日期。

It appears to be a problem with strptime() not populating the day of the week information if only given a year, month, and day, and then put_time() not filling in this information either. 这似乎是一个问题strptime()未填充的周信息的一天,如果只给了一年,月,日,然后put_time()在这个信息不灌装两种。

According to this documentation for strftime() , "missing fields in the tm structure may be filled in by strftime() if given enough information." 根据有关strftime() 文档 ,“如果给出足够的信息,则strftime()可能会填充tm结构中的缺失字段”。 I haven't found the same information regarding put_time() (which strftime() appears to be based on), so perhaps I'm expecting too much of the function. 我没有找到与put_time()相同的信息( strftime()似乎基于该信息),所以也许我期望的功能太多。

Can strptime() automatically fill in the day of the week ( tm_wday ) on input, given a year, month, and day? 可以strptime()自动填写在本周(当天tm_wday上输入),给出年,月,日? Or can put_time() automatically fill this information on output? 还是put_time()可以在输出中自动填充此信息? If not, is there another way to add this information to a tm object? 如果不是,是否有另一种方法可以将此信息添加到tm对象?

Here's a way to do it without using the C API, but instead the C++11/14 <chrono> facilities and this free, open-source, header-only library . 这是一种无需使用C API即可实现的方法,而是使用C ++ <chrono>设施以及这个免费的,开放源代码的,仅标头的库

#include "date.h"
#include <iostream>
#include <sstream>

using namespace std;

int main() {
    istringstream s("20131224");
    date::sys_days tp;
    s >> date::parse("%Y%m%e", tp);
    if (!s.fail())
        cout << date::format("%A, %B %e, %Y", tp) << endl;
}

Output: 输出:

Tuesday, December 24, 2013

date::sys_days above is just a typedef for a std::chrono::system_clock::time_point but with a precision of days instead of whatever your platform provides (microseconds, nanoseconds, whatever). 上面的date::sys_days只是std::chrono::system_clock::time_pointtypedef ,但精度为days而不是平台提供的精度(微秒,纳秒等)。 And that means you can easily add other durations to it, such as std::chrono::hours, minutes, seconds, milliseconds, etc. 这意味着您可以轻松地向其添加其他持续时间,例如std :: chrono ::小时,分钟,秒,毫秒等。

cout << date::format("%A, %B %e, %Y %H:%M", tp + 2h + 35min ) << endl;

Tuesday, December 24, 2013 02:35

You can paste the above code into this wandbox demo and try it out for yourself for various versions of clang and gcc: 您可以将上面的代码粘贴到此wandbox演示中,并针对各种版本的clang和gcc尝试一下:

http://melpon.org/wandbox/permlink/PodYB3AwdYNFKbMv http://melpon.org/wandbox/permlink/PodYB3AwdYNFKbMv

Try this (compiled in Mac) 试试看(在Mac中编译)

#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    const string DAY[]={"Sun","Mon","Tue",
        "Wed","Thu","Fri","Sat"};

    time_t rawtime;
    tm * timeinfo;
    time(&rawtime);
    timeinfo=localtime(&rawtime);

    int weekday=timeinfo->tm_wday;
    cout << "Today is: " << DAY[weekday] << "\n" << endl;
    return 0;
}

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

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