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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:11:19+00:00 2026-06-13T10:11:19+00:00

Bit of a c++ newbie so here we go; I have a method that

  • 0

Bit of a c++ newbie so here we go;

I have a method that is parsing a date/time, however that date/time is passed to me always with 00:00:00 as the hh:mm:ss. As such i want to add in the values of the current systime in place of those values. I have methods that do this, and the first method is returning the correct time in UTC format.

bool CTRHTranslationRTNS::ParseDateSysTime(const char* pszString, time_t& tValue)
{
    ASSERT(pszString != NULL);

    // DateTime fields.
    enum { YEAR, MONTH, DAY, HOURS, MINS, SECS, NUM_FIELDS };

    CStringArray astrFields;

    // Split the string into the date and time fields.
    int nFields = CStringParser::Split(pszString, "- :T", astrFields);

    // Not DD/MM/YYYY HH:MM:SS format.
    if (nFields != NUM_FIELDS)
        return false;

    int anFields[NUM_FIELDS] = { 0 };

    // Parse field numbers.
    for (int i = 0; i < NUM_FIELDS; ++i)
        anFields[i] = atoi(astrFields[i]);

    tm oTime = { 0 };

        //Add System Time instead
        time_t sysyemTimeNow;
        struct tm * ptm;
        time ( &sysyemTimeNow );
        ptm = gmtime ( &sysyemTimeNow );

    // Copy fields to time struct.
    oTime.tm_mday  = anFields[DAY];
    oTime.tm_mon   = anFields[MONTH] - 1;
    oTime.tm_year  = anFields[YEAR] - 1900;
    oTime.tm_hour  = ptm->tm_hour;
    oTime.tm_min   = ptm->tm_min;
    oTime.tm_sec   = ptm->tm_sec;
    oTime.tm_isdst = -1;

    // Convert to time_t.
    tValue = mktime(&oTime);

    // Invalid field values.
    if (tValue < 0)
        return false;

    return true;
}

In the second method I do some formatting on the date/time and this results in 2 hours being removed from the time.

string CTRHTranslationRTNS::ConvertDateSysTimeToDateInUTC(const string& bossDate)
{
    time_t dealDate;
    if (ParseDateSysTime(bossDate.c_str(), dealDate))
    {
        struct tm * ptm = gmtime(&dealDate);
        char buffer [80];
        strftime(buffer,80,"%Y-%m-%d %H:%M:%S",ptm);
        return string(buffer);
    }
    else
    {
        throw exception(string("Invalid date/SysTime value: ").append(bossDate).c_str());   
    }   
}

Just to be clear, the ParseDateSysTime method returns the time with the correct UTC value of 11:53, but as soon as

struct tm * ptm = gmtime(&dealDate);

is called the time changes to 08:53. It suggests this is a product of calling the gmtime() method but i am not sure.

Many Thanks

Graham

  • 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-13T10:11:21+00:00Added an answer on June 13, 2026 at 10:11 am

    The reson is the mktime() method used in the first function uses local time, but gmtime() uses UTC time.

    See http://www.cplusplus.com/reference/clibrary/ctime/mktime/ and http://www.cplusplus.com/reference/clibrary/ctime/gmtime/ for further explanation.

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

Sidebar

Related Questions

Bit of a newbie here. I'm currently working on a MySQL table that lists
enter code here i'm a bit newbie, so sorry for this question. i have
Here's a bit of a newbie question. I have a NSMutableDictionary *dict which in
Bit of a newbie question here. I have a form and one of it's
Sorry if the title sounds a bit vague. Stackoverflow posting newbie here. I have
a bit of a PHP / MySQL newbie here... I've been building a PHP-based
A bit of a newbie question on rails associations. I have a Bug model,
Ok bit of a newbie type question. I want to use Core Data, together
I'm a bit of a NuGet newbie and have come from the Maven world.
Bit of a JavaScript newbie here - I am firing this basic bit of

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.