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

  • SEARCH
  • Home
  • 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 901991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:38:51+00:00 2026-05-15T15:38:51+00:00

I am about to read data From a File which has stored it’s time

  • 0

I am about to read data From a File which has stored it’s time in nanoseconds from 1/1/1970. My problem is I want to read it to a QDateTime object, but it simply does not work as I want it to and the Qt Documentation did not help me either.

Note: milliseconds raster is enough for my purposes
Here my current approach:

void setDateTime(qint64 &ns)
{
    _datetime.setDate(QDate(1970,1,1));
    _datetime.setTime(QTime(0,0,0,0));
    ns /= 1000; //ns are now ms
    qDebug() << "| ms = " << ns;
    qDebug() << "| days = " << static_cast<int>(ns%(60*60*24*1E6));
    _datetime.addDays( static_cast<int>(ns%(60*60*24*1000)) );
    _datetime.addMSecs( ns - ((ns/(60*60*24*1000))*60*60*24*1E6) );
    qDebug() << "| dt = " << _datetime;
}

the result is always

 | dt =  QDateTime("Thu Jan 1 00:00:00 1970") 

which is surely wrong

Can anybody tell where my flaw is? Thanks for any tips and help.

Edit: setTime_t is obviously what I wanted (except for msec resolution), and that works as expected, but I am really curious why the above approach does not work.

Edit changed hack-away bug from 1E6 multiplicative to 1E6

  • 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-05-15T15:38:52+00:00Added an answer on May 15, 2026 at 3:38 pm

    QDateTime::addDays() and QDateTime::addMSecs() are const functions returning a new QDateTime. You’re simply throwning the return value away.

    And yes, this is written in the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.