我正在用c++编写一个黑斑羚udf,当在yyyyMMdd中提供日期时,它会得到一年中的一周。但似乎找不到办法将yyyyMMdd转换为c++的每周一周。在java中,我可以使用Calendar,但是如何在c++中实现它。
提亚
发布于 2014-09-28 06:33:23
您可以只使用来自<ctime>的<ctime>。示例:
std::tm date={};
date.tm_year=2014-1900;
date.tm_mon=9-1;
date.tm_mday=28;
std::mktime(&date);调用之后,date.tm_wday被调整(0=Sunday)。date.tm_yday也作了调整。
要使这一周进入年度,请使用:(date.tm_yday-date.tm_wday+7)/7
此计算返回第一整周的1(即一年中的第一周有一个星期日,包括从星期日开始的年份的1月1日);在第一周的部分时间返回0。
发布于 2019-05-05 08:11:45
我回答了这个这里,但为了完整起见,我也要在这里重复一遍:
使用来自iso_week.h的week.html:
#include <iostream>
#include "iso_week.h"
int main() {
using namespace iso_week;
using namespace std::chrono;
// Get the current time and floor to convert to the sys_days:
auto today = floor<days>(system_clock::now());
// Convert from sys_days to iso_week::year_weeknum_weekday format
auto yww = year_weeknum_weekday{today};
// Print current week number of the year
std::cout << "The current week of " << yww.year() << " is: "
<< yww.weeknum() << std::endl;
// Set any day
auto any_day = 2014_y/9/28;
// Get week of `any_day`
std::cout << "The week of " << any_day.year() << " on `any day` was: "
<< any_day.weeknum() << std::endl;
}它提供了输出:
The current week of 2019 is: W18
The week in 2014 on `any day` was: W09https://stackoverflow.com/questions/26082193
复制相似问题