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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:25:30+00:00 2026-05-17T01:25:30+00:00

Input : strings with date and optional time. Different representations would be nice but

  • 0

Input: strings with date and optional time. Different representations would be nice but necessary. The strings are user-supplied and can be malformed. Examples:

  • "2004-03-21 12:45:33" (I consider this the default layout)
  • "2004/03/21 12:45:33" (optional layout)
  • "23.09.2004 04:12:21" (german format, optional)
  • "2003-02-11" (time may be missing)

Needed Output: Seconds since Epoch (1970/01/01 00:00:00) or some other fixed point.

Bonus: Also, reading the UTC-offset of the local system time would be great.

The input is assumed to be a local time on the machine in question.
The output needs to be UTC. System is Linux only (Debian Lenny and Ubuntu needed).

I have tried to use boost/date_time, but must admit I can’t wrap my head around the documentation. The following works without the needed conversion from system local time to UTC:

std::string date = "2000-01-01";
boost::posix_time::ptime ptimedate = boost::posix_time::time_from_string(date);
ptimedate += boost::posix_time::hours(Hardcoded_UTC_Offset);// where to get from?
struct tm = boost::posix_time::to_tm(ptimedate);
int64_t ticks = mktime(&mTmTime);

I think boost::date_time can provide the needed UTC offset, but I wouldn’t know how.

  • 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-17T01:25:30+00:00Added an answer on May 17, 2026 at 1:25 am

    Although I don’t know how to format a single-digit month input in boost, I can do it after the two-digit edit:

    #include <iostream>
    #include <boost/date_time.hpp>
    namespace bt = boost::posix_time;
    const std::locale formats[] = {
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))};
    const size_t formats_n = sizeof(formats)/sizeof(formats[0]);
    
    std::time_t pt_to_time_t(const bt::ptime& pt)
    {
        bt::ptime timet_start(boost::gregorian::date(1970,1,1));
        bt::time_duration diff = pt - timet_start;
        return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
    
    }
    void seconds_from_epoch(const std::string& s)
    {
        bt::ptime pt;
        for(size_t i=0; i<formats_n; ++i)
        {
            std::istringstream is(s);
            is.imbue(formats[i]);
            is >> pt;
            if(pt != bt::ptime()) break;
        }
        std::cout << " ptime is " << pt << '\n';
        std::cout << " seconds from epoch are " << pt_to_time_t(pt) << '\n';
    }
    int main()
    {
        seconds_from_epoch("2004-03-21 12:45:33");
        seconds_from_epoch("2004/03/21 12:45:33");
        seconds_from_epoch("23.09.2004 04:12:21");
        seconds_from_epoch("2003-02-11");
    }
    

    note that the seconds-from-epoch output will be assuming the date was in UTC:

    ~ $ ./test | head -2
    ptime is 2004-Mar-21 12:45:33
    seconds from epoch are 1079873133
    ~ $ date -d @1079873133
    Sun Mar 21 07:45:33 EST 2004
    

    You could probably use boost::posix_time::c_time::localtime() from #include <boost/date_time/c_time.hpp> to get this conversion done assuming the input is in the current time zone, but it is rather inconsistent: for me, for example, the result will be different between today and next month, when daylight saving ends.

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

Sidebar

Related Questions

I've got a pointer to a string, (char *) as input. The date/time looks
I give possibility to user to input strings with unique keys that represent one-line
The user should input two strings and the script will performe a text search:
Okay, so ultimately I want a program where the user can input simple data
I have a website where I need to parse date/time strings from receipts. These
I'm trying to convert an input date string to a date format and then
I am using Datetime.TryParse method to check the valid datetime. the input date string
Hi I have the following date as String format. Input 2010-04-20 05:34:58.0 Output I
I am trying to get a date object from a string from an input.
I need to match input strings (URLs) against a large set (anywhere from 1k-250k)

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.