OSDN Git Service

update date format.
authorvisor <visor@users.sourceforge.jp>
Sat, 5 Nov 2011 14:47:14 +0000 (23:47 +0900)
committervisor <visor@users.sourceforge.jp>
Sat, 5 Nov 2011 14:47:14 +0000 (23:47 +0900)
lib/util_string.cc

index 9fcd9a0..371d8d7 100644 (file)
@@ -984,21 +984,21 @@ static void  format_literal (ustring& ans, MNode* a, const char* list[], int off
     }
 }
 
+static const char*  mstr_a[] = {
+    "Jan", "Feb", "Mar", "Apr",
+    "May", "Jun", "Jul", "Aug",
+    "Sep", "Oct", "Nov", "Dec"
+};
+static const char*  mstr[] = {
+    "January", "February", "March", "April",
+    "May", "June", "July", "August",
+    "September", "October", "November", "December"
+};
 static void  format_month (ustring& ans, MNode* a, std::vector<ustring>& par) {
-    static const char*  mstr_a[] = {
-       "Jan", "Feb", "Mar", "Apr",
-       "May", "Jun", "Jul", "Aug",
-       "Sep", "Oct", "Nov", "Dec"
-    };
     format_literal (ans, a, mstr_a, 1, 12);
 }
 
 static void  format_Month (ustring& ans, MNode* a, std::vector<ustring>& par) {
-    static const char*  mstr[] = {
-       "January", "February", "March", "April",
-       "May", "June", "July", "August",
-       "September", "October", "November", "December"
-    };
     format_literal (ans, a, mstr, 1, 12);
 }
 
@@ -1103,12 +1103,13 @@ static ustring  colpad0 (int n, const ustring& src) {
 
 /*
  ${Y:4}, ${Y:2}
- ${M:2}, ${M}
+ ${M:2}, ${M}, ${M:name}, ${M:ab}
  ${D:2}, ${D}
  ${h:2}, ${h}
  ${m:2}, ${m}
  ${s:2}, ${s}
  ${W}, ${w}
+ ${o}
 */
 ustring  formatDateString (const ustring& format, time_t tm) {
     ustring  ans;
@@ -1116,7 +1117,8 @@ ustring  formatDateString (const ustring& format, time_t tm) {
     uiterator  b, e;
     umatch  m;
     int  pc;
-    static uregex  re ("\\$\\{([YMDhmsWw])(:([0-9]))?\\}");
+//    static uregex  re ("\\$\\{([YMDhmsWw])(:([0-9]))?\\}");
+    static uregex  re ("\\$\\{(([YMDhmsWwo])(:([0-9]))?|M:((name)|(ab)|(abname)))\\}");
     std::vector<ustring>  fpar;
 
     localtime_r (&tm, &v);
@@ -1125,36 +1127,63 @@ ustring  formatDateString (const ustring& format, time_t tm) {
     while (usearch (b, e, m, re)) {
        ans.append (b, m[0].first);
        b = m[0].second;
-       if (m[2].matched) {
-           pc = strtol (ustring (m[3].first, m[3].second));
+       if (m[5].matched) {
+           if (m[6].matched) { // name
+               ans.append (mstr[v.tm_mon]);
+           } else if (m[7].matched || m[8].matched) { // abname
+               ans.append (mstr_a[v.tm_mon]);
+           }
        } else {
-           pc = 0;
-       }
-       switch (*m[1].first) {
-       case 'Y':
-           ans.append (colpad0 (pc, to_ustring (v.tm_year + 1900)));
-           break;
-       case 'M':
-           ans.append (colpad0 (pc, to_ustring (v.tm_mon + 1)));
-           break;
-       case 'D':
-           ans.append (colpad0 (pc, to_ustring (v.tm_mday)));
-           break;
-       case 'h':
-           ans.append (colpad0 (pc, to_ustring (v.tm_hour)));
-           break;
-       case 'm':
-           ans.append (colpad0 (pc, to_ustring (v.tm_min)));
-           break;
-       case 's':
-           ans.append (colpad0 (pc, to_ustring (v.tm_sec)));
-           break;
-       case 'W':
-           ans.append (WStr [v.tm_wday]);
-           break;
-       case 'w':
-           ans.append (WStr_a [v.tm_wday]);
-           break;
+//         if (m[2].matched) {
+           if (m[3].matched) {
+//             pc = strtol (ustring (m[3].first, m[3].second));
+               pc = strtol (ustring (m[4].first, m[4].second));
+           } else {
+               pc = 0;
+           }
+//         switch (*m[1].first) {
+           switch (*m[2].first) {
+           case 'Y':
+               ans.append (colpad0 (pc, to_ustring (v.tm_year + 1900)));
+               break;
+           case 'M':
+               ans.append (colpad0 (pc, to_ustring (v.tm_mon + 1)));
+               break;
+           case 'D':
+               ans.append (colpad0 (pc, to_ustring (v.tm_mday)));
+               break;
+           case 'h':
+               ans.append (colpad0 (pc, to_ustring (v.tm_hour)));
+               break;
+           case 'm':
+               ans.append (colpad0 (pc, to_ustring (v.tm_min)));
+               break;
+           case 's':
+               ans.append (colpad0 (pc, to_ustring (v.tm_sec)));
+               break;
+           case 'W':
+               ans.append (WStr [v.tm_wday]);
+               break;
+           case 'w':
+               ans.append (WStr_a [v.tm_wday]);
+               break;
+           case 'o':
+               {
+                   int  h, m;
+                   if (v.tm_gmtoff < 0) {
+                       h = - v.tm_gmtoff / 60;
+                       m = h % 60;
+                       h = h / 60;
+                       ans.append (CharConst ("-")).append (colpad0 (4, to_ustring (h * 100 + m)));
+                   } else {
+                       h = v.tm_gmtoff / 60;
+                       m = h % 60;
+                       h = h / 60;
+                       ans.append (CharConst ("+")).append (colpad0 (4, to_ustring (h * 100 + m)));
+                   }
+               }
+               break;
+           }
        }
     }
     ans.append (b, e);