Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDate.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM__JDATE__
2 #define __JSYSTEM__JDATE__
3 
4 #include <string>
5 #include <time.h>
6 
7 /**
8  * \file
9  * Date and time functions.
10  * \author mdejong
11  */
12 namespace JSYSTEM {}
13 namespace JPP { using namespace JSYSTEM; }
14 
15 namespace JSYSTEM {
16 
17  /**
18  * Get ASCII formatted date.
19  *
20  * \return date
21  */
22  inline const char* getDate()
23  {
24  static time_t ts;
25  static const int MAX_SIZE = 256;
26  static char buffer[MAX_SIZE];
27 
28  time(&ts);
29 
30  strftime(buffer, MAX_SIZE, "%x", localtime(&ts));
31 
32  return buffer;
33  }
34 
35 
36  /**
37  * Get ASCII formatted time.
38  *
39  * \return time
40  */
41  inline const char* getTime()
42  {
43  static time_t ts;
44  static const int MAX_SIZE = 256;
45  static char buffer[MAX_SIZE];
46 
47  time(&ts);
48 
49  strftime(buffer, MAX_SIZE, "%X", localtime(&ts));
50 
51  return buffer;
52  }
53 
54 
55  /**
56  * Auxililary class to get ASCII formatted date and time.
57  */
58  class JDateAndTime {
59  public:
60 
61  /**
62  * Default constructor.
63  */
65  {}
66 
67 
68  /**
69  * Get ASCII formatted date and time.
70  *
71  * \return date and time
72  */
73  inline std::string toString() const
74  {
75  time(&ts);
76 
77  return toString(ts);
78  }
79 
80 
81  /**
82  * Get ASCII formatted date and time.
83  *
84  * \return date and time
85  */
86  inline std::string operator()() const
87  {
88  return toString();
89  }
90 
91 
92  /**
93  * Get ASCII formatted date and time.
94  *
95  * \param ts time structure
96  * \return date and time
97  */
98  static std::string toString(const time_t& ts)
99  {
100  std::string buffer = ctime(&ts);
101 
102  // remove the last character (carriage return) from the date string
103 
104  return buffer.substr(0, buffer.length() - 1);
105  }
106 
107  private:
108  mutable time_t ts;
109  };
110 
111 
112  /**
113  * Function object to get ASCII formatted date and time.
114  */
116 }
117 
118 #endif
Auxililary class to get ASCII formatted date and time.
Definition: JDate.hh:58
static const JDateAndTime getDateAndTime
Function object to get ASCII formatted date and time.
Definition: JDate.hh:115
double getTime(const Hit &hit)
Get true time of hit.
std::string operator()() const
Get ASCII formatted date and time.
Definition: JDate.hh:86
static std::string toString(const time_t &ts)
Get ASCII formatted date and time.
Definition: JDate.hh:98
const char * getDate()
Get ASCII formatted date.
Definition: JDate.hh:22
JDateAndTime()
Default constructor.
Definition: JDate.hh:64
std::string toString() const
Get ASCII formatted date and time.
Definition: JDate.hh:73