微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?