如何在 C++ 中将 ISO 周数转换为美国周数

How to convert ISO week number to US week number in C++

我正在使用 boost gregorian 库在 C++ 中进行日期计算 提升公历 returns ISO 格式的周数,但我想要美国格式的周数。

如何获得美国格式的周数。

请参阅link 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 month, int day);
        // TODO: Add your methods for this class here.
    };
 }

 int CLRDll::CCalender::GetWeekNumber(int year, int month, int day)
 {
     DateTime^ dt = gcnew DateTime(year, month, day);
     return cal->GetWeekOfYear(*dt, *myCWR, *myFirstDOW);
 }

 __declspec(dllexport) int GetWeekNumber(int year, int month, int day)
 {
     CLRDll::CCalender c;
    return c.GetWeekNumber(year, month, day);
 }