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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:47:58+00:00 2026-05-16T03:47:58+00:00

I am writing a C++ app that has to parse a date/time string and

  • 0

I am writing a C++ app that has to parse a date/time string and give out epoch time.
But the format of the date/time string can be more than one (actually say 25 formats) like

    "EEE, dd MMM yyyy HH:mm:ss '('ZZZ')'",
    "EEE, dd MMM yyyy HH:mm:ss '\"'ZZZ'\"'",
    "EEE, dd MMM yyyy hh:mm:ss z",
    "EEE, dd MMM yyyy HH:mm Z",
    "EEE, dd MMM yyyy HH:mm:ss",
    "EEE, MMM dd, yyyy 'at' HH:mm:ss z",
    "EEE M/dd/yyyy hh:mm:ss a",
    "EEE MMM dd HH:mm:ss z yyyy",
    "EEE MMM dd hh:mm:ss yyyy",
    "EEEE, MMMM dd, yyyy hh:mm:ss a",
    "EEEE, MMMM dd, yyyy HH:mm a",
    "EEEE, MMMM dd, yyyy HH:mm",
    "MMM dd, yyyy hh:mm:ss a",
    "dd MMM yyyy hh:mm:ss z",
    "dd-MMM-yyyy HH:mm:ss z",
    "dd MMM yy HH:mm:ss",
    "MM/dd/yyyy  hh:mm a  (EEEE)",
    "MM/dd/yyyy hh:mm a (EEEE)",
    "MM/dd/yyyy hh:mm:ss",
    "MM/dd/yyyy hh:mm a Z",
    "MM/dd/yyyy hh:mma Z",
    "MM/dd/yyyy hh:mma",
    "MM/dd/yyyy hh:mm a",
    "MM/dd/yyyy hh:mm Z",
    "MM/dd/yy hh:mm a Z",
    "MM/dd/yy hh:mma Z",
    "MM/dd/yy HH:mm a",
    "MM/dd/yy HH:mm Z",
    "MM/dd/yyyy",   
    "yyyy-MM-dd HH:mm:ss",
    "yyyyMMddhhmmss",
    "yyyyMMddhhmm",
    "yyyyMMdd"

Now, i need to take the string, figure out it belongs to which of these formats, then get the time in epoch.

Can you suggest a way to do this. Code samples will be really helpful.
I am banking on Boost libraries. Let me know if this can be achieved by boost date/time parsing libraries.

Thanks in advance,
AJ

  • 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-16T03:47:59+00:00Added an answer on May 16, 2026 at 3:47 am

    I suppose you could attempt to convert your string into ptime using each of these formats and pick the ones that do not result in a not_a_date_time.:

    The boost format flags are slightly different from yours, I’ll do just the last five for this example:

    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    using boost::posix_time::time_input_facet;
    using std::locale;
    const locale inputs[] = {
        locale(locale::classic(), new time_input_facet("%m/%d/%Y")),
        locale(locale::classic(), new time_input_facet("%Y-%m-%d %H:%M:%S")),
        locale(locale::classic(), new time_input_facet("%Y%m%d%H%M%S")),
        locale(locale::classic(), new time_input_facet("%Y%m%d%H%M")),
        locale(locale::classic(), new time_input_facet("%Y%m%d")) };
    const size_t formats = sizeof(inputs)/sizeof(inputs[0]);
    
    time_t ptime_to_time_t(boost::posix_time::ptime t)
    {
           static boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
           return (t-epoch).ticks() / boost::posix_time::time_duration::ticks_per_second();
    }
    int main()
    {
           std::string msg = "2010-08-04 08:34:12";
    
           for(size_t i=0; i<formats; ++i)
           {
               std::istringstream ss(msg);
               ss.imbue(inputs[i]);
               boost::posix_time::ptime this_time;
               ss >> this_time;
    
               if(this_time != boost::posix_time::not_a_date_time)
                   std::cout << this_time << " or " << ptime_to_time_t(this_time) << std::endl;
           }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.