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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:00:25+00:00 2026-06-08T01:00:25+00:00

This small program reproduces the bug in my project. time_t variable is converted to

  • 0

This small program reproduces the bug in my project. time_t variable is converted to struct_tm, then to string, and serialized to file. Later, this string is loaded from the file, and should be converted back to time_t. Resulting time_t differs from original by one hour (possibly because of daylight saving time). I can change only deserialization part, because file format should remain unchanged.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>

using namespace std;

string FormatDateTime(struct tm* time);
void Test();


int _tmain(int argc, _TCHAR* argv[])
{
    Test();
    return 0;
}


string FormatDateTime(struct tm* time)
{
    static const char* months[] =
    {
        "Jan", "Feb",  "Mar", "Apr", "May", "Jun",  "Jul", "Aug",  "Sep", "Oct",  "Nov", "Dec"
    };

    char s[30];
    const char* month;

    if ( time->tm_mon >= 0  &&  time->tm_mon < 12 )
    {
        month = months[time->tm_mon];
    }
    else
    {
        month = "??";
    }

    sprintf(s, "%d-%s-%02d %02d:%02d:%02d",
        time->tm_year + 1900,
        month,
        time->tm_mday,
        time->tm_hour,
        time->tm_min,
        time->tm_sec);

    return s;
}

void Test()
{
    // time_t variable is initialized with current time
    time_t t = time(NULL);

    // time_t variable is converted to struct tm and then to string
    struct tm* ptm = localtime(&t);

    char buffer[100];

    sprintf(buffer, "%d %d %d %d %d %d",
        ptm->tm_mday, ptm->tm_mon + 1, ptm->tm_year + 1900, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

    cout << buffer << endl;         // string is OK


    // string is saved to the file

    // *********************************************************************************************

    // later this string is restored from the file

    // **** I can change something only after this line **** //

    struct tm stm;
    memset(&stm, 0, sizeof(stm));

    sscanf(buffer, "%d %d %d %d %d %d",
        &stm.tm_mday, &stm.tm_mon, &stm.tm_year, &stm.tm_hour, &stm.tm_min, &stm.tm_sec);

    stm.tm_mon -= 1;
    stm.tm_year -= 1900;

    string s = FormatDateTime(ptm);

    cout << s << endl;              // OK

    // *********************************************************************************************
    // Now I need to convert struct tm to time_t and keep it in the variable

    time_t t1 = mktime(&stm);       // not the same time - 1 hour difference

    if ( t1 == t )
    {
        cout << "t1 == t" << endl;
    }
    else
    {
        cout << "t1 != t   !!!!!" << endl;
    }

    // time_t is printed - result is incorrect

    // Utilities::FormatDateTime
    struct tm* ptm1 = localtime(&t1);

    s = FormatDateTime(ptm1);

    cout << s << endl;
}

Local time is 11.33. Result:

19 7 2012 11 33 17
2012-Jul-19 11:33:17
t1 != t   !!!!!
2012-Jul-19 12:33:17

How can I change the last part of this program to get correct result?

  • 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-08T01:00:26+00:00Added an answer on June 8, 2026 at 1:00 am

    my guess is that tm_isdst is different in your both tm structs. In one case its 0 and in the other 1.

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

Sidebar

Related Questions

this is a small program using Java Scanner which reads a file of double
I'm running this small C# test program launched from a pre-commit batch file private
Upon compiling and running this small program to reverse a string, I get a
I have this small program and it needs to create a small .txt file
I got this code inside a small program to read a file: #ifdef WIN32
I've created a small program wich can read a .txt file. This file contains
I wrote this small C program: #include <stdio.h> #include <stdlib.h> int main() { FILE
I have this small program: #include <stdio.h> #include <string.h> #include <ctype.h> #define SIZE 30
Consider this small program: #include <stdio.h> #include <stdlib.h> // Change 60000 to 70000 and
How can I include foo() function of foo.c in this small program (sorry for

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.