C++ Parsing TimeZone From Date String

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 121
Joined: Wed Oct 28, 2015 9:48 pm

C++ Parsing TimeZone From Date String

Post by Everydaydiesel » Sun Apr 19, 2020 6:46 am

Hello I realize this is not wxWidgets specific but it is C++ and you guys are a lot smarter then I am.

I have the following date that I am consuming from an api.

Code: Select all

string sToParse = "2020-04-17T09:30:00-04:00";
which should be in human form "Friday, April 17, 2020 08:30:00" central time or epoch of 1587130200

however this code

Code: Select all

cout << "raw: " << sToParse << endl;
static const std::string dateTimeFormat { "%Y-%m-%dT%H:%M:%S%Z" };
istringstream ss{ sToParse };
tm dt;
ss >> get_time(&dt, dateTimeFormat.c_str());
cout << mktime(&dt) << endl;

Gives me an epoch of 1587137400 which is a human format of "Friday, April 17, 2020 10:30:00 AM" which is two hours difference. How do i get the %Z to process the timezone appropriately?

Thanks in advance for any help you can give

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4170
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: C++ Parsing TimeZone From Date String

Post by ONEEYEMAN » Sun Apr 19, 2020 5:00 pm

Hi,
Which version of wxWidgets?
Which platform/toolkit version?
Did you set the appropriate locale?

Thank you.

maximand
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Nov 11, 2011 5:44 pm
Location: Russia

Re: C++ Parsing TimeZone From Date String

Post by maximand » Mon Apr 20, 2020 8:31 pm

75% complete solution.
The 'test' variable contains '-04:00'. It should be converted to wxTimeSpan. I have no ideas how.

Code: Select all

            wxRegEx pattern(R"(^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(-?[0-9]{2}:[0-9]{2})$)");
            wxString published_at = "2020-04-17T09:30:00-04:00";
            if (pattern.Matches(published_at)) {
                wxDateTime pub_date;
                wxString::const_iterator end;
                pub_date.ParseFormat(pattern.GetMatch(published_at, 1), "%Y-%m-%dT%H:%M:%S", &end);

                auto test = pattern.GetMatch(published_at, 2);
                //ts.ParseTime("%H:%M");
                //auto test2 = ts.FormatISOTime();
                wxString pd = pub_date.Format("#w, #m %d, %Y %H:%M:%S"); //"Friday, April 17, 2020 10:30:00 AM"
                pd.Replace("#w", wxDate::GetWeekDayName(pub_date.GetWeekDay()));
                pd.Replace("#m", wxDate::GetMonthName(pub_date.GetMonth()));
            }
Sorry, there seems to be a misunderstanding here. It's about the time zone, not the date formatting.
M$, VS2017, C++

Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 121
Joined: Wed Oct 28, 2015 9:48 pm

Re: C++ Parsing TimeZone From Date String

Post by Everydaydiesel » Thu Apr 23, 2020 4:36 am

This value is returned from the api "2020-04-17T09:30:00-04:00" which is EST time zone
and I need to convert it to CST but I am unsure really how to do this with built in functions of C++ and/or wxwidgets

There is some weird timezone / daylight savings time calculations that apparently makes it 1 hour difference but sometimes it is 2 hours difference. I dont really understand EPOCH dates that well to be honest which is why I am asking for help

User avatar
doublemax
Moderator
Moderator
Posts: 14893
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: C++ Parsing TimeZone From Date String

Post by doublemax » Thu Apr 23, 2020 10:51 am

I currently don't have the opportunity to play around with code, but i see a few issues/inconsistencies:

Code: Select all

"2020-04-17T09:30:00-04:00"
"-04:00" is not a valid time offset, should be "-0400"

Code: Select all

"%Y-%m-%dT%H:%M:%S%Z"
To parse time zone in number format, use "%z", not "%Z" (which is for time zone names)

Reference for both issues:
https://en.cppreference.com/w/cpp/chrono/c/strftime
which should be in human form "Friday, April 17, 2020 08:30:00" central time or epoch of 1587130200
When i enter "1587130200" at https://www.epochconverter.com i get a different result: "GMT: Friday, April 17, 2020 1:30:00 PM"
Use the source, Luke!

Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 121
Joined: Wed Oct 28, 2015 9:48 pm

Re: C++ Parsing TimeZone From Date String

Post by Everydaydiesel » Thu Apr 23, 2020 7:09 pm

Hello doublemax, You have helped me so much over the years and I thank you!

The api returns the data "2020-04-17T09:30:00-04:00" but i could strip the extra : out of what it returns (I didnt write the api, I only invoke/consume)

I will be the first to admit, I have alot to learn when its comes to EPOCH, timezones and c++

You are right that entering 1587130200 gives me these results
Site: https://www.epochconverter.com
Input: 1587130200
Output: GMT: Friday, April 17, 2020 1:30:00 PM

If i enter that GMT into this this site ( to convert GMT to EST)
Site: https://savvytime.com/converter/gmt-to-est
Input: GMT: Friday, April 17, 2020 1:30:00 PM
Output: EST: Friday, April 17, 2020 9:30:00 AM

My time zone is CST so I use this
Site: https://savvytime.com/converter/est-to-cst
Input: EST: Friday, April 17, 2020 9:30:00 AM
Output: CST: Friday, April 17, 2020 8:30:00 AM

In this example 8:30AM is the correct time I am looking for.

Basically i can summarize
1. The api returns EST timezone
2. I need CST timezone
3. Its not clear if daylight savings times affect these conversions or not

User avatar
doublemax
Moderator
Moderator
Posts: 14893
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: C++ Parsing TimeZone From Date String

Post by doublemax » Thu Apr 23, 2020 7:54 pm

What exactly is your goal? Do you want a wxDateTime object with the correctly parsed time? Or do you need to convert it to Unix time?
Use the source, Luke!

Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 121
Joined: Wed Oct 28, 2015 9:48 pm

Re: C++ Parsing TimeZone From Date String

Post by Everydaydiesel » Thu Apr 23, 2020 7:57 pm

The api that I invoke returns a date in this similar format "2020-04-17T09:30:00-04:00" and I would like to put that into a unix/epoch form (in my time zone)

User avatar
doublemax
Moderator
Moderator
Posts: 14893
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: C++ Parsing TimeZone From Date String

Post by doublemax » Thu Apr 23, 2020 8:38 pm

I would like to put that into a unix/epoch form (in my time zone)
Unix time is always UTC/GMT.

Code: Select all

wxString sToParse = "2020-04-17T09:30:00-0400";
wxDateTime dt;
dt.ParseFormat(sToParse, "%Y-%m-%dT%H:%M:%S%z");
wxLogMessage("unix: %ld", (dt.GetValue() / 1000).ToLong() );
This shows 1587130200
Use the source, Luke!

Post Reply