oqltime.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include "oql_p.h"
00026 
00027 namespace eyedb {
00028 
00029 #if 0
00030 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00031 //
00032 // oqmlTimeToString methods
00033 //
00034 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00035 
00036 oqmlTimeToString::oqmlTimeToString(oqml_List * _list) : oqmlNode(oqmlTIMETOSTRING)
00037 {
00038   list = _list;
00039 }
00040 
00041 oqmlTimeToString::~oqmlTimeToString()
00042 {
00043 }
00044 
00045 oqmlStatus *oqmlTimeToString::compile(Database *db, oqmlContext *ctx)
00046 {
00047   oqmlStatus *s;
00048   s = ql->compile(db, ctx);
00049 
00050   if (s)
00051     return s;
00052 
00053   return oqmlSuccess;
00054 }
00055 
00056 oqmlStatus *oqmlTimeToString::eval(Database *db, oqmlContext *ctx,
00057                             oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00058 {
00059   oqmlStatus *s;
00060   oqmlAtomList *al;
00061 
00062   s = ql->eval(db, ctx, &al);
00063 
00064   if (s)
00065     return s;
00066 
00067   if (al->cnt != 1 || al->first->type.type != oqmlATOM_INT)
00068     return new oqmlStatus("invalid argument for strtime(int)");
00069 
00070   eyedblib::int32 t = ((oqmlAtom_int *)al->first)->i;
00071   char *str = ctime((time_t *)&t);
00072   str[strlen(str)-1] = 0;
00073   *alist = new oqmlAtomList(new oqmlAtom_string(str));
00074   return oqmlSuccess;
00075 }
00076 
00077 void oqmlTimeToString::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00078 {
00079   at->type = oqmlATOM_INT;
00080   at->cls = 0;
00081   at->comp = oqml_False;
00082 }
00083 
00084 oqmlBool oqmlTimeToString::isConstant() const
00085 {
00086   return OQMLBOOL(ql->isConstant());
00087 }
00088 
00089 std::string
00090 oqmlTimeToString::toString(void) const
00091 {
00092   return std::string("strtime(") + ql->toString() + ")" + oqml_isstat();
00093 }
00094 #endif
00095 
00096 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00097 //
00098 // oqmlTimeFormat methods
00099 //
00100 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00101 
00102 oqmlTimeFormat::oqmlTimeFormat(oqml_List * _list) : oqmlNode(oqmlTIMEFORMAT)
00103 {
00104   list = _list;
00105 }
00106 
00107 oqmlTimeFormat::~oqmlTimeFormat()
00108 {
00109 }
00110 
00111 static oqmlStatus *
00112 usage()
00113 {
00114   return new oqmlStatus("usage: timeformat(time [, format])");
00115 }
00116 
00117 oqmlStatus *oqmlTimeFormat::compile(Database *db, oqmlContext *ctx)
00118 {
00119   if (list->cnt != 1 && list->cnt != 2)
00120     return usage();
00121 
00122   time = list->first->ql;
00123   format = list->cnt == 2 ? list->first->next->ql : 0;
00124   
00125   oqmlStatus *s;
00126   s = time->compile(db, ctx);
00127   if (s) return s;
00128 
00129   return format ? format->compile(db, ctx) : oqmlSuccess;
00130 }
00131 
00132 oqmlStatus *oqmlTimeFormat::eval(Database *db, oqmlContext *ctx,
00133                              oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00134 {
00135   oqmlStatus *s;
00136   oqmlAtomList *al;
00137 
00138   s = time->eval(db, ctx, &al);
00139 
00140   if (s) return s;
00141 
00142   if (al->cnt != 1 || !OQML_IS_INT(al->first))
00143     return usage();
00144 
00145   struct tm *t = localtime((time_t *)&(OQML_ATOM_INTVAL(al->first)));
00146 
00147   // must check second argument
00148   // if no argument => ctime
00149   const char *fmt = 0;
00150   if (format)
00151     {
00152       oqmlAtomList *fmtlist;
00153       s = format->eval(db, ctx, &fmtlist);
00154       if (s) return s;
00155       if (fmtlist->cnt != 1 || !OQML_IS_STRING(fmtlist->first))
00156         return usage();
00157       fmt = OQML_ATOM_STRVAL(fmtlist->first);
00158     }
00159 
00160   char str[512];
00161   if (!strftime(str, sizeof(str)-1, fmt, t))
00162     *alist = new oqmlAtomList(new oqmlAtom_string("<time format error>"));
00163   else
00164     *alist = new oqmlAtomList(new oqmlAtom_string(str));
00165 
00166   return oqmlSuccess;
00167 }
00168 
00169 void oqmlTimeFormat::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00170 {
00171   at->type = oqmlATOM_STRING;
00172   at->cls = 0;
00173   at->comp = oqml_True;
00174 }
00175 
00176 oqmlBool oqmlTimeFormat::isConstant() const
00177 {
00178   return OQMLBOOL(time->isConstant());
00179 }
00180 
00181 std::string
00182 oqmlTimeFormat::toString(void) const
00183 {
00184   return std::string("timeformat(") + time->toString() +
00185     (format ? std::string(", ") + format->toString() : std::string("")) +
00186     ")" + oqml_isstat();
00187 }
00188 }

Generated on Mon Dec 22 18:16:06 2008 for eyedb by  doxygen 1.5.3