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 8622319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:01:27+00:00 2026-06-12T07:01:27+00:00

What does time(NULL) return? Given documentation I had assumed it returns the number of

  • 0

What does time(NULL) return? Given documentation I had assumed it returns the number of seconds since epoch, however, when I run the following code:

#include <iostream>
#include "Time.h"
#include "Math.h"

int main () {

    double curT = 0;
    double curJ = 0;

    time_t curTtimeT = 0;
    double curJtimeT = 0;

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++) {

        //get the current time
        curT = time(NULL);
        curTtimeT = time(NULL);

        //check if current time equals previous time
        if (time(NULL) != curT) {
            //output time and approx. "time" through iteration
            std::cout << "Time match with doubles:\t" << curT << "\tdeltaJ:\t\t\t" << (j - curJ) <<std::endl;
            curT = time(NULL);
            curJ = j;
        }

        if (time(NULL) != curTtimeT) {
            //output time and approx. "time" through iteration
            std::cout << "Time match with time_t:\t\t" << curTtimeT << "\t\tdeltaJtimeT:\t\t" << (j - curJtimeT) <<std::endl;
            curTtimeT = time(NULL);
            curJtimeT = j;
        }

    }
    return 0;
}

I get the following results (the skipping does not appear to have a pattern between runs):

Time match with time_t:     1348873002      deltaJtimeT:        290842
Time match with doubles:    1.34887e+09 deltaJ:         2.41017e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08409e+06
Time match with doubles:    1.34887e+09 deltaJ:         2.16587e+06
Time match with time_t:     1348873007      deltaJtimeT:        5.36928e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08696e+06
Time match with time_t:     1348873008      deltaJtimeT:        1.08696e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08534e+06
Time match with doubles:    1.34887e+09 deltaJ:         3.18296e+06
...
Time match with time_t:     1348873122      deltaJtimeT:        2.16217e+06
Time match with doubles:    1.34887e+09 deltaJ:         6.42553e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08727e+06
Time match with doubles:    1.34887e+09 deltaJ:         2.14147e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.04733e+06
Time match with time_t:     1348873130      deltaJtimeT:        8.42965e+06

Note: I am using the deltaJ output to show a bit more numerically what is a clear visual difference in terms of when the console actually outputs results from this loop.

Clearly, this does not seem to consistently be returning the number of seconds since epoch, as I would instead be able to see a very consistent output of very close to 1 line for both double and time_t per second. Instead, I miss the entire range between 1348873002 and 1348873007 – none of these values appear to be found via time(NULL). These gaps appear consistently throughout the entire runtime of this loop.

Additionally, sometimes it does not even output for some of the seconds (see the gap between 1348873122 and 1348873130).

I don’t understand why the output doesn’t show one entry for both variable types, for each second of real time. Instead, it seems time(NULL) inconsistently returns the number of seconds since epoch, and actually skips some values.

What am I missing?

I am running these tests on a 2.4GHz dualcore Macbook Pro, in X-Code 3.2.5, on Snow Leopard (perhaps this issue is specific to my system)?

  • 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-06-12T07:01:28+00:00Added an answer on June 12, 2026 at 7:01 am

    Clearly, this does not seem to consistently be returning the number of seconds since epoch, I would instead be able to see a very consistent output of very close to 1 line for both double and time_t per second.

    This doesn’t follow. I think you intended for your code to be something a bit different than it actually is.

    Specifically, I think you need to change this:

        //run indefinitely... use a debugger you can stop :-)
        for (double j = 0; j < 2000000000; j++) {
    
            //get the current time
            curT = time(NULL);
            curTtimeT = time(NULL);
    

    to this:

        //get the current time
        curT = time(NULL);
        curTtimeT = time(NULL);
    
        //run indefinitely... use a debugger you can stop :-)
        for (double j = 0; j < 2000000000; j++) {
    

    because as it is, you have a sort of race condition: you’re testing if the return-value of time changes during certain portions of the loop. Obviously that won’t happen 100% of the time; sometimes the second will roll over during one part of the loop, and other times it will roll over during a different part.

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

Sidebar

Related Questions

Why does every time I do mvn jetty:run, maven tries to download some dependencies
I've always been curious, why does the time(time_t *) function both return a time_t
Does File.list() have a longer run time if there are more files in its
The following code does not time out in Ruby 1.9.3p194 (2012-04-20) [i386-mingw32]: require 'timeout'
I'd like a response from someone who actually does real-time programming in C# or
I'm thinking about this question for a time: when does an ARM7(with 3 pipelines)
Does System.currentTimeMillis() represent UTC time or local system time?
Does anyone know of a way to detect when the last time a Microsoft
Does anyone know of a List implementation that has a constant time get(int index)
If I come across old code that does if (!this) return; in an app,

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.