1

I am using a python program which uses time.time() function in variable as:

currentTime = time.time()

Now I am trying to write same function in C++:

lastCallTime = std::time(nullptr);

Unfortunately, the return value is not the same, sure should not be 100 % the same, but it should more close if I am not wrong.

from python return 1598271246.1680057 and

from c++ return 1598271273

in c++ I am using ctime library, chrono did not help either or I did not use properly.

15
  • DId you try this: stackoverflow.com/questions/997946/…
    – Omer
    Aug 24, 2020 at 12:25
  • 1
    @Omer yes it's not returning that close python func either :/
    – user12640668
    Aug 24, 2020 at 12:26
  • I got almost same values from them. What is your environment? How are you executing the programs?
    – MikeCAT
    Aug 24, 2020 at 12:28
  • You did everything by the book, the 23 seconds difference may be due to anything. Both calculations start from 00:00 hours, Jan 1, 1970 UTC, but have they really taken the 00:00:00 instant with atomic clock precision?
    – Giogre
    Aug 24, 2020 at 12:29
  • 1
    Okay this may sound crazy but is there a chance one isnt counting some leap seconds and stuff like that. They occasionally add a couple extra seconds onto UTC to make it more accurate
    – Omer
    Aug 24, 2020 at 12:34

3 Answers 3

1

Let me just compile a full answer:

One is very very slighty off for some reason I think one counts a second as not 1 second but 0.99999999 ... seconds. Im not sure about a fix but for short term calculations it should be accurate enough (e.g) 14.9999999999999 and 15

For long term calculations a possible fix is using different libraries

// C++ program to find Current Day, Date 
// and Local Time 
#include<iostream> 
#include<ctime> 
using namespace std; 
int main() 
{ 
    // Declaring argument for time() 
    time_t tt; 
  
    // Declaring variable to store return value of 
    // localtime() 
    struct tm * ti; 
  
    // Applying time() 
    time (&tt); 
  
    // Using localtime() 
    ti = localtime(&tt); 
  
    cout << "Current Day, Date and Time is = " 
         << asctime(ti); 
  
  return 0; 
} 

number 2:

// CPP program to print current date and time 
// using time and ctime. 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
  
int main() 
{ 
    // declaring argument of time() 
    time_t my_time = time(NULL); 
  
    // ctime() used to give the present time 
    printf("%s", ctime(&my_time)); 
    return 0; 
} 

number 3:

// CPP program to print current date and time 
// using chronos. 
#include <chrono> 
#include <ctime> 
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
    // Here system_clock is wall clock time from 
    // the system-wide realtime clock 
    auto timenow = 
      chrono::system_clock::to_time_t(chrono::system_clock::now()); 
  
    cout << ctime(&timenow) << endl; 
} 

Another possible reason is: Leap Seconds

UTC is based on lots of atomic clocks in places all around the world. These clocks are more accurate then the rotation and movement of the earth so they sometimes add leap seconds or other various things from time to time.And as 1970 is a pretty dang while ago, there must have been a couple additions.

reference: https://www.geeksforgeeks.org/print-system-time-c-3-different-ways/

1

Try this:

auto lastCallTime = system_clock::now().time_since_epoch()/1.0s;

lastCallTime will have type long double and will contain the fractional seconds.

0

As Omer speculated, this indeed appears to be a leap seconds problem.

The root cause is that the real world uses leap seconds, but the POSIX idea of "seconds since 1-1-1970 does not. I'm intentionally writing that as 1-1-1970 and not the ISO notation of 1970-01-01, because ISO dates are real-world dates including leap seconds.

This will give you a difference of exactly +27 leap seconds between 1970 and 2020. The remaining 4 missing seconds are possibly due to an imprecise clock. Your PC might have more than one clock. C++ even acknowledges that; std::chrono::system_clock and std::chrono::high_resolution_clock co-exist, and may differ in value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy