Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : #include "libmesh/libmesh_config.h" 19 : 20 : // C++ includes 21 : #include <ctime> 22 : #include <sstream> 23 : #ifdef LIBMESH_HAVE_LOCALE 24 : #include <locale> 25 : #endif 26 : 27 : // Local includes 28 : #include "libmesh/timestamp.h" 29 : 30 : namespace libMesh 31 : { 32 : 33 : namespace Utility 34 : { 35 : // If the locale header is available, we will use the "C++" way of 36 : // creating a timestamp, otherwise, we'll fall back on the C way. 37 1552 : std::string get_timestamp() 38 : { 39 : #ifdef LIBMESH_HAVE_LOCALE 40 : // Create time_put "facet" 41 2400 : std::locale loc; 42 1552 : const std::time_put<char> & tp = std::use_facet <std::time_put<char>> (loc); 43 : 44 : // Call C-style time getting functions 45 1552 : time_t now = time(nullptr); 46 1552 : tm * tm_struct = localtime(&now); 47 : 48 : // Date will eventually be stored in this ostringstream's string 49 2400 : std::ostringstream date_stream; 50 : 51 : // See below for documentation on the use of the 52 : // std::time_put::put() function 53 704 : tp.put(date_stream, /*s*/ 54 : date_stream, /*str*/ 55 1552 : date_stream.fill(), /*fill*/ 56 : tm_struct, /*tm*/ 57 848 : 'c'); /*format*/ 58 : 59 : // Another way to use it is to totally customize the format... 60 : // char pattern[]="%d %B %Y %I:%M:%S %p"; 61 : // tp.put(date_stream, /*s*/ 62 : // date_stream, /*str*/ 63 : // date_stream.fill(), /*fill*/ 64 : // tm_struct, /*tm*/ 65 : // pattern, /*format begin*/ 66 : // pattern+sizeof(pattern)-1); /*format end */ 67 : 68 1976 : return date_stream.str(); 69 : #else 70 : // C-style code originally found here: 71 : // http://people.sc.fsu.edu/~burkardt/cpp_src/timestamp/timestamp.C 72 : // Author: John Burkardt, 24 September 2003 73 : const unsigned int time_size = 40; 74 : char time_buffer[time_size]; 75 : 76 : time_t now = time (nullptr); 77 : tm * tm_struct = localtime (&now); 78 : 79 : // No more than time_size characters will be placed into the array. If the 80 : // total number of resulting characters, including the terminating 81 : // NUL character, is not more than time_size, strftime() returns the 82 : // number of characters in the array, not counting the terminating 83 : // NUL. Otherwise, zero is returned and the buffer contents are 84 : // indeterminate. 85 : size_t len = strftime ( time_buffer, time_size, "%c", tm_struct ); 86 : 87 : if (len != 0) 88 : return std::string(time_buffer); 89 : else 90 : { 91 : libMesh::out << "Error formatting time buffer, returning empty string!" << std::endl; 92 : return std::string(""); 93 : } 94 : 95 : #endif // LIBMESH_HAVE_LOCALE 96 704 : } 97 : 98 : // std::time_put::put() documentation 99 : // s=Iterator pointing to the first character of the output sequence. 100 : // 101 : // str=Object of a class derived from ios_base (generally an output 102 : // stream object). It is used to obtain formatting information. 103 : // 104 : // fill=Fill character. The fill character is used in output insertion 105 : // operations to fill spaces when the format requires some character padding. 106 : // 107 : // tm=Pointer to an object of type struct tm 108 : // 109 : // format=The final argument to time_put::put is an individual format character. 110 : // The function will format some of the information pointed by the tm struct 111 : // into a sequence of characters as specified by this character, just as if 112 : // it was preceded by a percentage sign in a format string passed to strftime. 113 : // (see 'man strftime' for more...) 114 : // Example: 'c' is national representation of time and date 115 : // 'c' = Thu Feb 4 12:24:11 2010 116 : // tp.put(date_stream /*s*/, 117 : // date_stream /*str*/, 118 : // date_stream.fill() /*fill*/, 119 : // tm_struct, /*tm*/ 120 : // 'c'/*format*/); 121 : 122 : // We can also pass to char * to the beginning and end of the desired format: 123 : // const charT * pattern, const charT * pat_end. This allows us to have the full 124 : // flexibility of strftime. 125 : // Example: "%d %B %Y %I:%M:%S %p" 126 : // 04 February 2010 01:44:10 PM 127 : } 128 : 129 : } // namespace libMesh