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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:53+00:00 2026-06-18T12:08:53+00:00

ctime() function is supposed to give the time in string format since Epoch for

  • 0

ctime() function is supposed to give the time in string format since Epoch for the seconds passed in. This is my code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(int argc, int **argv)
{
    time_t tmp=86400; // Seconds for one day
    char *s;
    s = ctime(&tmp);
    if(!s) perror("ctime");
    else printf("%s", s);
    return 0;
}

The program gives correct output on my Ubuntu 12.04, 32 bit machine:

Fri Jan  2 01:00:00 1970

But the same code gives wrong output on my Scientific Linux, 64-bit machine:

Thu Jan  1 16:00:00 1970

Apparently, according to this machine Epoch is:

Wed Dec 31 16:00:00 1969

Any idea why Scientific Linux 6 is giving wrong output?!

  • 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-18T12:08:54+00:00Added an answer on June 18, 2026 at 12:08 pm

    A time_t value always [1] represents the number of seconds since the epoch, which is 1970-01-01 00:00:00 UTC — plus or minus whatever handwaving is required to acknowledge that we’re probably ignoring leap seconds. So (time_t)86400 represents a single moment in time, regardless of your current time zone.

    The ctime() function returns a pointer to an awkwardly formatted[2] string expressed in local time. I’m 8 hours west of UTC, so on my system your program’s output is:

    Thu Jan  1 16:00:00 1970
    

    (the epoch plus 24 hours from your 86400, minus 8 hours for my time zone).

    A system’s idea of the current time zone can be determined in any of several ways. On my Ubuntu system, /etc/timezone contains the line America/Los_Angeles. On CentOS, which should be similar to Scientific Linux, /etc/localtime is a binary timezone data file.

    On both systems, the system default time zone can be overridden by setting the $TZ environment variable, with UTC or the empty string denoting UTC. If I set $TZ to either UTC or "", your program’s output is:

    Fri Jan  2 00:00:00 1970
    

    The epoch is always the same moment in history, but it can be expressed differently. For example (using the GNU Coreutils date command, showing the epoch itself rather than 24 hours after the epoch as your program does):

    $ date -d @0
    Wed Dec 31 16:00:00 PST 1969
    $ date -u -d @0
    Thu Jan  1 00:00:00 UTC 1970
    $ TZ=UTC date -d @0
    Thu Jan  1 00:00:00 UTC 1970
    $ TZ= date -d @0
    Thu Jan  1 00:00:00 UTC 1970
    $ 
    

    Note that if you’re in the UK, your local time matches UTC only part of the year.

    [1] Well, nearly always. POSIX guarantees that a time_t value represents seconds since the epoch, but the C standard says only that it’s an arithmetic type capable of representing times. And you can store any value you like in a time_t object, and have it mean whatever you like, but all the standard C and POSIX functions that deal with time_t values treat them as seconds since the epoch.

    [2] The format produced by ctime() and asctime() is rather awkward, for historical reasons. It’s of the form Sun Sep 16 01:03:52 1973\n\0. Note the lack of any time zone information; also the trailing '\n' can be confusing (I’ve seen extraneous blank lines in log files because of it). The strftime() function gives you a lot more flexibility. I recommend using ISO 8601 format wherever possible.

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

Sidebar

Related Questions

According to this documentation http://www.cplusplus.com/reference/clibrary/ctime/time/ for time(NULL) If the function could not retrieve the
#include <ctime> #include <cstdio> #include <sys/time.h> #include <iostream> using namespace std; int main() {
I have a question about the following code : #include <iostream> #include <ctime> int
According to http://www.cplusplus.com/reference/clibrary/ctime/time_t/ time_t is the number of seconds since midnight 1/1/1970 UTC. So
I write a function which sets system time in linux. I write next code:
The man page for ctime says that POSIX.1-2008 marks this function (among some other
I'm testing the following code: #include <iostream> #include <vector> #include <algorithm> #include <ctime> int
the prototype of the function ctime is char *ctime(const time_t *timep); As we can
This is my 'tick' function: - (void) tick: (ccTime) dt { NSLog(@%d,ticker); if(fbut.Adown ==
Is there an easy beginner way to take the current time using <ctime> to

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.