summaryrefslogtreecommitdiff
path: root/parse.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2003-10-11 21:39:09 +0000
committerJohn Wiegley <johnw@newartisans.com>2003-10-11 21:39:09 +0000
commit9a14d6322ca1b1651ba31f997c1f719c1d8612e3 (patch)
tree96c35e5afe2f0465e3678e8c6980572ffcd1d3ab /parse.cc
parent4e8bd8cc5bf5292f79a7ea852d0d3953b3d4aaa6 (diff)
downloadfork-ledger-9a14d6322ca1b1651ba31f997c1f719c1d8612e3.tar.gz
fork-ledger-9a14d6322ca1b1651ba31f997c1f719c1d8612e3.tar.bz2
fork-ledger-9a14d6322ca1b1651ba31f997c1f719c1d8612e3.zip
*** empty log message ***
Diffstat (limited to 'parse.cc')
-rw-r--r--parse.cc58
1 files changed, 31 insertions, 27 deletions
diff --git a/parse.cc b/parse.cc
index dc073c4a..5cdfb9e3 100644
--- a/parse.cc
+++ b/parse.cc
@@ -49,40 +49,44 @@ static const char *formats[] = {
NULL
};
-bool parse_date(const char * date_str, std::time_t * result,
- const int year = -1)
+bool parse_date_mask(const char * date_str, struct std::tm * result)
{
- struct std::tm when;
-
- std::time_t now = std::time(NULL);
- struct std::tm * now_tm = std::localtime(&now);
-
for (const char ** f = formats; *f; f++) {
- memset(&when, INT_MAX, sizeof(struct std::tm));
- if (strptime(date_str, *f, &when)) {
- when.tm_hour = 0;
- when.tm_min = 0;
- when.tm_sec = 0;
-
- if (when.tm_year == -1)
- when.tm_year = year == -1 ? now_tm->tm_year : year - 1900;
-
- if (std::strcmp(*f, "%Y") == 0) {
- when.tm_mon = 0;
- when.tm_mday = 1;
- } else {
- if (when.tm_mon == -1)
- when.tm_mon = now_tm->tm_mon;
- if (when.tm_mday == -1)
- when.tm_mday = now_tm->tm_mday;
- }
- *result = std::mktime(&when);
+ memset(result, INT_MAX, sizeof(struct std::tm));
+ if (strptime(date_str, *f, result))
return true;
- }
}
return false;
}
+bool parse_date(const char * date_str, std::time_t * result, const int year)
+{
+ struct std::tm when;
+
+ if (! parse_date_mask(date_str, &when))
+ return false;
+
+ static std::time_t now = std::time(NULL);
+ static struct std::tm * now_tm = std::localtime(&now);
+
+ when.tm_hour = 0;
+ when.tm_min = 0;
+ when.tm_sec = 0;
+
+ if (when.tm_year == -1)
+ when.tm_year = ((year == -1) ? now_tm->tm_year : (year - 1900));
+
+ if (when.tm_mon == -1)
+ when.tm_mon = now_tm->tm_mon;
+
+ if (when.tm_mday == -1)
+ when.tm_mday = now_tm->tm_mday;
+
+ *result = std::mktime(&when);
+
+ return true;
+}
+
void parse_price_setting(const std::string& setting)
{
char buf[128];