-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from jpmorganchase/date-parser
C++ Date Validation
- Loading branch information
Showing
4 changed files
with
247 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2019, the Perspective Authors. | ||
* | ||
* This file is part of the Perspective library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
|
||
#include <sstream> | ||
#include <perspective/first.h> | ||
#include <perspective/date_parser.h> | ||
#include <locale> | ||
#include <iomanip> | ||
|
||
namespace perspective { | ||
|
||
// Milliseconds & timezones are not currently handled | ||
const std::string t_date_parser::VALID_FORMATS[12] = { | ||
"%Y%m%dT%H%M%S", // ISO "%Y%m%dT%H%M%S%F%q" | ||
"%Y-%m-%dT%H:%M:%S", | ||
"%Y-%m-%d %H:%M:%S", // ISO extended | ||
"%A, %d %b %Y %H:%M:%S", // RFC 0822 | ||
"%Y-%m-%d\\%H:%M:%S" | ||
"%m-%d-%Y", | ||
"%m/%d/%Y", | ||
"%m-%d-%Y", | ||
"%m %d %Y", | ||
"%m/%d/%Y", | ||
"%m/%d/%y", | ||
"%d %m %Y" | ||
}; | ||
|
||
t_date_parser::t_date_parser() {} | ||
|
||
bool | ||
t_date_parser::is_valid(std::string const& datestring) { | ||
for (const std::string& fmt : VALID_FORMATS) { | ||
if (fmt != "") { | ||
std::tm t = {}; | ||
std::stringstream ss(datestring); | ||
ss.imbue(std::locale::classic()); | ||
ss >> std::get_time(&t, fmt.c_str()); | ||
if (!ss.fail()) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} // end namespace perspective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2019, the Perspective Authors. | ||
* | ||
* This file is part of the Perspective library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
#include <memory> | ||
#include <vector> | ||
#include <locale> | ||
#include <perspective/first.h> | ||
#include <perspective/exports.h> | ||
|
||
namespace perspective { | ||
|
||
class PERSPECTIVE_EXPORT t_date_parser { | ||
public: | ||
t_date_parser(); | ||
|
||
bool is_valid(std::string const& datestring); | ||
|
||
private: | ||
static const std::string VALID_FORMATS[12]; | ||
}; | ||
} // end namespace perspective |