ffead.server.doc
DateFormat.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * DateFormat.cpp
18  *
19  * Created on: Jun 4, 2010
20  * Author: sumeet
21  */
22 
23 #include "DateFormat.h"
24 
25 DateFormat::DateFormat() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 DateFormat::~DateFormat() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 
35 DateFormat::DateFormat(string format)
36 {
37  this->formatspec = format;
38 }
39 
40 string DateFormat::format(Date date)
41 {
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)
54  {
55  temp += ("+" + CastUtil::lexical_cast<string>(date.getTimeZoneOffset()));
56  }
57  else if(date.getTimeZoneOffset()<0)
58  {
59  temp += ("-" + CastUtil::lexical_cast<string>(date.getTimeZoneOffset()));
60  }
61  return temp;
62 }
63 
64 Date* DateFormat::parse(string strdate)
65 {
66  string temp = this->formatspec;
67  Date* date = NULL;
68  string yyyy,yy,ddd,dd,mmm,mm,hh,mi,ss,tzv;
69  if(temp.find("yyyy")!=string::npos)
70  {
71  yyyy = strdate.substr(temp.find("yyyy"),4);
72  }
73  else if(temp.find("yy")!=string::npos)
74  {
75  yy = strdate.substr(temp.find("yy"),2);
76  }
77  if(temp.find("ddd")!=string::npos)
78  {
79  ddd = strdate.substr(temp.find("ddd"),3);
80  }
81  if(temp.find("dd", temp.find("ddd")+3)!=string::npos)
82  {
83  dd = strdate.substr(temp.find("dd", temp.find("ddd")+3),2);
84  }
85  if(temp.find("mmm")!=string::npos)
86  {
87  mmm = strdate.substr(temp.find("mmm"),3);
88  }
89  else if(temp.find("mm")!=string::npos)
90  {
91  mm = strdate.substr(temp.find("mm"),2);
92  }
93  if(temp.find("hh")!=string::npos)
94  {
95  hh = strdate.substr(temp.find("hh"),2);
96  }
97  if(temp.find("mi")!=string::npos)
98  {
99  mi = strdate.substr(temp.find("mi"),2);
100  }
101  if(temp.find("ss")!=string::npos)
102  {
103  ss = strdate.substr(temp.find("ss"),2);
104  }
105  if(strdate.find("+")!=string::npos)
106  {
107  tzv = strdate.substr(temp.find("+")+1);
108  }
109  else if(strdate.find("-")!=string::npos)
110  {
111  tzv = strdate.substr(temp.find("-"));
112  }
113  try
114  {
115  if(yyyy!="")
116  {
117  if(mmm!="")
118  {
119  date = new Date(CastUtil::lexical_cast<int>(yyyy),
120  mmm, CastUtil::lexical_cast<int>(dd));
121  }
122  else if(mm!="")
123  {
124  date = new Date(CastUtil::lexical_cast<int>(yyyy),
125  CastUtil::lexical_cast<int>(mm), CastUtil::lexical_cast<int>(dd));
126  }
127  else
128  {
129  throw "Invalid Date month specified";
130  }
131  }
132  else if(yy!="")
133  {
134  if(mmm!="")
135  {
136  date = new Date(CastUtil::lexical_cast<int>(yy),
137  mmm, CastUtil::lexical_cast<int>(dd));
138  }
139  else if(mm!="")
140  {
141  date = new Date(CastUtil::lexical_cast<int>(yy),
142  CastUtil::lexical_cast<int>(mm), CastUtil::lexical_cast<int>(dd));
143  }
144  else
145  {
146  throw "Invalid Date month specified";
147  }
148  }
149  else
150  {
151  throw "Invalid Date year specified";
152  }
153  if(tzv!="")
154  {
155  try {
156  date->setTimeZoneOffset(CastUtil::lexical_cast<float>(tzv));
157  } catch (...) {
158  throw "Invalid Timezone specified";
159  }
160  }
161  } catch (const char* s) {
162  throw s;
163  } catch (...) {
164  throw "Invalid Date specified";
165  }
166  date->setTime(CastUtil::lexical_cast<int>(hh),
167  CastUtil::lexical_cast<int>(mi), CastUtil::lexical_cast<int>(ss));
168  return date;
169 }