Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::get_time and strptime for the truly unfortunate. (Closes #9) #11

Merged
merged 12 commits into from
Jan 7, 2017
Merged
2 changes: 1 addition & 1 deletion src/examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int example2() {
load_time_zone("America/Los_Angeles", &lax);
std::chrono::system_clock::time_point tp;

const bool ok = cctz::parse("%Y-%m-%d %H:%M:%S%p", civil_string, lax, &tp);
const bool ok = cctz::parse("%Y-%m-%d %H:%M:%S", civil_string, lax, &tp);

if (!ok) return -1;

Expand Down
27 changes: 17 additions & 10 deletions src/time_zone_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#define __MINGW64__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one probably needs to go eventually.


#if !defined(HAS_STRPTIME)
# if !( defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) )
# define HAS_STRPTIME 1 // assume everyone has strptime() except windows
Expand All @@ -28,18 +30,23 @@
#include <ctime>
#include <limits>
#include <vector>

#if !HAS_STRPTIME
#include <iomanip>
#include <sstream>
#if defined(__MINGW32__) || defined(__MINGW64__)
#include <get_time.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the include conditional on being compiled with MinGW? Just to minimise side-effects...

#endif
#endif

namespace RcppCCTZ {

namespace cctz {
namespace detail {
#if !HAS_STRPTIME

// Build a strptime() using C++11's std::get_time().
inline char* strptime(const char* s, const char* fmt, std::tm* tm) {
inline char* strptime(const char* s, const char* fmt, std::tm* tm)
{
std::cout << "in my strptime." << std::endl;
std::istringstream input(s);

#if defined(__MINGW32__) || defined(__MINGW64__)
Expand All @@ -56,16 +63,16 @@ inline char* strptime(const char* s, const char* fmt, std::tm* tm) {
return const_cast<char*>(s) + strlen(s);
}
}

#else
inline char* strptime(const char* s, const char* fmt, std::tm* tm) {

inline char* inline char* strptime(const char* s, const char* fmt, std::tm* tm)
{
::strptime(s, fmt, tm);
}
#endif
}


namespace cctz {
namespace detail {
#endif
#undef __MINGW64__

namespace {
std::tm ToTM(const time_zone::absolute_lookup& al) {
Expand Down Expand Up @@ -516,7 +523,7 @@ const char* ParseSubSeconds(const char* dp,
// Parses a string into a std::tm using strptime(3).
const char* ParseTM(const char* dp, const char* fmt, std::tm* tm) {
if (dp != nullptr) {
dp = RcppCCTZ::strptime(dp, fmt, tm);
dp = cctz::detail::strptime(dp, fmt, tm);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a better namespace -- +1

}
return dp;
}
Expand Down