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.

  • DId you try this: https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c – Omer Aug 24 '20 at 12:25
  • 1
    @Omer yes it's not returning that close python func either :/ –  Aug 24 '20 at 12:26
  • [I got almost same values from them](https://wandbox.org/permlink/qEZQKfXt7odQP1f6). What is your environment? How are you executing the programs? – MikeCAT Aug 24 '20 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 '20 at 12:29
  • `1598271246` is not that different from `1598271273`, 23 seconds. Are you worried about the fractional part? – 463035818_is_not_an_ai Aug 24 '20 at 12:29
  • @idclev463035818 yeap, for the function I need the fractional part is also important –  Aug 24 '20 at 12:31
  • @MikeCAT I am running on ubuntu 20.04 –  Aug 24 '20 at 12:34
  • 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 '20 at 12:34
  • Try changing the time from 1970 to like 2019 and try again for both. What do you get? Are they the same now? @full_steak_developer – Omer Aug 24 '20 at 12:36
  • @Omer I am giving a try now –  Aug 24 '20 at 12:37
  • @full_steak_developer what are the results? Did it work? – Omer Aug 24 '20 at 12:44
  • @Omer it's still small difference 14.999983310699463 vs 15 –  Aug 24 '20 at 12:58
  • Yea 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 @full_steak_developer – Omer Aug 24 '20 at 13:02
  • @Omer: That sort of sloppy thinking got 28 soldiers killed in the Gulf war, when a Patriot missile used floating point time. – MSalters Aug 24 '20 at 15:50
  • @MSalters Yeah thats very sad. I similar case happened with nasa and this european agency, because they used different units (imperial and metric) some mix ups happened and the **mars climate orbiter** failed. – Omer Aug 24 '20 at 16:15

3 Answers3

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/

Omer
  • 88
  • 1
  • 13
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.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
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.

MSalters
  • 173,980
  • 10
  • 155
  • 350