Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I get a difference between two DateTime intervals in micro and nano seconds?
Forum Updated to NodeBB v4.3 + New Features

How can I get a difference between two DateTime intervals in micro and nano seconds?

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 6 Posters 10.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • srikanthS Offline
    srikanthS Offline
    srikanth
    wrote on last edited by A Former User
    #1

    am getting milliseconds like this

    quint64 ITime::elapsed()
    {
    oEndTime = QDateTime::currentDateTime();
    quint64 elapsed = qAbs(oStartTime.date().daysTo(oEndTime .date()));
    elapsed *= static_cast<quint64>(24); // days to hours
    elapsed *= static_cast<quint64>(60); // hours to minutes
    elapsed *= static_cast<quint64>(60); // minutes to seconds
    elapsed *= static_cast<quint64>(1000); // seconds to milliseconds
    elapsed += qAbs(oStartTime.time().msecsTo(oEndTime .time()));
    return elapsed;
    }
    

    am getting milliseconds here , but i want micro and nano seconds in difference .

    am using Qt 4.3.2 version , in visual studio 2005.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't, dates are calculated as milliseconds since epoch.

      If you want something with a higher level of details to measure a duration then you should take a look at std::chrono::duration.

      Note that you are currently developing with a pretty outdated version of Qt as well as compiler. If possible, you should upgrade.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      srikanthS 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        You can't, dates are calculated as milliseconds since epoch.

        If you want something with a higher level of details to measure a duration then you should take a look at std::chrono::duration.

        Note that you are currently developing with a pretty outdated version of Qt as well as compiler. If possible, you should upgrade.

        srikanthS Offline
        srikanthS Offline
        srikanth
        wrote on last edited by
        #3

        @SGaist chrono is not working for me
        i try like this with #include <chrono>

        	auto start = std::chrono::high_resolution_clock::now();
        
        	auto finish = std::chrono::high_resolution_clock::now();
        	std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << "ns\n";
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          in what way "not working" ?

          #include <iostream>
          #include <chrono>
          using namespace std;
           
          int main()
          {
          	cout << chrono::high_resolution_clock::period::den << endl;
          	auto start_time = chrono::high_resolution_clock::now();
          	int temp;
          	for (int i = 0; i< 242000000; i++)
          		temp+=temp;
          	auto end_time = chrono::high_resolution_clock::now();
          	cout << chrono::duration_cast<chrono::seconds>(end_time - start_time).count() << ":";
          	cout << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << ":";
          	return 0;
          }
          
          srikanthS 1 Reply Last reply
          0
          • mrjjM mrjj

            in what way "not working" ?

            #include <iostream>
            #include <chrono>
            using namespace std;
             
            int main()
            {
            	cout << chrono::high_resolution_clock::period::den << endl;
            	auto start_time = chrono::high_resolution_clock::now();
            	int temp;
            	for (int i = 0; i< 242000000; i++)
            		temp+=temp;
            	auto end_time = chrono::high_resolution_clock::now();
            	cout << chrono::duration_cast<chrono::seconds>(end_time - start_time).count() << ":";
            	cout << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << ":";
            	return 0;
            }
            
            srikanthS Offline
            srikanthS Offline
            srikanth
            wrote on last edited by
            #5

            @mrjj fatal error C1083: Cannot open include file: 'chrono': No such file or directory
            error is showing like this

            mrjjM 1 Reply Last reply
            0
            • srikanthS srikanth

              @mrjj fatal error C1083: Cannot open include file: 'chrono': No such file or directory
              error is showing like this

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @srikanth
              Ok, sounds like you dont have it then.
              VS 2005 is epic old compiler wise.
              Its not possible to use c++11 with it.

              srikanthS 1 Reply Last reply
              0
              • mrjjM mrjj

                @srikanth
                Ok, sounds like you dont have it then.
                VS 2005 is epic old compiler wise.
                Its not possible to use c++11 with it.

                srikanthS Offline
                srikanthS Offline
                srikanth
                wrote on last edited by
                #7

                @mrjj is there any way to get precision time difference (micro & nano) in my environment , am struck here , please help me to get out from this , thanks in advance :)

                mrjjM 1 Reply Last reply
                0
                • srikanthS srikanth

                  @mrjj is there any way to get precision time difference (micro & nano) in my environment , am struck here , please help me to get out from this , thanks in advance :)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @srikanth
                  Im not sure nano is an option unless high precision timer is available.

                  You can google on ways to time functions etc in c++
                  Sounds like you want something like that
                  http://stackoverflow.com/questions/15092504/how-to-time-a-function-in-milliseconds-without-boosttimer

                  There are also native windows functions etc but i do not know what task or requirements you have.

                  There are a lot on google.

                  1 Reply Last reply
                  2
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    boost chrono is an option, I don't think it needs C++11.

                    Do you need to get the difference between 2 timers (so you have to get the current time) or is it the difference of some static data-points you have? in the second case you can handle it quite simply manually

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    3
                    • J.HilkJ Online
                      J.HilkJ Online
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      You can use

                      #include <QElapsedTimer>
                      

                      its has the function

                      qint64 QElapsedTimer::nsecsElapsed() const
                      

                      that is nano seconds since last started.
                      Available since Qt 4.7.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        That's what I would have proposed except his environment uses Qt 4.3.2

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        J.HilkJ 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          That's what I would have proposed except his environment uses Qt 4.3.2

                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by J.Hilk
                          #12

                          @SGaist
                          Oh, I missed that, sorry.
                          For some reason all I thought was "pre Qt5.0 so it fits" .


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          mrjjM 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @SGaist
                            Oh, I missed that, sorry.
                            For some reason all I thought was "pre Qt5.0 so it fits" .

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Hi
                            He is trying the boost chrono as @VRonin suggest but
                            im not sure building it with VS 2005 is great joy. ? :)

                            VRoninV 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              Hi
                              He is trying the boost chrono as @VRonin suggest but
                              im not sure building it with VS 2005 is great joy. ? :)

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              @mrjj The current release uses C++11 but boost version 1.47.0 does not. I also think boost::chrono and std::chrono are the same thing

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              mrjjM 1 Reply Last reply
                              1
                              • VRoninV VRonin

                                @mrjj The current release uses C++11 but boost version 1.47.0 does not. I also think boost::chrono and std::chrono are the same thing

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @VRonin
                                Ah and such old version might have makefile for Vs 2005 included.
                                We cross fingers :=)

                                1 Reply Last reply
                                1
                                • srikanthS Offline
                                  srikanthS Offline
                                  srikanth
                                  wrote on last edited by
                                  #16

                                  can i use chrono in VS 2005 , am unable to get nanoseconds is there anyway to get nanoseconds in vs 2005 & Qt 4.3.2

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • srikanthS srikanth

                                    can i use chrono in VS 2005 , am unable to get nanoseconds is there anyway to get nanoseconds in vs 2005 & Qt 4.3.2

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @srikanth As @VRonin said you can use Boost 1.47.0 (boost::chrono: https://theboostcpplibraries.com/boost.chrono).

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    srikanthS 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @srikanth As @VRonin said you can use Boost 1.47.0 (boost::chrono: https://theboostcpplibraries.com/boost.chrono).

                                      srikanthS Offline
                                      srikanthS Offline
                                      srikanth
                                      wrote on last edited by srikanth
                                      #18

                                      @jsulm i downloaded the boost.zip file from this link
                                      [http://www.boost.org/users/history/version_1_63_0.html]

                                      unzip them and then add something like INCLUDEPATH += C:/boost/boost_1_63_0/ to my pro file , if i run this code then it is getting error like this "fatal error LNK1104: cannot open file 'libboost_chrono-vc80-mt-gd-1_63.lib' " so what can i do now ? to get this lib file .

                                      mrjjM jsulmJ 2 Replies Last reply
                                      0
                                      • srikanthS srikanth

                                        @jsulm i downloaded the boost.zip file from this link
                                        [http://www.boost.org/users/history/version_1_63_0.html]

                                        unzip them and then add something like INCLUDEPATH += C:/boost/boost_1_63_0/ to my pro file , if i run this code then it is getting error like this "fatal error LNK1104: cannot open file 'libboost_chrono-vc80-mt-gd-1_63.lib' " so what can i do now ? to get this lib file .

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @srikanth
                                        Try the 1.47.0 one.
                                        1_63 might be too new for VS 2005.

                                        1 Reply Last reply
                                        2
                                        • srikanthS srikanth

                                          @jsulm i downloaded the boost.zip file from this link
                                          [http://www.boost.org/users/history/version_1_63_0.html]

                                          unzip them and then add something like INCLUDEPATH += C:/boost/boost_1_63_0/ to my pro file , if i run this code then it is getting error like this "fatal error LNK1104: cannot open file 'libboost_chrono-vc80-mt-gd-1_63.lib' " so what can i do now ? to get this lib file .

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @srikanth You need to link to the library mentioned in the error message:

                                          LIBS += -LPATH_TO_BOOST_LIBS -lboost_chrono-vc80-mt-gd-1_63
                                          

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved