I am using boost gregorian library for date calculation in c++ boost gregorian returns week number in ISO format but I want week number in US format.
how I can get week number in US format.
please see link https://planetcalc.com/1252/
e.g.
#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; }
Using visual studio 2019 community on windows 10
Answer
I find no direct way in C++ or any library which support US method for week number, So I have to use CLR dll and call it from native C++.
//Header file #pragma once __declspec(dllexport) int GetWeekNumber(int year, int month, int day);
C++/CLI File
#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); }