簡體   English   中英

如何在 C++ 中將 ISO 周數轉換為美國周數

[英]How to convert ISO week number to US week number in C++

我在 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 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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM