如何解决如何在 C++ 中将 ISO 周数转换为美国周数
我在 C++ 中使用 boost gregorian 库进行日期计算 boost gregorian 以 ISO 格式返回周数,但我想要美国格式的周数。
如何获得美国格式的周数。
请参阅链接 https://planetcalc.com/1252/
例如
#include <boost/date_time/gregorian/gregorian.hpp>
int main(int argc,char* argv[])
{
boost::gregorian::date myday1 = boost::gregorian::from_simple_string("2005-1-1");
int weekNum1 = myday1.week_number(); //return ISO which is 53 //week 1 as US method
boost::gregorian::date myday2 = boost::gregorian::from_simple_string("2005-1-2");
int weekNum2 = myday2.week_number(); //return ISO which is 53 //week 2 as US method
return 0;
}
在 Windows 10 上使用 Visual Studio 2019 社区
解决方法
我在 C++ 或任何支持周数美国方法的库中找不到直接的方法, 所以我必须使用 CLR dll 并从本机 C++ 调用它。
//Header file
#pragma once
__declspec(dllexport) int GetWeekNumber(int year,int month,int day);
C++/CLI 文件
#include "pch.h"
#include "CLRDll.h"
using namespace System;
using namespace System::Globalization;
namespace CLRDll
{
public ref class CCalender
{
static CultureInfo^ cinfo = gcnew CultureInfo("en-US");
static Calendar^ cal = cinfo->Calendar;
// Gets the DTFI properties required by GetWeekOfYear.
static CalendarWeekRule^ myCWR = cinfo->DateTimeFormat->CalendarWeekRule;
static DayOfWeek^ myFirstDOW = cinfo->DateTimeFormat->FirstDayOfWeek;
public:
int GetWeekNumber(int year,int day);
// TODO: Add your methods for this class here.
};
}
int CLRDll::CCalender::GetWeekNumber(int year,int day)
{
DateTime^ dt = gcnew DateTime(year,month,day);
return cal->GetWeekOfYear(*dt,*myCWR,*myFirstDOW);
}
__declspec(dllexport) int GetWeekNumber(int year,int day)
{
CLRDll::CCalender c;
return c.GetWeekNumber(year,day);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。