简体   繁体   English

如何使用date.h在C ++中获取当天的当前日期?

[英]How do I get the current day of week in C++ using date.h?

I am using C++ 14 and am trying to get the current day of week. 我正在使用C ++ 14,并试图获得当周的当天。 After some reading I am using date.h by Howard E. Hinnant. 一些阅读后,我使用date.h霍华德E. Hinnant(欣南特)。

However I am struggling to get a day of the week (encoded as 0 thru 6). 但是我很难得到一周的一天(编码为0到6)。

Something like this prints out Thu : 像这样的东西打印出Thu

 int main(void)
 {
     date::sys_days t;
     weekday wd{t};
     cout << wd << '\n';
 }

Many answers on this site regarding getting the current day of the week use chrono . 本网站上关于获取当周当天的许多答案都使用了chrono

How can I print the current weekday as a range from 0-6 according to when the program is run, using date.h ? 如何使用date.h打印当前工作日作为程序运行时间的0-6范围?

For example, if I run the program today (Tuesday) I would expect a value of 2. 例如,如果我今天(星期二)运行程序,我希望值为2。

Any suggestions would be appreciated. 任何建议,将不胜感激。


Just to be clear about what I am trying to achieve, something similar in Java: 为了清楚我想要实现的目标,在Java中类似的东西:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

The problem with your code is that t is not being initialized to anything interesting. 你的代码的问题是t没有被初始化为任何有趣的东西。 You just need to set it to "now". 你只需要将它设置为“现在”。 There's a slight hitch, though: now() returns a different kind of time point, that can't automagically be converted into a date::sys_days value. 但是有一个小小的障碍: now()返回一个不同类型的时间点,不能自动转换为date::sys_days值。

So, here's the minimal change to make your program work: 所以,这是让你的程序工作的最小变化:

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

int main()
{
     auto now = std::chrono::system_clock::now();
     date::sys_days t { std::chrono::time_point_cast<date::days>(now) };
     date::weekday wd{t};
     std::cout << wd << '\n';
}

edit: Now let's do a bit more, and thanks @HowardHinnant for the informative comments. 编辑:现在让我们再做一点,感谢@HowardHinnant提供的信息。

Use more meaningful names 使用更有意义的名称

Replace the code for main() with main()替换main()的代码

auto now = std::chrono::system_clock::now();
date::sys_days now_in_days { std::chrono::time_point_cast<date::days>(now) };
date::weekday weekday {now_in_days};
std::cout << weekday << '\n';

Get the weekday as a number 将工作日作为一个数字

You said you wanted to get the weekday as a number. 你说你想把工作日作为一个数字。 Well, we can do that: 好吧,我们可以这样做:

auto weekday_index = weekday.c_encoding();

the type will be unsigned , and the value will be in the range 0...6 类型将是unsigned ,值将在0 ... 6范围内

Get the weekday in your own timezone 在您自己的时区获取工作日

Your code and mine, so far, is UTC. 到目前为止,您的代码和我的代码是UTC。 Which is good as a default, but may give you something surprising as the weekday. 这是一个默认的好,但可能会给你一个惊人的工作日。 We can use zoned_time and write: 我们可以使用zoned_time并写:

auto now = std::chrono::system_clock::now();
auto now_local = zoned_time{current_zone(), now}.get_local_time();
date::sys_days now_local_in_days { std::chrono::time_point_cast<date::days>(now_local) };
auto weekday_index = now_local_in_days.c_encoding();

Gotcha for dates before the epoch (1970) 在纪元之前的日期(1970年)

This is super annoying, but time_point_cast() may not always do what you want! time_point_cast()烦人了,但是time_point_cast()可能并不总能做到你想要的! Howard says that for days before 1970 we need to use floor() instead: 霍华德说,在1970年之前的几天,我们需要使用floor()代替:

date::sys_days now_local_in_days { std::chrono::floor<date::days>(now_local) };

Final program 最后的节目

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

int main()
{
    auto now = std::chrono::system_clock::now();
    auto now_local = zoned_time{current_zone(), now}.get_local_time();
    date::sys_days now_local_in_days { std::chrono::floor<date::days>(now_local) };
    auto weekday_index = now_local_in_days.c_encoding();
    std::cout << weekday_index << '\n';
}

And if today is Tuesday, the resulting output should be 2 . 如果今天是星期二,那么结果输出应该是2

This would give the index of the day using date.h: 这将使用date.h给出当天的索引:

using namespace date;
using namespace std::chrono;
auto now = system_clock::now();
std::cout << "The current time is " << now << " UTC\n";
auto current_day = (year_month_weekday{floor<days>(now)}.weekday() - Sunday).count();
std::cout << "current_day " << current_day << "\n";

you can also use std::time_get::get_weekday 你也可以使用std::time_get::get_weekday

#include <iostream>       // std::cout, std::ios
#include <sstream>        // std::istringstream
#include <ctime>          // std::tm
#include <locale>         // std::locale, std::time_get, std::use_facet

int main ()
{
  std::locale loc;        // classic "C" locale

  // get time_get facet:
  const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("Friday");
  std::tm when;

  tmget.get_weekday (iss, std::time_get<char>::iter_type(), iss, state, &when);

  std::cout << "weekday: " << when.tm_wday << '\n';
  return 0;
}

or: 要么:

time_t t = time(nullptr);
tm* timePtr = localtime(&t);
uint32_t y = timePtr->tm_year + 1900;
uint32_t m = timePtr->tm_mon + 1;
uint32_t d = timePtr->tm_wday;
cout<< d<<endl;

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

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