Back to index

nordugrid-arc-nox  1.1.0~rc6
DateTimeAttribute.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <iostream>
00006 #include "DateTimeAttribute.h"
00007 
00008 namespace ArcSec {
00009 
00010 //DateTimeAttribute
00011 std::string DateTimeAttribute::identifier = "datetime";
00012 bool DateTimeAttribute::equal(AttributeValue* o, bool check_id){
00013   DateTimeAttribute *other;
00014   try{
00015     other = dynamic_cast<DateTimeAttribute*>(o);
00016   } catch(std::exception&) { };
00017   if(other==NULL){
00018     //std::cerr<<"not DateTimeAttribute"<<std::endl;
00019     return false;
00020   }
00021   if(check_id) { if(id != other->id) return false; }
00022   if(value==(other->getValue()))
00023     return true;
00024   else
00025     return false;
00026 }
00027 
00028 bool DateTimeAttribute::lessthan(AttributeValue* o){
00029   DateTimeAttribute *other;
00030   try{
00031     other = dynamic_cast<DateTimeAttribute*>(o);
00032   } catch(std::exception&) { };
00033   if(other==NULL){
00034     //std::cerr<<"not DateTimeAttribute"<<std::endl;
00035     return false;
00036   }
00037   if(value<(other->getValue()))
00038     return true;
00039   else
00040     return false;
00041 }
00042 
00043 bool DateTimeAttribute::inrange(AttributeValue* o){
00044   PeriodAttribute *other;
00045   try{
00046     other = dynamic_cast<PeriodAttribute*>(o);
00047   } catch(std::exception&) { };
00048   if(other==NULL){
00049     //std::cerr<<"not PeriodAttribute"<<std::endl;
00050     return false;
00051   } 
00052   ArcPeriod period = other->getValue(); 
00053 
00054   Arc::Time st, et;
00055   st = period.starttime;
00056   et = period.endtime;
00057   if(period.starttime == Arc::Time(-1))
00058     st = period.endtime - period.duration;
00059   else if(period.endtime == Arc::Time(-1))
00060     et = period.starttime + period.duration;
00061 
00062   if((value>=st)&&(value<=et))
00063     return true;
00064   else
00065     return false;  
00066 } 
00067 
00068 std::string DateTimeAttribute::encode(){
00069   return(value.str(Arc::ISOTime));
00070 }
00071 
00072 //TimeAttribute
00073 std::string TimeAttribute::identifier = "time";
00074 TimeAttribute::TimeAttribute(const std::string& v,const std::string& i) {
00075   id=i;
00076   std::string v1 = "1970-01-01T" + v;
00077   DateTimeAttribute attr(v1,i);
00078   value = attr.getValue(); 
00079 }
00080 
00081 bool TimeAttribute::equal(AttributeValue* o, bool check_id){
00082   TimeAttribute *other;
00083   try{
00084     other = dynamic_cast<TimeAttribute*>(o);
00085   } catch(std::exception&) { };
00086   if(other==NULL){
00087     //std::cerr<<"not TimeAttribute"<<std::endl;
00088     return false;
00089   }
00090   if(check_id) { if(id != other->id) return false; }
00091   if(value==(other->getValue()))
00092     return true;
00093   else
00094     return false;
00095 }
00096 
00097 bool TimeAttribute::lessthan(AttributeValue* o){
00098   TimeAttribute *other;
00099   try{
00100     other = dynamic_cast<TimeAttribute*>(o);
00101   } catch(std::exception&) { };
00102   if(other==NULL){
00103     //std::cerr<<"not TimeAttribute"<<std::endl;
00104     return false;
00105   }
00106   if(value<(other->getValue()))
00107     return true;
00108   else
00109     return false;
00110 }
00111 
00112 std::string TimeAttribute::encode(){
00113   std::string v;
00114   v = value.str(Arc::ISOTime);
00115   return(v.substr(11));
00116 }
00117 
00118 //DateAttribute
00119 std::string DateAttribute::identifier = "date";
00120 DateAttribute::DateAttribute(const std::string& v,const std::string& i) {
00121   id=i;
00122   std::string v1 = v + "T00:00:00+00:00";
00123   DateTimeAttribute attr(v1,i);
00124   value = attr.getValue();
00125 }
00126 
00127 bool DateAttribute::equal(AttributeValue* o, bool check_id){
00128   DateAttribute *other;
00129   try{
00130     other = dynamic_cast<DateAttribute*>(o);
00131   } catch(std::exception&) { };
00132   if(other==NULL){
00133     //std::cerr<<"not DateAttribute"<<std::endl;
00134     return false;
00135   }
00136   if(check_id) { if(id != other->id) return false; }
00137   if(value==(other->getValue()))
00138     return true;
00139   else
00140     return false;
00141 }
00142 
00143 bool DateAttribute::lessthan(AttributeValue* o){
00144   DateAttribute *other;
00145   try{
00146     other = dynamic_cast<DateAttribute*>(o);
00147   } catch(std::exception&) { };
00148   if(other==NULL){
00149     //std::cerr<<"not DateAttribute"<<std::endl;
00150     return false;
00151   }
00152   if(id != other->id) return false;
00153   if(value<(other->getValue()))
00154     return true;
00155   else
00156     return false;
00157 }
00158 
00159 std::string DateAttribute::encode(){
00160   std::string v;
00161   v = value.str(Arc::ISOTime);
00162   return(v.substr(0,9));
00163 }
00164 
00165 //DurationAttribute
00166 std::string DurationAttribute::identifier = "duration";
00167 bool DurationAttribute::equal(AttributeValue* o, bool check_id){
00168   DurationAttribute *other;
00169   try{
00170     other = dynamic_cast<DurationAttribute*>(o);
00171   } catch(std::exception&) { };
00172   if(other==NULL){
00173     //std::cerr<<"not DurationAttribute"<<std::endl;
00174     return false;
00175   }
00176   if(check_id) { if(id != other->id) return false; }
00177   if((value.GetPeriod())==((other->getValue()).GetPeriod()))
00178     return true;
00179   else
00180     return false;
00181 }
00182 
00183 std::string DurationAttribute::encode(){
00184   return(std::string(value));
00185 }
00186 
00187 //PeriodAttribute
00188 std::string PeriodAttribute::identifier = "period";
00189 PeriodAttribute::PeriodAttribute(const std::string& v,const std::string& i) {
00190   id=i;
00191   (value.starttime).SetTime(-1);
00192   (value.endtime).SetTime(-1);
00193   (value.duration).SetPeriod(0);
00194 
00195   Arc::Time t1(-1), t2(-1);
00196   Arc::Period d1(0), d2(0);
00197 
00198   std::string::size_type pos = v.find("/");
00199   std::string s1 = v.substr(0, pos);
00200   std::string s2 = v.substr(pos+1);
00201 
00202   pos = s1.find("-");
00203   if(pos == std::string::npos) pos = s1.find(":");
00204   if(pos == std::string::npos) pos = s1.find("Z");
00205   if(pos == std::string::npos) pos = s1.find("GMT");
00206   if(pos != std::string::npos)
00207     t1 = Arc::Time(s1);
00208   else
00209     d1 = Arc::Period(s1);
00210 
00211   pos = s2.find("-");
00212   if(pos == std::string::npos) pos = s2.find(":");
00213   if(pos == std::string::npos) pos = s2.find("Z");
00214   if(pos == std::string::npos) pos = s2.find("GMT");
00215   if(pos != std::string::npos)
00216     t2 = Arc::Time(s2);
00217   else
00218     d2 = Arc::Period(s2);
00219 
00220   if(t1.GetTime()!=-1){
00221     if(t2.GetTime()!=-1){
00222       value.starttime = t1;
00223       value.endtime = t2;
00224     }
00225     else if(d2.GetPeriod()!=0){
00226       value.starttime = t1;
00227       value.duration = d2;
00228     }
00229     else {
00230       //std::cerr<<"Invalid ISO period format!"<<std::endl;
00231     }
00232   }
00233   else{
00234     if(d1.GetPeriod()!=0){
00235       if(t2.GetTime()!=-1){
00236         value.duration = d1;
00237         value.endtime = t2;
00238       }
00239       else {
00240         //std::cerr<<"Invalid ISO period format!"<<std::endl;
00241       }
00242     }
00243     else {
00244       //std::cerr<<"Invalid ISO period format!"<<std::endl;
00245     }
00246   }
00247 }
00248 
00249 bool PeriodAttribute::equal(AttributeValue* o, bool check_id){
00250   PeriodAttribute *other;
00251   try{
00252     other = dynamic_cast<PeriodAttribute*>(o);
00253   } catch(std::exception&) { };
00254   if(other==NULL){
00255     //std::cerr<<"not PeriodAttribute"<<std::endl;
00256     return false;
00257   }
00258   if(check_id) { if(id != other->id) return false; }
00259   
00260   ArcPeriod oth = other->getValue();
00261   Arc::Time ls, le, os, oe;
00262   Arc::Period ld, od;
00263   ls = value.starttime;
00264   le = value.endtime;
00265   os = oth.starttime;
00266   oe = oth.endtime;
00267   ld = value.duration;
00268   od = oth.duration;
00269 
00270   if((ls!=Arc::Time(-1))&&(le==Arc::Time(-1)))
00271     le = ls + ld;
00272   else if((ls==Arc::Time(-1))&&(le!=Arc::Time(-1)))
00273     ls = le - ld;
00274   else if((ls==Arc::Time(-1))||(le==Arc::Time(-1)))
00275     return false;
00276 
00277   if((os!=Arc::Time(-1))&&(oe==Arc::Time(-1)))
00278     oe = os + od;
00279   else if((os==Arc::Time(-1))&&(oe!=Arc::Time(-1)))
00280     os = oe - od;
00281   else if((os==Arc::Time(-1))||(oe==Arc::Time(-1)))
00282     return false;
00283   
00284   //std::cout<<ls.str()<<(std::string)(ld)<<os.str()<<(std::string)(od)<<std::endl;
00285 
00286   if((ls==os)&&(le==oe))
00287     return true;
00288   else 
00289     return false;
00290 }
00291 
00292 std::string PeriodAttribute::encode(){
00293   std::string s1 = (std::string)(value.starttime);
00294   std::string s2 = (std::string)(value.endtime);
00295   std::string s3 = (std::string)(value.duration);
00296 
00297   if(value.starttime==Arc::Time(-1))
00298     return(s3+"/"+s2);
00299   else if(value.endtime==Arc::Time(-1))
00300     return(s1+"/"+s3);
00301   else
00302     return(s1+"/"+s2);
00303 }
00304 
00305 }  //namespace ArcSec