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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:47:31+00:00 2026-06-06T21:47:31+00:00

While trying to write code which returns 24 hours less than a given time,

  • 0

While trying to write code which returns 24 hours less than a given time, mktime()shows inconsistent output. I calculate it similar to this: current_time(GMT) - 86400 which should return the right value. All we need to do is calculate based on the input time; we used mktime() to change the time and get the GMT time and then do the regular calculation. I included my code below.

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

int main()
{
    time_t currentTime, tempTime;
    struct tm *localTime;

    time(&currentTime);
    //localTime = localtime(&currentTime);
    localTime = gmtime(&currentTime); //get the time in GMT as we are in PDT

    printf("Time %2d:%02d\n", (localTime->tm_hour)%24, localTime->tm_min);
    localTime->tm_hour = 19; // Set the time to 19:00 GMT
    localTime->tm_min = 0;
    localTime->tm_sec = 0;
    tempTime = mktime(localTime);
    //tempTime = mktime(localTime) - timezone;

    printf("Current time is %ld and day before time is %ld\n", currentTime, (currentTime - 86400));
    printf("Current timezone is %ld \n", timezone);

    printf("New time is %ld and day before time is %ld\n",tempTime, (tempTime - 86400));
}

But when we check the output it is coming back incorrect after the call to call mktime(). Below is the output of above program.

$ ./a.out
Time 11:51
Current time is 1341229916 and day before time is 1341143516
New time is 1341284400 and day before time is 1341198000
$ ./print_gmt 1341229916
Mon Jul  2 11:51:56 2012
$ ./print_gmt 1341143516
Sun Jul  1 11:51:56 2012
$ ./print_gmt 1341284400
Tue Jul  3 03:00:00 2012
$ ./print_gmt 1341198000
Mon Jul  2 03:00:00 2012
$ date
Mon Jul  2 04:52:46 PDT 2012

Now if we un-comment the line which subtracts the timezone (present in time.h), then the output is as expected. Below is the value for timezone in above program

$ ./a.out
. . .
Current timezone is 28800
. . .

So why is there such inconsistent behavior for mktime() although the man pages do not mention such adjustment of the timezone.
Is there something we are missing while doing such conversions?

Thanks in advance.

  • 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-06T21:47:33+00:00Added an answer on June 6, 2026 at 9:47 pm

    I think the problem is here:

    The mktime() function shall convert the broken-down time, expressed as local time, in the structure pointed to by timeptr, into a time since the Epoch value...

    Note the words local time. The C standard has them too in the description of mktime():

    The mktime function converts the broken-down time, expressed as local time, in the
    structure pointed to by timeptr into a calendar time value with the same encoding as
    that of the values returned by the time function.

    gmtime(), on the other hand, produces time in GMT/UTC and not in your time zone:

    The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).

    EDIT: If you just want 19:00 of the previous GMT/UTC day, you can do this:

    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        time_t currentTime;
        struct tm *brokenDownTime;
    
        time(&currentTime);
    
        // get the time in GMT as we are in PDT
        brokenDownTime = gmtime(&currentTime);
        printf("Current Time (GMT): %2d:%02d\n"
               "  seconds since Epoch: %ld\n", 
               brokenDownTime->tm_hour,
               brokenDownTime->tm_min,
               (long)currentTime);
    
        // "Unwind" time to 0:00:00 (assuming time_t is an integer):
        currentTime /= 24 * (time_t)3600;
        currentTime *= 24 * (time_t)3600;
    
        brokenDownTime = gmtime(&currentTime);
        printf("Time at the beginning of the current GMT day: %2d:%02d\n"
               "  seconds since Epoch: %ld\n", 
               brokenDownTime->tm_hour,
               brokenDownTime->tm_min,
               (long)currentTime);
    
        // Add 19 hours:
        currentTime += 19 * (time_t)3600;
    
        brokenDownTime = gmtime(&currentTime);
        printf("Time at 19:00:00 of the current GMT day: %2d:%02d\n"
               "  seconds since Epoch: %ld\n", 
               brokenDownTime->tm_hour,
               brokenDownTime->tm_min,
               (long)currentTime);
    
        // Subtract 1 day:
        currentTime -= 24 * (time_t)3600;
    
        brokenDownTime = gmtime(&currentTime);
        printf("Time at 19:00:00 of the previous GMT day: %2d:%02d\n"
               "  seconds since Epoch: %ld\n", 
               brokenDownTime->tm_hour,
               brokenDownTime->tm_min,
               (long)currentTime);
    
        return 0;
    }
    

    Output:

    Current Time (GMT): 13:23
      seconds since Epoch: 1341235429
    Time at the beginning of the current GMT day:  0:00
      seconds since Epoch: 1341187200
    Time at 19:00:00 of the current GMT day: 19:00
      seconds since Epoch: 1341255600
    Time at 19:00:00 of the previous GMT day: 19:00
      seconds since Epoch: 1341169200
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I'm trying to write a LRU Cache. I
I am trying to write a program wit Perl which should returns the frequency
Today, while I was trying to write code to just add and subtract the
I'm trying to write a simple C code for the following function: Given an
While trying to write an answer for another SO question something really peculiar happened.
I'm trying to write for and while loops in Python — functional programming style.
I'm trying to write some html in the Html tab of TinyMCE while editing
I am trying to write a program for evaluating postfix-expression code: #include <iostream> #include
I am trying to open an output file which I am sure has a
Here I am trying to write a function that returns me value based on

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.