$extrastylesheet
timestamp.C
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #include "libmesh/libmesh_config.h"
00019 
00020 // C++ includes
00021 #include <ctime>
00022 #include <sstream>
00023 #ifdef LIBMESH_HAVE_LOCALE
00024 #include <locale>
00025 #endif
00026 
00027 // Local includes
00028 #include "libmesh/timestamp.h"
00029 
00030 namespace libMesh
00031 {
00032 
00033 namespace Utility
00034 {
00035 // If the locale header is available, we will use the "C++" way of
00036 // creating a timestamp, otherwise, we'll fall back on the C way.
00037 std::string get_timestamp()
00038 {
00039 #ifdef LIBMESH_HAVE_LOCALE
00040   // Create time_put "facet"
00041   std::locale loc;
00042   const std::time_put<char>& tp = std::use_facet <std::time_put<char> > (loc);
00043 
00044   // Call C-style time getting functions
00045   time_t now    = time(NULL);
00046   tm* tm_struct = localtime(&now);
00047 
00048   // Date will eventually be stored in this ostringstream's string
00049   std::ostringstream date_stream;
00050 
00051   // See below for documentation on the use of the
00052   // std::time_put::put() function
00053   tp.put(date_stream,        /*s*/
00054          date_stream,        /*str*/
00055          date_stream.fill(), /*fill*/
00056          tm_struct,          /*tm*/
00057          'c');               /*format*/
00058 
00059   // Another way to use it is to totally customize the format...
00060   //    char pattern[]="%d %B %Y %I:%M:%S %p";
00061   //    tp.put(date_stream,                /*s*/
00062   //   date_stream,                /*str*/
00063   //   date_stream.fill(),         /*fill*/
00064   //   tm_struct,                  /*tm*/
00065   //   pattern,                    /*format begin*/
00066   //   pattern+sizeof(pattern)-1); /*format end  */
00067 
00068   return date_stream.str();
00069 #else
00070   // C-stye code originally found here:
00071   // http://people.sc.fsu.edu/~burkardt/cpp_src/timestamp/timestamp.C
00072   // Author: John Burkardt, 24 September 2003
00073   const unsigned int time_size = 40;
00074   char time_buffer[time_size];
00075 
00076   time_t now = time ( NULL );
00077   tm* tm_struct = localtime ( &now );
00078 
00079   // No more than time_size characters will be placed into the array.  If the
00080   // total number of resulting characters, including the terminating
00081   // NUL character, is not more than time_size, strftime() returns the
00082   // number of characters in the array, not counting the terminating
00083   // NUL.  Otherwise, zero is returned and the buffer contents are
00084   // indeterminate.
00085   size_t len = strftime ( time_buffer, time_size, "%c", tm_struct );
00086 
00087   if (len != 0)
00088     return std::string(time_buffer);
00089   else
00090     {
00091       libMesh::out << "Error formatting time buffer, returning empty string!" << std::endl;
00092       return std::string("");
00093     }
00094 
00095 #endif // LIBMESH_HAVE_LOCALE
00096 }
00097 
00098 // std::time_put::put() documentation
00099 // s=Iterator pointing to the first character of the output sequence.
00100 //
00101 // str=Object of a class derived from ios_base (generally an output
00102 // stream object). It is used to obtain formatting information.
00103 //
00104 // fill=Fill character. The fill character is used in output insertion
00105 // operations to fill spaces when the format requires some character padding.
00106 //
00107 // tm=Pointer to an object of type struct tm
00108 //
00109 // format=The final argument to time_put::put is an individual format character.
00110 // The function will format some of the information pointed by the tm struct
00111 // into a sequence of characters as specified by this character, just as if
00112 // it was preceded by a percentage sign in a format string passed to strftime.
00113 // (see 'man strftime' for more...)
00114 // Example: 'c' is national representation of time and date
00115 // 'c' = Thu Feb  4 12:24:11 2010
00116 //  tp.put(date_stream /*s*/,
00117 // date_stream /*str*/,
00118 // date_stream.fill() /*fill*/,
00119 // tm_struct, /*tm*/
00120 // 'c'/*format*/);
00121 
00122 // We can also pass to char* to the beginning and end of the desired format:
00123 // const charT* pattern, const charT* pat_end.  This allows us to have the full
00124 // flexibility of strftime.
00125 // Example: "%d %B %Y %I:%M:%S %p"
00126 // 04 February 2010 01:44:10 PM
00127 }
00128 
00129 } // namespace libMesh