stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r512584 - in /incubator/stdcxx/trunk/examples/manual: out/time_put.out time_put.cpp
Date Wed, 28 Feb 2007 02:56:18 GMT
Author: sebor
Date: Tue Feb 27 18:56:17 2007
New Revision: 512584

URL: http://svn.apache.org/viewvc?view=rev&rev=512584
Log:
2007-02-27  Martin Sebor  <sebor@roguewave.com>

	* time_put.cpp: Added descriptions of ordinary (unmodified) conversion
	specifiers and printed out the "base" format specifier string for %c,
	%r, %x, and %X. Changed the date to demonstrate the formatting of %e
	formats (space followed by a digit for single-digit days of month).
	Treated the first command line argument as the name of the locale to
	format output in (including "" for the locale specified by the
	environment).
	* time_put.out: Adjusted accordingly.

Modified:
    incubator/stdcxx/trunk/examples/manual/out/time_put.out
    incubator/stdcxx/trunk/examples/manual/time_put.cpp

Modified: incubator/stdcxx/trunk/examples/manual/out/time_put.out
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/manual/out/time_put.out?view=diff&rev=512584&r1=512583&r2=512584
==============================================================================
--- incubator/stdcxx/trunk/examples/manual/out/time_put.out (original)
+++ incubator/stdcxx/trunk/examples/manual/out/time_put.out Tue Feb 27 18:56:17 2007
@@ -1,58 +1,58 @@
-   "%a"   "Wed"
-   "%A"   "Wednesday"
-   "%b"   "Feb"
-   "%B"   "February"
-   "%c"   "Wed Feb 29 14:34:56 1984"
-   "%C"   "19"
-   "%d"   "29"
-   "%D"   "02/29/84"
-   "%e"   "29"
-   "%F"   "1984-02-29"
-   "%g"   "84"
-   "%G"   "1984"
-   "%h"   "Feb"
-   "%H"   "14"
-   "%I"   "02"
-   "%j"   "060"
-   "%k"   "14"
-   "%l"   " 2"
-   "%m"   "02"
-   "%M"   "34"
-   "%n"   "\n"
-   "%p"   "PM"
-   "%r"   "02:34:56 PM"
-   "%R"   "14:34"
-   "%S"   "56"
-   "%t"   "\t"
-   "%T"   "14:34:56"
-   "%u"   "3"
-   "%U"   "09"
-   "%V"   "09"
-   "%w"   "3"
-   "%W"   "09"
-   "%x"   "02/29/84"
-   "%X"   "14:34:56"
-   "%y"   "84"
-   "%Y"   "1984"
-   "%z"   "+0800"
-   "%Z"   "PST"
-   "%%"   "%"
-   "%Ec"   "Wed Feb 29 14:34:56 1984"
-   "%EC"   "19"
-   "%Ex"   "02/29/84"
-   "%EX"   "14:34:56"
-   "%Ey"   "84"
-   "%EY"   "1984"
-   "%Od"   "29"
-   "%Oe"   "29"
-   "%OH"   "14"
-   "%OI"   "02"
-   "%Om"   "02"
-   "%OM"   "34"
-   "%OS"   "56"
-   "%Ou"   "3"
-   "%OU"   "09"
-   "%OV"   "09"
-   "%Ow"   "3"
-   "%OW"   "09"
-   "%Oy"   "84"
+  "%a"    "Fri"                           abbreviated weekday name
+  "%A"    "Friday"                        full weekday name
+  "%b"    "Feb"                           abbreviated month name
+  "%B"    "February"                      full month name
+  "%c"    "Fri Feb  3 14:34:56 1984"      date and time ("%a %b %e %H:%M:%S %Y")
+  "%C"    "19"                            two-digit year [00,99]
+  "%d"    "03"                            month number [01,31]
+  "%D"    "02/03/84"                      date as "%m/%d/%y"
+  "%e"    " 3"                            month number [ 1,31]
+  "%F"    "1984-02-03"                    date as "%Y-%m-%d"
+  "%g"    "84"                            week-based year [00,99]
+  "%G"    "1984"                          week-based year
+  "%h"    "Feb"                           same as "%b"
+  "%H"    "14"                            hour [00-23]
+  "%I"    "02"                            hour [01-12]
+  "%j"    "034"                           day of the year [001,366]
+  "%k"    "14"                            hour [ 0,23]
+  "%l"    " 2"                            hour [ 1,12]
+  "%m"    "02"                            month number [01,12]
+  "%M"    "34"                            minute [00,59]
+  "%n"    "\n"                            newline character
+  "%p"    "PM"                            a.m. or p.m. equivalent
+  "%r"    "02:34:56 PM"                   AM/PM time ("%I:%M:%S %p")
+  "%R"    "14:34"                         24-hour time
+  "%S"    "56"                            second [00,60]
+  "%t"    "\t"                            tab character
+  "%T"    "14:34:56"                      time as "%H:%M:%S"
+  "%u"    "5"                             Monday-based weekday [1,7]
+  "%U"    "05"                            week number [00,53]
+  "%V"    "05"                            week numver [01,53]
+  "%w"    "5"                             weekday [0,6]
+  "%W"    "05"                            week number [00,53]
+  "%x"    "02/03/84"                      date ("%m/%d/%y")
+  "%X"    "14:34:56"                      time ("%H:%M:%S")
+  "%y"    "84"                            two-digit year [00,99]
+  "%Y"    "1984"                          year
+  "%z"    "+0700"                         offset from UTC
+  "%Z"    "MST"                           time zone
+  "%%"    "%"                             percent character
+  "%Ec"   "Fri Feb  3 14:34:56 1984"      
+  "%EC"   "19"                            
+  "%Ex"   "02/03/84"                      
+  "%EX"   "14:34:56"                      
+  "%Ey"   "84"                            
+  "%EY"   "1984"                          
+  "%Od"   "03"                            
+  "%Oe"   " 3"                            
+  "%OH"   "14"                            
+  "%OI"   "02"                            
+  "%Om"   "02"                            
+  "%OM"   "34"                            
+  "%OS"   "56"                            
+  "%Ou"   "5"                             
+  "%OU"   "05"                            
+  "%OV"   "05"                            
+  "%Ow"   "5"                             
+  "%OW"   "05"                            
+  "%Oy"   "84"                            

Modified: incubator/stdcxx/trunk/examples/manual/time_put.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/manual/time_put.cpp?view=diff&rev=512584&r1=512583&r2=512584
==============================================================================
--- incubator/stdcxx/trunk/examples/manual/time_put.cpp (original)
+++ incubator/stdcxx/trunk/examples/manual/time_put.cpp Tue Feb 27 18:56:17 2007
@@ -2,80 +2,211 @@
  *
  * time_put.cpp - Example program for the std::time_put facet. 
  *
- * $Id: //stdlib/dev/examples/stdlib/manual/time_put.cpp#7 $
+ * $Id$
  *
  ***************************************************************************
  *
- * Copyright (c) 1994-2005 Quovadx,  Inc., acting through its  Rogue Wave
- * Software division. Licensed under the Apache License, Version 2.0 (the
- * "License");  you may  not use this file except  in compliance with the
- * License.    You    may   obtain   a   copy   of    the   License    at
- * http://www.apache.org/licenses/LICENSE-2.0.    Unless   required    by
- * applicable law  or agreed to  in writing,  software  distributed under
- * the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
- * CONDITIONS OF  ANY KIND, either  express or implied.  See  the License
- * for the specific language governing permissions  and limitations under
- * the License.
+ * Licensed to the Apache Software  Foundation (ASF) under one or more
+ * contributor  license agreements.  See  the NOTICE  file distributed
+ * with  this  work  for  additional information  regarding  copyright
+ * ownership.   The ASF  licenses this  file to  you under  the Apache
+ * License, Version  2.0 (the  "License"); you may  not use  this file
+ * except in  compliance with the License.   You may obtain  a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the  License is distributed on an  "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY  KIND, either  express or
+ * implied.   See  the License  for  the  specific language  governing
+ * permissions and limitations under the License.
+ *
+ * Copyright 1998-2006 Rogue Wave Software.
  * 
  **************************************************************************/
 
 // NOTE: due to a SunPro bug, <ctime> must preceed <locale> (see PR #26255)
-#include <ctime>      // for struct tm
-#include <iostream>   // for cout, endl
-#include <locale>     // for locale, time_put
+#include <ctime>       // for struct tm
+#include <exception>   // for exception
+#include <iostream>    // for cout, endl
+#include <locale>      // for locale, time_put
+#include <sstream>     // for ostringstream
 
 #include <examples.h>
 
 
-int main ()
+int main (int argc, char *argv[])
 {
     // zero-out all members (including any non-standard ones)
     std::tm t = std::tm ();
 
-    // assign individual members being careful to avoid relying
+    // assign individual members, being careful to avoid relying
     // on the implementation-defined layout of the struct
     t.tm_sec   = 56;   // seconds
     t.tm_min   = 34;   // minutes
     t.tm_hour  = 14;   // hours (i.e., 2 PM)
-    t.tm_mday  = 29;   // day of month
+    t.tm_mday  =  3;   // day of month
     t.tm_mon   =  1;   // month (February)
     t.tm_year  = 84;   // years since 1900 (i.e., 1984)
-    t.tm_yday  = 59;   // day of the year
-    t.tm_wday  =  3;   // day of the week (Wednesday)
+    t.tm_yday  = 33;   // day of the year
+    t.tm_wday  =  5;   // Sunday-based weekday (Friday)
     t.tm_isdst =  0;   // Daylight Saving Time
 
-    // an array of all supported format specifiers
-    const char fmt[][4] = {
-        "%a", "%A", "%b", "%B", "%c", "%C", "%d", "%D", "%e", "%F",
-        "%g", "%G", "%h", "%H", "%I", "%j", "%k", "%l", "%m", "%M",
-        "%n", "%p", "%r", "%R", "%S", "%t", "%T", "%u", "%U", "%V",
-        "%w", "%W", "%x", "%X", "%y", "%Y", "%z", "%Z", "%%",
-
-        "%Ec", "%EC", "%Ex", "%EX", "%Ey", "%EY",
-        "%Od", "%Oe", "%OH", "%OI", "%Om", "%OM", "%OS",
-        "%Ou", "%OU", "%OV", "%Ow", "%OW", "%Oy"
-        
+    // array of all supported conversion specifiers
+    static const struct {
+        const char spec [4];    // specifier
+        const char desc [40];   // description
+    } fmt[] = {
+        // plain (unmodified) conversion specifiers
+        { "%a", "abbreviated weekday name" },
+        { "%A", "full weekday name" },
+        { "%b", "abbreviated month name" },
+        { "%B", "full month name" },
+        { "%c", "date and time" },
+        { "%C", "two-digit year [00,99]" },
+        { "%d", "month number [01,31]" },
+        { "%D", "date as \"%m/%d/%y\"" },
+        { "%e", "month number [ 1,31]" },
+        { "%F", "date as \"%Y-%m-%d\"" },
+        { "%g", "week-based year [00,99]" },
+        { "%G", "week-based year" },
+        { "%h", "same as \"%b\"" },
+        { "%H", "hour [00-23]" },
+        { "%I", "hour [01-12]" },
+        { "%j", "day of the year [001,366]" },
+        { "%k", "hour [ 0,23]" },
+        { "%l", "hour [ 1,12]" },
+        { "%m", "month number [01,12]" },
+        { "%M", "minute [00,59]" },
+        { "%n", "newline character" },
+        { "%p", "a.m. or p.m. equivalent" },
+        { "%r", "AM/PM time" },
+        { "%R", "24-hour time" },
+        { "%S", "second [00,60]" },
+        { "%t", "tab character" },
+        { "%T", "time as \"%H:%M:%S\"" },
+        { "%u", "Monday-based weekday [1,7]" },
+        { "%U", "week number [00,53]" },
+        { "%V", "week numver [01,53]" },
+        { "%w", "weekday [0,6]" },
+        { "%W", "week number [00,53]" },
+        { "%x", "date" },
+        { "%X", "time" },
+        { "%y", "two-digit year [00,99]" },
+        { "%Y", "year" },
+        { "%z", "offset from UTC" },
+        { "%Z", "time zone" },
+        { "%%", "percent character" },
+
+        // modified conversion specifiers
+        { "%Ec", "" },
+        { "%EC", "" },
+        { "%Ex", "" },
+        { "%EX", "" },
+        { "%Ey", "" },
+        { "%EY", "" },
+        { "%Od", "" },
+        { "%Oe", "" },
+        { "%OH", "" },
+        { "%OI", "" },
+        { "%Om", "" },
+        { "%OM", "" },
+        { "%OS", "" },
+        { "%Ou", "" },
+        { "%OU", "" },
+        { "%OV", "" },
+        { "%Ow", "" },
+        { "%OW", "" },
+        { "%Oy", "" }
     };
 
+    if (1 < argc) {
+        try {
+            // imbue the locale specified by the argument in cout
+            std::cout.imbue (std::locale (argv [1]));
+        }
+        catch (std::exception &ex) {
+            // print out the reason for the failure and exit
+            std::cerr << ex.what () << '\n';
+            return 1;
+        }
+    }
+
     // retrieve the time_put facet installed in cout
     const std::time_put<char> &tput =
         std::use_facet<std::time_put<char> >(std::cout.getloc ());
 
-    // print out the value of t formatted according to each
-    // of the format/ specifiers defined above
+    // left-justify text
+    std::cout.setf (std::cout.left);
+
+    // print out the time value formatted according to each
+    // of the conversion specifiers defined above
     for (std::size_t i = 0; i != sizeof fmt / sizeof *fmt; ++i) {
-        std::cout << "   \"" << fmt [i] << "\"   \"";
 
-        if ('n' == fmt [i][1])
-            std::cout << "\\n";
-        else if ('t' == fmt [i][1])
-            std::cout << "\\t";
+        const char* const spec = fmt [i].spec;
+
+        std::ostringstream tmp;
+
+        // format the directive, in quotes, in a temporary stringstream
+        tmp << '"' << spec << '"';
+
+        // format the quoted directive in a field of the given width
+        std::cout << "  ";
+        std::cout.width (8);
+        std::cout << tmp.str ();
+
+        // clear the temporary stringstream
+        tmp.str ("");
+
+        tmp << '"';
+        if ('n' == spec [1])
+            tmp << "\\n";
+        else if ('t' == spec [1])
+            tmp << "\\t";
         else
-            tput.put (std::ostreambuf_iterator<char>(std::cout.rdbuf ()),
-                      std::cout, '\0', &t, fmt [i],
-                      fmt [i] + std::char_traits<char>::length (fmt [i]));
+            tput.put (std::ostreambuf_iterator<char>(tmp.rdbuf ()),
+                      std::cout, '\0', &t, spec,
+                      spec + std::char_traits<char>::length (spec));
+
+        tmp << '"';
+
+        // format the string in a field of the given width
+        std::cout.width (32);
+        std::cout << tmp.str ();
+
+        // clear the temporary stringstream
+        tmp.str ("");
+
+        const char* const d = spec + 1 + ('E' == *spec || 'O' == *spec);
+
+        if ('c' == *d || 'r' == *d || 'x' == *d || 'X' == *d) {
+
+            if (*fmt [i].desc) {
+                // format the description of the directive
+                tmp << fmt [i].desc << " (";
+            }
+
+            tmp << "\"";
+
+            // extension of this implementation:
+            // for %c, %r, %x, and %X, write out the formatting
+            // string that the conversion specifier expands to
+            tput.put (std::ostreambuf_iterator<char>(tmp.rdbuf ()),
+                      std::cout, '\0', 0, spec,
+                      spec + std::char_traits<char>::length (spec));
+
+            tmp << "\"";
+
+            if (*fmt [i].desc)
+                tmp << ')';
+        }
+        else if (*fmt [i].desc) {
+            // format the description of the directive
+            tmp << fmt [i].desc;
+        }
 
-        std::cout << "\"\n";
+        std::cout << tmp.str () << '\n';
     }
 
     return 0;



Mime
View raw message