Thread: Problem with <chrono>

  1. #1
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14

    Problem with <chrono>

    Probably somebody can help me with a new library <chrono>

    By some way when I'm trying get system time with nanoseconds for example I have this result:

    9h 7m 55s 0ms 0us 0ns

    In header:

    #include <chrono>
    #include <iostream>
    #include <ctime>

    I don't have any idea if something else is required...

    Software used: Codeblocks
    Compilator: GNU GCC
    C++0x enabled

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the problem that you're facing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14
    I'm expecting some numbers instead of zero in "0ms 0us 0ns". Seems like time is counted only till seconds.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14
    Whole script:

    Code:
    #include <chrono>
    #include <iostream>
    #include <ctime>
    
    
    std::chrono::system_clock::duration duration_since_midnight() {
        auto now = std::chrono::system_clock::now();
    
        time_t tnow = std::chrono::system_clock::to_time_t(now);
        tm *date = std::localtime(&tnow);
        date->tm_hour = 0;
        date->tm_min = 0;
        date->tm_sec = 0;
        auto midnight = std::chrono::system_clock::from_time_t(std::mktime(date));
    
        return now-midnight;
    }
    
    int main()
    {
        auto since_midnight = duration_since_midnight();
    
        auto hours = std::chrono::duration_cast<std::chrono::hours>(since_midnight);
        auto minutes = std::chrono::duration_cast<std::chrono::minutes>(since_midnight - hours);
        auto seconds = std::chrono::duration_cast<std::chrono::seconds>(since_midnight - hours - minutes);
        auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(since_midnight - hours - minutes - seconds);
        auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(since_midnight - hours - minutes - seconds - milliseconds);
        auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(since_midnight - hours - minutes - seconds - milliseconds - microseconds);
    
        std::cout << hours.count() << "h ";
        std::cout << minutes.count() << "m ";
        std::cout << seconds.count() << "s ";
        std::cout << milliseconds.count() << "ms ";
        std::cout << microseconds.count() << "us ";
        std::cout << nanoseconds.count() << "ns\n";
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I am getting output like:
    Code:
    15h 58m 43s 282ms 425us 0ns
    so this could just be platform dependent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14
    Seems like I was thinking about this also...
    I test it on Windows XP and also on Windows 7...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM