00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #ifndef EXPANDLIB_VARTYPE_DATE_H_INCLUDED
00031 #define EXPANDLIB_VARTYPE_DATE_H_INCLUDED
00032
00033 #include <boost/lexical_cast.hpp>
00034
00035 #include "expandlib-vartype.h"
00036
00037 namespace expander {
00038
00040
00045 template<
00046
00047 class StringType = ::std::string
00048 >
00049 class expvartype_date : public expandvartype<StringType> {
00050 protected:
00051
00053 class DateType {
00054 public:
00055 DateType(int y=0, int m=0, int d=0) { year=y; month=m; day=d; }
00056
00057 int year;
00058 int month;
00059 int day;
00060 } Date;
00061
00063 inline StringType PadWithZeroes(const StringType &s, const unsigned int num) const {
00064 const StringType::value_type padchar(_T('0'));
00065
00066 if ( s.size() < num ) {
00067 StringType d(num-s.size(), padchar);
00068
00069 d+=s;
00070 return d;
00071 } else {
00072 return s;
00073 }
00074 }
00075
00077 inline void PadWithZeroesTo(StringType &d, const StringType &s, const unsigned int num) const {
00078 const StringType::value_type padchar(_T('0'));
00079
00080 if ( s.size() < num ) {
00081 d.append(num-s.size(), padchar);
00082 d+=s;
00083 } else {
00084 d+=s;
00085 }
00086 }
00087
00088 public:
00089
00090
00091
00092
00094 expvartype_date(int y=0, int m=0, int d=0) : Time(y,m,d) {}
00095
00097 expvartype_date(const _TCHAR * cs) {
00098 const StringType s(cs);
00099 SetValue(s);
00100 }
00101
00103 expvartype_date(const StringType s) {
00104 SetValue(s);
00105 }
00106
00107 virtual void AppendValue(StringType &dest) const {
00108 try {
00109 dest+=boost::lexical_cast<StringType>(Date.year);
00110 } catch(...) {
00111 dest+=_T("0000");
00112 }
00113 dest+=_T('-');
00114 try {
00115 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Date.month),2);
00116 } catch(...) {
00117 dest+=_T("00");
00118 }
00119 dest+=_T('-');
00120 try {
00121 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Date.day),2);
00122 } catch(...) {
00123 dest+=_T("00");
00124 }
00125 }
00126
00127 virtual void AppendValue(StringType &dest, const FParamVectorType ¶ms, const FVarMapType &vars, ExpandResultsType &results) const {
00128 AppendValue(dest);
00129 }
00130
00131 virtual void SetValue(const StringType &s) {
00132 std::vector<int> vals;
00133 {
00134 StringType temp;
00135 for(StringType::const_iterator it=s.begin(); it!=s.end(); ++it) {
00136 if ( *it >= _T('0') && *it <= _T('9') ) {
00137 temp+=*it;
00138 } else if ( *it == _T('-') ) {
00139 try {
00140 vals.push_back(boost::lexical_cast<int>(temp));
00141 } catch (boost::bad_lexical_cast) {
00142 vals.push_back(0);
00143 }
00144 temp=_T("");
00145 } else {
00146 break;
00147 }
00148 }
00149 try {
00150 vals.push_back(boost::lexical_cast<int>(temp));
00151 } catch (boost::bad_lexical_cast) {
00152 vals.push_back(0);
00153 }
00154 }
00155
00156 if ( vals.size() == 3 ) {
00157 Date.year=vals[0];
00158 Date.month=vals[1];
00159 Date.day=vals[2];
00160 if ( Date.month > 12 ) Date.month=12;
00161 if ( Date.month < 1 ) Date.month=1;
00162 if ( Date.day > 31 ) Date.day=31;
00163 if ( Date.day < 1 ) Date.day=1;
00164 }
00165 }
00166
00168 inline StringType get_string() {
00169 StringType s;
00170 AppendValue(s);
00171 return s;
00172 }
00173 };
00174
00175 }
00176
00177 #endif //EXPANDLIB_VARTYPE_DATE_H_INCLUDED