OSDN Git Service

date-format function.
authorvisor <visor@users.sourceforge.jp>
Sat, 17 Apr 2010 15:49:02 +0000 (00:49 +0900)
committervisor <visor@users.sourceforge.jp>
Sat, 17 Apr 2010 15:49:02 +0000 (00:49 +0900)
lib/util_string.cc
lib/util_string.h
modules/ml-string.cc
modules/ml-string.h
modules/motor-function.cc
wiki/wikiformat.cc
wiki/wikiline.cc

index 8c73d20..de62292 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <time.h>
 #include <float.h>
 #include <ctype.h>
 
@@ -1019,20 +1020,22 @@ static void  format_Month (ustring& ans, MNode* a, std::vector<ustring>& par) {
     format_literal (ans, a, mstr, 1, 12);
 }
 
+static const char*  WStr_a[] = {
+    "Sun", "Mon", "Tue", "Wed",
+    "Thu", "Fri", "Sat"
+};
+
+static const char*  WStr[] = {
+    "Sunday", "Monday", "Tuesday", "Wednesday",
+    "Thursday", "Friday", "Saturday"
+};
+
 static void  format_week (ustring& ans, MNode* a, std::vector<ustring>& par) {
-    static const char*  wstr_a[] = {
-       "Sun", "Mon", "Tue", "Wed",
-       "Thu", "Fri", "Sat"
-    };
-    format_literal (ans, a, wstr_a, 0, 7);
+    format_literal (ans, a, WStr_a, 0, 7);
 }
 
 static void  format_Week (ustring& ans, MNode* a, std::vector<ustring>& par) {
-    static const char*  wstr[] = {
-       "Sunday", "Monday", "Tuesday", "Wednesday",
-       "Thursday", "Friday", "Saturday"
-    };
-    format_literal (ans, a, wstr, 0, 7);
+    format_literal (ans, a, WStr, 0, 7);
 }
 
 ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par) {
@@ -1094,6 +1097,28 @@ ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par)
     return ans;
 }
 
+static ustring  colpad0 (int n, const ustring& src) {
+    int  m;
+
+    if (n > 0) {
+       n = std::min (32, n);
+       m = n - src.length ();
+       if (m > 0) {
+           ustring  ans;
+           ans.reserve (n);
+           ans.append (m, '0');
+           ans.append (src);
+           return ans;
+       } else if (m == 0) {
+           return src;
+       } else {
+           return ustring (src.end () - n, src.end ());
+       }
+    } else {
+       return src;
+    }
+}
+
 /*
  ${Y:4}, ${Y:2}
  ${M:2}, ${M}
@@ -1103,61 +1128,51 @@ ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par)
  ${s:2}, ${s}
  ${W}, ${w}
 */
-ustring  formatDateString (const ustring& format, boost::ptr_vector<MNodePtr>& par) {
+ustring  formatDateString (const ustring& format, time_t tm) {
     ustring  ans;
+    struct tm  v;
     uiterator  b, e;
     umatch  m;
-    u_int  i;
-    MNode*  a;
+    int  pc;
     static uregex  re ("\\$\\{([YMDhmsWw])(:([0-9]))?\\}");
+    std::vector<ustring>  fpar;
 
+    localtime_r (&tm, &v);
     b = format.begin ();
     e = format.end ();
     while (usearch (b, e, m, re)) {
-       std::vector<ustring>  fpar;
        ans.append (b, m[0].first);
        b = m[0].second;
+       if (m[2].matched) {
+           pc = strtol (ustring (m[3].first, m[3].second));
+       } else {
+           pc = 0;
+       }
        switch (*m[1].first) {
        case 'Y':
-           a = par[0] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_year + 1900)));
            break;
        case 'M':
-           a = par[1] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_mon + 1)));
            break;
        case 'D':
-           a = par[2] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_mday)));
            break;
        case 'h':
-           a = par[3] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_hour)));
            break;
        case 'm':
-           a = par[4] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_min)));
            break;
        case 's':
-           a = par[5] ();
+           ans.append (colpad0 (pc, to_ustring (v.tm_sec)));
            break;
        case 'W':
+           ans.append (WStr [v.tm_wday]);
+           break;
        case 'w':
-           a = par[6] ();
+           ans.append (WStr_a [v.tm_wday]);
            break;
-       default:
-           a = NULL;
-       }
-
-       if (! m[2].matched) {
-           switch (*m[1].first) {
-           case 'W':
-               format_Week (ans, a, fpar);
-               break;
-           case 'w':
-               format_week (ans, a, fpar);
-               break;
-           default:
-               if (a)
-                   ans.append (to_string (a));
-           }
-       } else {
-           format_int (ans, a, strtol (ustring (m[3].first, m[3].second)), true);
        }
     }
     ans.append (b, e);
index d864b78..c6b4e35 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "ustring.h"
 #include "utf16.h"
+#include <time.h>
 #include <vector>
 #include <boost/ptr_container/ptr_vector.hpp>
 #include <boost/lexical_cast.hpp>
@@ -237,6 +238,6 @@ ustring  toLower (uiterator b, uiterator e);
 
 class  MNodePtr;
 ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
-ustring  formatDateString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
+ustring  formatDateString (const ustring& format, time_t tm);
 
 #endif /* UTIL_STRING_H */
index 15aa4d0..d7258bb 100644 (file)
@@ -634,6 +634,38 @@ MNode*  ml_random_key (MNode* cell, MlEnv* mlenv) {
 }
 
 /*DOC:
+===date-format===
+ (date-format FORMAT INTEGER) -> STRING
+
+ ${Y:4}, ${Y:2}
+ ${M:2}, ${M}
+ ${D:2}, ${D}
+ ${h:2}, ${h}
+ ${m:2}, ${m}
+ ${s:2}, ${s}
+ ${W}, ${w}
+
+*/
+//#AFUNC       date-format     ml_date_format
+//#WIKIFUNC    date-format
+MNode*  ml_date_format (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  format;
+    time_t  tm;
+
+    if (! arg)
+       throw (uErrorWrongNumber);
+    format = eval_str (arg->car (), mlenv);
+    nextNodeNonNil (arg);
+    tm = eval_int (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_str (new ustring (formatDateString (format, tm)));
+}
+
+/*DOC:
 ===to-string===
  (to-string OBJECT) -> STRING
 
index 7d8c3e2..7693e92 100644 (file)
@@ -21,6 +21,7 @@ MNode*  ml_length (MNode* cell, MlEnv* mlenv);
 MNode*  ml_pad0 (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ellipsis (MNode* cell, MlEnv* mlenv);
 MNode*  ml_string_format (MNode* cell, MlEnv* mlenv);
+MNode*  ml_date_format (MNode* cell, MlEnv* mlenv);
 MNode*  ml_random_key (MNode* cell, MlEnv* mlenv);
 MNode*  ml_to_string (MNode* cell, MlEnv* mlenv);
 MNode*  ml_to_sexp (MNode* cell, MlEnv* mlenv);
index e5668f4..41d87d0 100644 (file)
@@ -131,13 +131,20 @@ void  mf_wiki (std::vector<ustring>& args, MlEnv* mlenv) {
 ===date===
  [[date:VAR:FORMAT]]
 
+ ${Y:4}, ${Y:2}
+ ${M:2}, ${M}
+ ${D:2}, ${D}
+ ${h:2}, ${h}
+ ${m:2}, ${m}
+ ${s:2}, ${s}
+ ${W}, ${w}
+
 */
 //#MTFUNC      date    mf_date
 void  mf_date (std::vector<ustring>& args, MlEnv* mlenv) {
     time_t  tm;
-    struct tm  v;
     ustring  format;
-    boost::ptr_vector<MNodePtr>  par;
+//    boost::ptr_vector<MNodePtr>  par;
     int  i;
 
     if (args.size () > 0) {
@@ -149,7 +156,8 @@ void  mf_date (std::vector<ustring>& args, MlEnv* mlenv) {
        }
        if (format.length () == 0)
            format = uTimeFormat;
-       datetime_list (par, tm);
-       mlenv->env->output->out_toHTML (formatDateString (format, par));
+//     datetime_list (par, tm);
+//     mlenv->env->output->out_toHTML (formatDateString (format, par));
+       mlenv->env->output->out_toHTML (formatDateString (format, tm));
     }
 }
index aa139c1..86fe3ca 100644 (file)
@@ -1038,7 +1038,7 @@ void  WikiBlockItemDL::output (MotorOutput* out) {
  |1|テレビ|2|
  |2|空気清浄機|3|
 
-]・テーブルセルオプション
++・テーブルセルオプション
 表定義の最初の行の最初のカラムに「table:」を書くと,この行はテーブルオプション指定行になる。2番目以降のカラムは,デフォルトのカラムオプション指定になる。
 
 |table:||c:||
@@ -1059,7 +1059,7 @@ void  WikiBlockItemDL::output (MotorOutput* out) {
 |nowhite|nw||
 |turn||セルの並びの横方向と縦方向を交換する。|
 
-]・セルオプション
++・セルオプション
  |header:名前|class=big:東武|
  |header:
 |table:||c:||
@@ -1088,7 +1088,7 @@ void  WikiBlockItemDL::output (MotorOutput* out) {
 |&^;''text''|上のセルと内容が一致するとき,結合する。|
 |&^;<''text''|上のセルと内容が一致し,上のセルが左のセルと結合しているとき,結合する。|
 
-]・継続オプション
++・継続オプション
 行末に「\」を指定すると,次のテーブル記述を1行として継続する。数値指定を付加し「!''NUM''\」と指定するとカラム数が''NUM''に達したところで継続をやめる。
 
 */
index 29e7d0a..3a601d5 100644 (file)
@@ -537,7 +537,7 @@ bool  wl_date (WikiMotorObjVecVec* args, WikiMotorObjVec* arg2, MotorOutput* out
     ustring  val;
     time_t  tm;
     ustring  format;
-    boost::ptr_vector<MNodePtr>  par;
+//    boost::ptr_vector<MNodePtr>  par;
 
     if (b < e) {
        val = (*b)->textOut (wiki);
@@ -549,8 +549,9 @@ bool  wl_date (WikiMotorObjVecVec* args, WikiMotorObjVec* arg2, MotorOutput* out
            else
                format = uTimeFormat;
            
-           datetime_list (par, tm);
-           out->out_toHTML (formatDateString (format, par));
+//         datetime_list (par, tm);
+//         out->out_toHTML (formatDateString (format, par));
+           out->out_toHTML (formatDateString (format, tm));
        }
        return true;
     }