μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
log_inserters.time.cpp
Go to the documentation of this file.
1/*
2---------------------------------------------------------------------------
3
4 This file is part of uHAL.
5
6 uHAL is a hardware access library and programming framework
7 originally developed for upgrades of the Level-1 trigger of the CMS
8 experiment at CERN.
9
10 uHAL is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 uHAL is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22
23
24 Andrew Rose, Imperial College, London
25 email: awr01 <AT> imperial.ac.uk
26
27 Marc Magrans de Abril, CERN
28 email: marc.magrans.de.abril <AT> cern.ch
29
30---------------------------------------------------------------------------
31*/
32
34
35
36#include <iomanip>
37#include <stdio.h> // for NULL
38#include <time.h> // for tm
39
40
41
42namespace uhal
43{
44
45 // Template specialization for printing the year field of a time struct as a 4 digit number
46 template<>
47 void print< year > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
48 {
49 // the tm struct stores years as int number of years since 1900
50 aStr << ( aTm->tm_year+1900 );
51 }
52
53 // Template specialization for printing the year field of a time struct as a 2 digit number
54 template<>
55 void print< yr > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
56 {
57 static const char* lCharacterMapping ( "00010203040506070809"
58 "10111213141516171819"
59 "20212223242526272829"
60 "30313233343536373839"
61 "40414243444546474849"
62 "50515253545556575859"
63 "60616263646566676869"
64 "70717273747576777879"
65 "80818283848586878889"
66 "90919293949596979899" );
67 aStr.write ( lCharacterMapping + ( ( aTm->tm_year%100 ) <<1 ) , 2 );
68 }
69
70 // Template specialization for printing the month field of a time struct as a three character string
71 template<>
72 void print< strmth > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
73 {
74 // the tm struct stores months as int with values 0 - 11 (not 1 - 12)
75 static const char* lCharacterMapping ( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" );
76 aStr.write ( lCharacterMapping + ( aTm->tm_mon<<2 ) , 3 );
77 }
78
79 // Template specialization for printing the month field of a time struct as a 2 digit number
80 template<>
81 void print< mth > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
82 {
83 // the tm struct stores months as int with values 0 - 11 (not 1 - 12)
84 static const char* lCharacterMapping ( "010203040506070809101112" );
85 aStr.write ( lCharacterMapping + ( aTm->tm_mon<<1 ) , 2 );
86 }
87
88 // Template specialization for printing the day field of a time struct as a 2 digit number
89 template<>
90 void print< day > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
91 {
92 // the tm struct stores days as int with values 1 - 31 (so I add a dummy entry to start of LUT)
93 static const char* lCharacterMapping ( "xx010203040506070809"
94 "10111213141516171819"
95 "20212223242526272829"
96 "3031" );
97 aStr.write ( lCharacterMapping + ( aTm->tm_mday<<1 ) , 2 );
98 }
99
100 // Template specialization for printing the hour field of a time struct as a 2 digit number
101 template<>
102 void print< hr > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
103 {
104 // the tm struct stores hours as int with values 0-23
105 static const char* lCharacterMapping ( "00010203040506070809"
106 "10111213141516171819"
107 "20212223" );
108 aStr.write ( lCharacterMapping + ( aTm->tm_hour<<1 ) , 2 );
109 }
110
111 // Template specialization for printing the minute field of a time struct as a 2 digit number
112 template<>
113 void print< min > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
114 {
115 // the tm struct stores minutes as int with values 0-59
116 static const char* lCharacterMapping ( "00010203040506070809"
117 "10111213141516171819"
118 "20212223242526272829"
119 "30313233343536373839"
120 "40414243444546474849"
121 "50515253545556575859" );
122 aStr.write ( lCharacterMapping + ( aTm->tm_min<<1 ) , 2 );
123 }
124
125 // Template specialization for printing the second field of a time struct as a 2 digit number
126 template<>
127 void print< sec > ( std::ostream& aStr , const tm* aTm , const uint32_t& )
128 {
129 // the tm struct stores minutes as int with values 0-61 to take into account the double leap second
130 static const char* lCharacterMapping ( "00010203040506070809"
131 "10111213141516171819"
132 "20212223242526272829"
133 "30313233343536373839"
134 "40414243444546474849"
135 "50515253545556575859"
136 "6061" );
137 aStr.write ( lCharacterMapping + ( aTm->tm_sec<<1 ) , 2 );
138 }
139
140 template<>
141 void print< usec > ( std::ostream& aStr , const tm* , const uint32_t& aUsec )
142 {
143 // the uSeconds are just a 32bit number so print it as an integer
144 aStr << std::setfill ( '0' ) << std::setw ( 6 ) << aUsec;
145 }
146
147
148
149 _Time< DefaultTimeFmt > Time ( const timeval& aTime )
150 {
151 return _Time< DefaultTimeFmt > ( aTime );
152 }
153
154
155 timeval Now()
156 {
157 timeval lTime;
158 gettimeofday ( &lTime, NULL );
159 return lTime;
160 }
161
162}
Forward declaration.
timeval Now()
A helper function to return the current time.
_Time< DefaultTimeFmt > Time(const timeval &aTime)
Helper function which wrap the template uglyness in a pretty package.
void print< yr >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< sec >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< mth >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< hr >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< min >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< day >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< usec >(std::ostream &aStr, const tm *, const uint32_t &aUsec)
void print< year >(std::ostream &aStr, const tm *aTm, const uint32_t &)
void print< strmth >(std::ostream &aStr, const tm *aTm, const uint32_t &)