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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:37:09+00:00 2026-05-16T02:37:09+00:00

I’ve written a function to convert datetimes from one format to another – int

  • 0

I’ve written a function to convert datetimes from one format to another –


int convertDateTime(char* source_fmt,char* dest_fmt,char* source,char* dest)
{
        struct tm tmpptr;
        if (strptime(source,source_fmt,&tmpptr) == NULL)
        {
                strcpy(dest,"");
                return -1;
        }
        strftime(dest,100,dest_fmt,&tmpptr);
        return 0;
}

It works fine for most formats, But when I use format = “%y%j”, all I get is 10001; the julian day does not work.

I’m using gcc on solaris 10. Any idea what i need to change?

  • 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-16T02:37:10+00:00Added an answer on May 16, 2026 at 2:37 am

    Update
    The original answer (below) presumed that the “%y%j” format was used on the output (strftime) not the input (strptime). The mktime function will compute yday from valid info, but it doesn’t work the other way.

    If you want to decode something like that you need to do it manually. Some example code follows. It has had minimal testing and almost certainly has bugs. You may need to alter ir, depending in the input format you want.

    #include <string>
    #include <ctime>
    #include <cassert>
    #include <iostream>
    
    int GetCurrentYear()
    {
        time_t tNow(::time(NULL));
        struct tm tmBuff = *::localtime(&tNow);
        return tmBuff.tm_year;
    }
    bool IsLeapYear(int nYear)
    {
        if (0 == (nYear%1000)) return true;
        if (0 == (nYear%100))  return false;
        if (0 == (nYear%4))    return true;
        return false;
    }
    // nMonth = 0 (Jan) to 11 (Dec)
    int DaysPerMonth(int nMonth, bool bLeapYear)
    {
        //                 J   F   M   A   M   J   J   A   S   O   N   D
        int nDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        assert(nMonth>=0 && nMonth<12);
        int nRet = nDays[nMonth];
        if (bLeapYear && nMonth==1)
            nRet++;
        return nRet;
    }
    
    // sDate is in the format YYDDD where YY is the last 2 digits of the year
    // and YYY is the day of the year (1/1 = 1, 31/12 = 365 for non-leap year)
    bool DecodeDate(const std::string &sDate, struct tm &tmBuff)
    {
        if (sDate.length() != 5 ||
            !isdigit(sDate[0])  ||
            !isdigit(sDate[1])  ||
            !isdigit(sDate[2])  ||
            !isdigit(sDate[3])  ||
            !isdigit(sDate[4]))
        {
            return false;
        }
        ::memset(&tmBuff, 0, sizeof(struct tm));
        tmBuff.tm_year = GetCurrentYear();
        // drop last 2 digits
        tmBuff.tm_year -= tmBuff.tm_year%100; 
        // replace last 2 digits
        tmBuff.tm_year += ::atoi(sDate.substr(0, 2).c_str());    
    
        tmBuff.tm_yday = ::atoi(sDate.substr(2).c_str());
        int nDays(tmBuff.tm_yday);
        bool bLeapYear(IsLeapYear(1900 + tmBuff.tm_year));
        int nTmp = DaysPerMonth(0, bLeapYear);
        while (nTmp < nDays)
        {
            nDays -= nTmp;
            tmBuff.tm_mon++;
            nTmp = DaysPerMonth(tmBuff.tm_mon, bLeapYear);
        }
        tmBuff.tm_mday = nDays;
        ::mktime(&tmBuff);
        return true;
    }
    
    int main(int argc, char *argv[])
    {
        for (int i=1; i<argc; i++)
        {
            struct tm tmBuff;
            DecodeDate(argv[i], tmBuff);
            const size_t nSize(128);
            char szBuff[nSize];
            strftime(szBuff, nSize, "%A, %d %B %Y", &tmBuff);
            std::cout << argv[i] << '\t' << szBuff << std::endl;
        }
        return 0;
    }
    

    ================================================

    C:\Dvl\Tmp>Test.exe 07123 08123 08124
    07123   Thursday, 03 May 2007
    08123   Friday, 02 May 2008
    08124   Saturday, 03 May 2008
    

    End Update

    After you call strptime(), call mktime() which will populate any missing members of the struct. Also, you should zero out the struct before beginning.

    #include <string>
    #include <ctime>
    
    int convertDateTime(const std::string &sSourceFmt, 
                        const std::string &sDestFmt,
                        const std::string &sSource,
                        std::string &sDest)
    {
        struct tm tmbuff = { 0 };
    
        if (::strptime(sSource.c_str(), sSourceFmt.c_str(), &tmbuff) != NULL)
        {
            ::mktime(&tmbuff);
            const size_t nSize(256);
            char szBuff[nSize+1] = "";
    
            if (::strftime(szBuff, nSize, sDestFmt.c_str(), &tmbuff))
            {
                sDest = szBuff;
                return 0;
            }
        }
        sDest.clear();
        return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 484k
  • Answers 484k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of trying to reinvent the wheel, you might want… May 16, 2026 at 7:28 am
  • Editorial Team
    Editorial Team added an answer Well, it works for me: print read to-url "http://www.earnforex.com/blog/2010/08/forex-technical-analysis-for-week-0809%E2%80%940813/" May 16, 2026 at 7:28 am
  • Editorial Team
    Editorial Team added an answer I'm still experimenting with different MVP approaches myself, but the… May 16, 2026 at 7:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.