C++ 用boost检索当前世纪

C++ 用boost检索当前世纪,c++,datetime,boost,C++,Datetime,Boost,我可以通过拨打以下电话检索当前年份: boost::posix_time::second_clock::local_time().date().year(); 但是我如何用boost函数从那一年中提取世纪数呢?除以100有什么错?除以100有什么错?一个世纪在所有地区都被定义为100年。然而,有一个问题是世纪何时开始。(你是从零开始算还是从一开始算?)假设是公历,或者是与教皇格雷格(Pope Greg)就开始计算时间达成一致的日历: #include <iostream> in

我可以通过拨打以下电话检索当前年份:

boost::posix_time::second_clock::local_time().date().year(); 

但是我如何用boost函数从那一年中提取世纪数呢?

除以100有什么错?

除以100有什么错?

一个世纪在所有地区都被定义为100年。然而,有一个问题是世纪何时开始。(你是从零开始算还是从一开始算?)假设是公历,或者是与教皇格雷格(Pope Greg)就开始计算时间达成一致的日历:

#include <iostream>

int yearToCentury(int year)
{
    return (year + 99) / 100;
}

int main()
{
    std::cout << 1999 << ": " << yearToCentury(1999) << std::endl;
    std::cout << 2000 << ": " << yearToCentury(2000) << std::endl;
    std::cout << 2001 << ": " << yearToCentury(2001) << std::endl;
    return 0;
}

但是,我在标准库或boost中找不到任何函数的证据来为您进行此计算。

在所有地区,一个世纪被定义为100年。然而,有一个问题是世纪何时开始。(你是从零开始算还是从一开始算?)假设是公历,或者是与教皇格雷格(Pope Greg)就开始计算时间达成一致的日历:

#include <iostream>

int yearToCentury(int year)
{
    return (year + 99) / 100;
}

int main()
{
    std::cout << 1999 << ": " << yearToCentury(1999) << std::endl;
    std::cout << 2000 << ": " << yearToCentury(2000) << std::endl;
    std::cout << 2001 << ": " << yearToCentury(2001) << std::endl;
    return 0;
}

但是,我在标准库或boost中找不到任何函数可以为您进行此计算的证据。

首先使用boost获取当前日期时间:

boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();
auto century = GetCentury(timeLocal.date().year());
获取当前世纪的方法:

/// <summary>Gets century from year.</summary>
/// <param name="year">The year.</param>
/// <returns>The century as int value.</returns>
int GetCentury(int year)
{
    if (year % 100 == 0)
        return year / 100;
    else
        return year / 100 + 1;
}
///从年份中获取世纪。
///今年。
///世纪是一个完整的价值观。
整数世纪(整数年)
{
如果(年份%100==0)
返回年份/100;
其他的
返回年份/100+1;
}

首先使用boost获取当前日期时间:

boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();
auto century = GetCentury(timeLocal.date().year());
获取当前世纪的方法:

/// <summary>Gets century from year.</summary>
/// <param name="year">The year.</param>
/// <returns>The century as int value.</returns>
int GetCentury(int year)
{
    if (year % 100 == 0)
        return year / 100;
    else
        return year / 100 + 1;
}
///从年份中获取世纪。
///今年。
///世纪是一个完整的价值观。
整数世纪(整数年)
{
如果(年份%100==0)
返回年份/100;
其他的
返回年份/100+1;
}

并添加一个。(2014/100+1=21)除法前加99。2000年是20世纪。这就是为什么我要求一个升压函数的原因。也许有一个?再加上一个。(2014/100+1=21)除法前加99。2000年是20世纪。这就是为什么我要求一个升压函数的原因。也许有一个?这是简单的算术。我认为这样一个函数不值得,它只是简单的算术运算。我认为这样一个功能是不值得的。