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. QDateTime function toSecsSinceEpoch() returning 0

QDateTime function toSecsSinceEpoch() returning 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 653 Views
  • 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.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by
    #1

    Hello,

    I would like to retrieve the number of seconds since Epoch from a date so I try to use the toSecsSinceEpoch given by the QDateTime class.

    But it always returns 0, do you have any idea on what could generate this problem? (Maybe the fact that my date is not in UTC? But I use toUtc() to try, change nothing)

    Here is the source sample :

        QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate);
        std::cout <<   "New date reception : " << qPrintable(newDate.toString()) << std::endl;
        std::cout <<  "New date secs" << newDate.toSecsSinceEpoch() <<  std::endl;    
    

    Output:

    New date reception Mon Apr 25 12:26:02 2022 GMT
    New date secs : 0
    

    Thank for your answer and sorry for my approximative english :)

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • P Pantoufle

      Hello,

      I would like to retrieve the number of seconds since Epoch from a date so I try to use the toSecsSinceEpoch given by the QDateTime class.

      But it always returns 0, do you have any idea on what could generate this problem? (Maybe the fact that my date is not in UTC? But I use toUtc() to try, change nothing)

      Here is the source sample :

          QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate);
          std::cout <<   "New date reception : " << qPrintable(newDate.toString()) << std::endl;
          std::cout <<  "New date secs" << newDate.toSecsSinceEpoch() <<  std::endl;    
      

      Output:

      New date reception Mon Apr 25 12:26:02 2022 GMT
      New date secs : 0
      

      Thank for your answer and sorry for my approximative english :)

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

      @Pantoufle Should work.
      What does https://doc.qt.io/qt-5/qdatetime.html#isValid return?

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

      1 Reply Last reply
      0
      • P Pantoufle

        Hello,

        I would like to retrieve the number of seconds since Epoch from a date so I try to use the toSecsSinceEpoch given by the QDateTime class.

        But it always returns 0, do you have any idea on what could generate this problem? (Maybe the fact that my date is not in UTC? But I use toUtc() to try, change nothing)

        Here is the source sample :

            QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate);
            std::cout <<   "New date reception : " << qPrintable(newDate.toString()) << std::endl;
            std::cout <<  "New date secs" << newDate.toSecsSinceEpoch() <<  std::endl;    
        

        Output:

        New date reception Mon Apr 25 12:26:02 2022 GMT
        New date secs : 0
        

        Thank for your answer and sorry for my approximative english :)

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @Pantoufle said in QDateTime function toSecsSinceEpoch() returning 0:

        Here is the source sample :

        QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate);
        std::cout <<   "New date reception : " << qPrintable(newDate.toString()) << std::endl;
        std::cout <<  "New date secs" << newDate.toSecsSinceEpoch() <<  std::endl;  
        

        As output and code do not totally match, I am not sure which part is the correct one: code or output.
        Please check if parsing string has really worked:

        QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate);
        std::cout << "New date reception: " << (newDate.isValid() ? "OK" : "INVALID") << std::endl;
        std::cout << "Unix time: " << newDate.toSecsSinceEpoch() << "/" << newDate.toTime_t() << std::endl;
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pantoufle
          wrote on last edited by Pantoufle
          #4

          @Pantoufle Should work.
          What does https://doc.qt.io/qt-5/qdatetime.html#isValid return?

          It return true

          As output and code do not totally match, I am not sure which part is the correct one: code or output.

          It does not tottaly match cause I use a printf custom function in my embedded system so I translate it so you can see clearly.

          Also to check if it was not my string i test with this :

          QDateTime::currentDateTime().toSecsSinceEpoch()
          

          and it also doesn't work :(

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pantoufle
            wrote on last edited by
            #5

            Ok I found it, it was from my printf like function... I just used %d and it doesn't work with quint64 ...
            Sorry and thank for your help

            1 Reply Last reply
            3

            • Login

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