Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 1
    The result is 1 (as it has been rounded down) - the [31556952]s is the unit. Use duration.count() if you don't want that printed. Commented yesterday
  • Format strings are documented here, but it seams it doesn't work for year specific parts like %Y: godbolt.org/z/jfsxYeaqj It works for %T. Commented yesterday
  • 1
    I've missed that table is split and year specifiers are in "invalid part": The following specifiers are recognized, but will cause std::format_error to be thrown: Commented yesterday
  • 2
    Yes, that's expected. From beginning of 2025 to beginning of 2026 is less than 31556952 seconds; rounding down gives 0. You perhaps want to convert to floating-point year duration? Commented yesterday
  • 2
    Your question performs chronological arithmetic, but also hints that you may desire calendrical arithmetic. In <chrono> you can do either. Chronological arithmetic operates on fixed units of time. Calendrical arithmetic follows the irregularity of calendars. See this SO Q/A for more details: stackoverflow.com/q/43010362/576911. This concentrates on months arithmetic but the same analogous behavior is also true for years. Commented yesterday