Namespaces
Variants
Views
Actions

std::chrono::operator+, std::chrono::operator- (std::chrono::year)

From cppreference.com
< cpp‎ | chrono‎ | year
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
Date and time utilities
(C++11)
(C++11)
Time of day
(C++20)



(C++20)(C++20)(C++20)(C++20)
Clocks
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
 
 
constexpr std::chrono::year operator+(const std::chrono::year& y,
                                      const std::chrono::years& ys) noexcept;
(1) (since C++20)
constexpr std::chrono::year operator+(const std::chrono::years& ys,
                                      const std::chrono::year& y) noexcept;
(2) (since C++20)
constexpr std::chrono::year operator-(const std::chrono::year& y,
                                      const std::chrono::years& ys) noexcept;
(3) (since C++20)
constexpr std::chrono::years operator-(const std::chrono::year& y1,
                                       const std::chrono::year& y2) noexcept;
(4) (since C++20)
1-2) Adds ys.count() years to y.
3) Subtracts ys.count() years from y.
4) Returns the difference in years between y1 and y2.

Contents

[edit] Return value

1-2) std::chrono::year(int(y) + ys.count())
3) std::chrono::year(int(y) - ys.count())
4) std::chrono::years(int(y1) - int(y2))

[edit] Notes

If the resulting year value for (1-3) is outside the range [-32767,32767], the actual value stored is unspecified.

The result of subtracting two year values is a duration of type std::chrono::years. This duration unit represents the length of the average Gregorian year, and the resulting duration bears no relationship to the number of days in the particular years represented by the operands. For example, the result of 2018y - 2017y is std::chrono::years(1), which represents 365.2425 days, not 365 days.


[edit] Example

#include <iostream>
#include <chrono>
 
int main()
{
 
    std::chrono::year y {2020};
 
    y = y + std::chrono::years(12);
    if (y == std::chrono::year(2032)) {
        std::cout << "Years added correctly" << "\n";
    } else {
        std::cout << "Years not added correctly" << "\n";
    }
 
    y = y - std::chrono::years(33);
    if (y == std::chrono::year(1999)) {
        std::cout << "Years subtracted correctly" << "\n";
    } else {
        std::cout << "Years not subtracted correctly" << "\n";
    }
 
    std::chrono::years ys = std::chrono::year(2025) - std::chrono::year(2020);
    if (ys == std::chrono::years(5)) {
        std::cout << "Years subtracted correctly" << "\n";
    } else {
        std::cout << "Years not subtracted correctly" << "\n";
    }
 
}

Output:

Years added correctly
Years subtracted correctly
Years subtracted correctly

[edit] See also

increments or decrements the month
(public member function of std::chrono::month) [edit]
adds or subtracts a number of months
(public member function of std::chrono::month) [edit]