Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

706
Views
C# to C++: Convert C# Time calculation to C++ chrono

I've got the task to convert some C# code to C++ and I've problems with chrono. Goal is to round a time to a variable time span.

C#:

int iRoundTo = 30; // can be 45 or other variable value, not a const
DateTime dt = Floor(DateTime.Now, new TimeSpan(0, 0, 0, iRoundTo));

I found a solution with const iRoundTo but this is not what I'm looking for. How do I convert this to C++ with using std::chono?

C++:

std::chrono::seconds diff(iRoundTo);
auto dt = std::chrono::floor<diff>(Now);

This is not working due to compile error.

Thanks.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'm doing a bit of guessing with this answer, but my guess is that you want to truncate the current time to the floor of the current half minute (in case of iRoundTo == 30).

If I'm correct, this is easy to do as long as iRoundTo is a compile-time constant.

#include <chrono>
#include <iostream>

int
main()
{
    using RoundTo = std::chrono::duration<int, std::ratio<30>>;
    auto Now = std::chrono::system_clock::now();
    auto dt = std::chrono::floor<RoundTo>(Now);
    std::cout << dt << '\n';
}

The above creates a new duration type that is 30 seconds long. It then gets the current time and floors it (truncates downwards) to the previous half minute unit. It then prints out the result. Everything before the print works in C++17. The printing (the last line) requires C++20.

Example output for me:

2022-01-18 01:45:30

The above code can also work pre-C++17 but in that case you'll need to find your own floor (e.g. date.h) or use std::chrono::duration_cast which truncates towards zero.

Update

In the comments below, it is explained that iRoundTo is not a compile-time constant, but does always represent a multiple of seconds. In this case I would do this in two steps:

int iRoundTo = 30;
auto Now = std::chrono::system_clock::now();

// First truncate to seconds
auto dt = std::chrono::floor<std::chrono::seconds>(Now);
// Then subtract of the modulus according to iRoundTo
dt -= dt.time_since_epoch() % iRoundTo;

The sub-expression dt.time_since_epoch() % iRoundTo has type seconds and a value between 0s and seconds{iRoundTo} - 1s.

over 4 years ago · Santiago Trujillo Report

0

Without access to the newest tools (and both std::format and << overload seem to require rather new tools) I've got got it tested only so far (please edit if you can test it further):

#include <chrono>
#include <iostream>
#include <ctime>
#include <iomanip>
//#include <format>

int main() {
    int roundTo = 15;
    auto dt = std::chrono::floor<std::chrono::seconds> (std::chrono::system_clock::now());
    dt -= dt.time_since_epoch() % roundTo;
    std::time_t t = std::chrono::system_clock::to_time_t(dt);
    std::cout << std::ctime(&t); // Using <ctime>
    std::cout << std::put_time(std::localtime(&t), "%Y-%m-%e %H:%M:%S") << '\n'; // Using <iomanip>
    //std::cout << "date: " << std::format("%Y-%m-%e %H:%M:%S", dt) << '\n'; // Using <format>
    //std::cout << "date: " << std::format("{%Y-%m-%e %H:%M:%S}", dt) << '\n'; // Using newer <format>
    //std::cout << dt << '\n'; // Using << overload
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!