23 #include "DateFormat.h"
25 DateFormat::DateFormat() {
30 DateFormat::~DateFormat() {
35 DateFormat::DateFormat(
string format)
37 this->formatspec = format;
40 string DateFormat::format(
Date date)
42 string temp = this->formatspec;
43 StringUtil::replaceAll(temp,
"hh",date.getHhStr());
44 StringUtil::replaceAll(temp,
"mi",date.getMmStr());
45 StringUtil::replaceAll(temp,
"ss",date.getSsStr());
46 StringUtil::replaceAll(temp,
"ns",date.getNsStr());
47 StringUtil::replaceAll(temp,
"ddd",date.getDayw());
48 StringUtil::replaceAll(temp,
"dd",date.getDayStr());
49 StringUtil::replaceAll(temp,
"mmm",date.getMonthw());
50 StringUtil::replaceAll(temp,
"mm",date.getMonthStr());
51 StringUtil::replaceAll(temp,
"yyyy",date.getYearStr());
52 StringUtil::replaceAll(temp,
"yy",date.getYearStr().substr(2));
53 if(date.getTimeZoneOffset()>0)
55 temp += (
"+" + CastUtil::lexical_cast<
string>(date.getTimeZoneOffset()));
57 else if(date.getTimeZoneOffset()<0)
59 temp += (
"-" + CastUtil::lexical_cast<
string>(date.getTimeZoneOffset()));
64 Date* DateFormat::parse(
string strdate)
66 string temp = this->formatspec;
68 string yyyy,yy,ddd,dd,mmm,mm,hh,mi,ss,tzv;
69 if(temp.find(
"yyyy")!=string::npos)
71 yyyy = strdate.substr(temp.find(
"yyyy"),4);
73 else if(temp.find(
"yy")!=string::npos)
75 yy = strdate.substr(temp.find(
"yy"),2);
77 if(temp.find(
"ddd")!=string::npos)
79 ddd = strdate.substr(temp.find(
"ddd"),3);
81 if(temp.find(
"dd", temp.find(
"ddd")+3)!=string::npos)
83 dd = strdate.substr(temp.find(
"dd", temp.find(
"ddd")+3),2);
85 if(temp.find(
"mmm")!=string::npos)
87 mmm = strdate.substr(temp.find(
"mmm"),3);
89 else if(temp.find(
"mm")!=string::npos)
91 mm = strdate.substr(temp.find(
"mm"),2);
93 if(temp.find(
"hh")!=string::npos)
95 hh = strdate.substr(temp.find(
"hh"),2);
97 if(temp.find(
"mi")!=string::npos)
99 mi = strdate.substr(temp.find(
"mi"),2);
101 if(temp.find(
"ss")!=string::npos)
103 ss = strdate.substr(temp.find(
"ss"),2);
105 if(strdate.find(
"+")!=string::npos)
107 tzv = strdate.substr(temp.find(
"+")+1);
109 else if(strdate.find(
"-")!=string::npos)
111 tzv = strdate.substr(temp.find(
"-"));
119 date =
new Date(CastUtil::lexical_cast<int>(yyyy),
120 mmm, CastUtil::lexical_cast<int>(dd));
124 date =
new Date(CastUtil::lexical_cast<int>(yyyy),
125 CastUtil::lexical_cast<int>(mm), CastUtil::lexical_cast<int>(dd));
129 throw "Invalid Date month specified";
136 date =
new Date(CastUtil::lexical_cast<int>(yy),
137 mmm, CastUtil::lexical_cast<int>(dd));
141 date =
new Date(CastUtil::lexical_cast<int>(yy),
142 CastUtil::lexical_cast<int>(mm), CastUtil::lexical_cast<int>(dd));
146 throw "Invalid Date month specified";
151 throw "Invalid Date year specified";
156 date->setTimeZoneOffset(CastUtil::lexical_cast<float>(tzv));
158 throw "Invalid Timezone specified";
161 }
catch (
const char* s) {
164 throw "Invalid Date specified";
166 date->setTime(CastUtil::lexical_cast<int>(hh),
167 CastUtil::lexical_cast<int>(mi), CastUtil::lexical_cast<int>(ss));