Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:58:07+00:00 2026-06-13T04:58:07+00:00

I wanted to have a PrintDuration function that prints how long a task took

  • 0

I wanted to have a PrintDuration function that prints how long a task took and would allow the user to specify the units in which to print. Example usage:

auto start = std::chrono::system_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(3));
PrintDuration<std::chrono::microseconds>(std::cout, start, "Waited for:");

and it would print something like: "Waited for: 3000000μs\n"

My question is: how to find the units supplied by the user?

At the moment, I am using the following:

template <class T>
std::intmax_t GetNum(const T& duration)
{
    return T::period::num;
}

template <class T>
std::intmax_t GetDen(const T& duration)
{
    return T::period::den;
}


typedef std::chrono::time_point<std::chrono::system_clock> TimePoint_t;

template <class Duration = std::chrono::seconds>
void PrintDuration(std::ostream& output,
                   const TimePoint_t& start,
                   std::string message) 
{
    auto duration = std::chrono::system_clock::now() - start;
    auto convertedDuration = std::chrono::duration_cast<Duration>(duration);
    auto count = convertedDuration.count();

    std::string units("");

    if (GetNum(convertedDuration) == 1)
    {
        if (GetDen(convertedDuration) == std::nano::den)    
            units = "ns";
        else if (GetDen(convertedDuration) == std::micro::den)
            units = "\u03BCs"; // us
        else if (GetDen(convertedDuration) == std::milli::den)
            units = "ms";
        else if (GetDen(convertedDuration) == 1)
            units = "s";
        else
            units = "";
    }
    else if(GetDen(convertedDuration) == 1)
    {
        if (GetNum(convertedDuration) == 60)
            units = "m";
        else if (GetNum(convertedDuration) == 3600)
            units = "h";
        else
            units = "";
    }

    output << message << ' ' << count << units << '.' << std::endl;
}

While that works fine, it seems quite verbose. I looked at the documentation for C++ but couldn’t find it. Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:58:08+00:00Added an answer on June 13, 2026 at 4:58 am

    You could write it like this:

    template <typename T>
    const wchar_t* GetUnits(T) { return L"unknown units"; }
    
    template<>
    const wchar_t* GetUnits(std::chrono::nanoseconds) { return L"ns"; }
    
    template<>
    const wchar_t* GetUnits(std::chrono::microseconds) { return L"\u03BCs"; }
    
    template<>
    const wchar_t* GetUnits(std::chrono::milliseconds) { return L"ms"; }
    
    typedef std::chrono::time_point<std::chrono::system_clock> TimePoint_t;
    
    template <class Duration>
    void PrintDuration(std::wostream& output, const TimePoint_t& start, const std::wstring& message) 
    {
        auto duration = std::chrono::system_clock::now() - start;
        auto convertedDuration = std::chrono::duration_cast<Duration>(duration);
    
        output << message << L" " << convertedDuration.count() << GetUnits(convertedDuration) << L"." << std::endl;
    }
    
    int main(int /*argc*/, char* /*argv*/[])
    {
        auto start = std::chrono::system_clock::now();
        std::this_thread::sleep_for(std::chrono::seconds(3));
        PrintDuration<std::chrono::microseconds>(std::wcout, start, L"Waited for:");
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to have a function in PLSQL that would return an set of
I wanted to have a list of lambdas that act as sort of a
If I wanted to have a collection that described the (recursive) contents of a
I have a listview in which i wanted to have custom typeface e.g. Arial.
I have some data (TEMP_2) and I wanted to obtain a distribution for that
I have wanted to use a HashMap that maps a value of type String
I wanted to have a javascript function to basically return an array of all
I wanted to have a popup window when the user opens the application for
I wanted to have a feature in my Jommla based application, that when ever
i have a problem in android, first i wanted to have a button which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.