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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:56:42+00:00 2026-05-28T17:56:42+00:00

The set_time() function is supposed to take a particular time and add the specified

  • 0

The set_time() function is supposed to take a particular time and add the specified number of minutes to this time and save it in *t.

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

enum {JAN, FEB, MAR, APR, MAY, JUNE, JULY, AUG, SEP, OCT, NOV, DEC};

void set_time(struct tm *t,
              int year, int mon, int day,
              int hour, int min, int sec,
              int mins_to_add)
{
    /* Copy all the values. */
    t->tm_year = (year - 1900);
    t->tm_mon = mon;
    t->tm_mday = day;
    t->tm_hour = hour;
    t->tm_min = min;
    t->tm_sec = sec;

    // No DST since we are storing UTC time.
    t->tm_isdst = 0;

    /* Add minutes to time. */
    t->tm_min += mins_to_add;
    mktime(t);

    /* Debug print */
    printf("%s\n", asctime(t));
}

int main(int argc, char **argv)
{
    struct tm t;
    set_time(&t, 2011, AUG, 1, 10, 00, 00, 0);
    return 0;
}

So, I ask set_time() to add nothing to Aug 01 10:00:00 2011. However, if my Windows system is set to EST time zone, the output I get is: Mon Aug 01 11:00:00 2011

The error happens because I do the task of adding the specified number of minutes (0 in the above example) to the specified time (Aug 01 10:00:00 2011 in the above example) with this part of the code:

    /* Add minutes to time. */
    t->tm_min += mins_to_add;
    mktime(t);

mktime() is called to adjust all other member variables of the structure in case t->tm_min exceeds 59 due to the addition. But mktime() treats the time in struct tm object as local time. So, when it sees that the date is Aug 01 2011 and local time zone as EST, it assumes that DST needs to be applied but it finds that t->tm_isdst is set to 0 (meaning DST isn’t applied to the time in *t). So, it applies DST, adjusts the date from Aug 01 10:00:00 2011 to Aug 01 11:00:00 2011 and sets t->tm_isdst to 1.

Had I initialized t->tm_isdst to 1, this issue wouldn’t have occurred because mktime() would have found that the time in *t already has DST applied to it. However, it would have messed up the result if I asked set_time() to add nothing to a date on which DST should not not applied, say Jan 01 10:00:00 2011. Now, it would try to turn off DST in *t thereby setting t->tm_isdst to 0 and re-adjusting the date to Jan 01 09:00:00 2011.

As a result, this has the undesirable effect of adding an extra hour to or subtracting an extra hour from the specified time when I wanted to add nothing to it. This happens because when mktime() finds that the DST setting in t->tm_isdst is not what it should be as per the local time-zone of the system, it tries to set it correctly by readjusting the time in *t.

What is the right way to solve this problem?

  • 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-05-28T17:56:43+00:00Added an answer on May 28, 2026 at 5:56 pm

    The Windows function _mkgmtime can be used instead of mktime order to perform this conversion safely (regardless of the local time zone settings).

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

Sidebar

Related Questions

I've written a function that's supposed to (1) generate a random number, (2) judge
I'm using the following function that changes a calendar selection at a set time
The Win32 GetTimeZoneInformation function returns your systems local time zone as set up in
I've written these 2 JQuery functions which are supposed to take 9 blocks with
I have this code I been working on but I'm having a hard time
I'm getting odd output from the getDate() function. It's supposed to return a date
When setting a service to go off at particular time, I use the AlarmManager
This is deriving from 'http://stackoverflow.com/questions/8369174/setting-a-time-for-flicker-animation-on-img' I'm unable to figure out how to set the
I have this piece of code that is supposed to work on Firefox 3.6
I have a function (say foo() )that will be called from time to time

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.