Skip to content

Commit 0a6d366

Browse files
author
Nicolas Fauvet
committed
add ability to control time format by making milliseconds and microseconds parts optional
1 parent ca07a5b commit 0a6d366

File tree

5 files changed

+211
-105
lines changed

5 files changed

+211
-105
lines changed

include/fixpp/dsl/details/lexical_cast.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ namespace Fixpp
150150

151151
const int sec = parseFragment(2, "Could not parse seconds from UTCTimestamp");
152152

153-
int msec = 0;
153+
std::optional<int> msec;
154154
if (!cursor.eof())
155155
{
156156
// Literal('.')
157157
cursor.advance(1);
158158
msec = parseFragment(3, "Could not parse milliseconds from UTCTimestamp");
159159
}
160160

161-
int usec = 0;
161+
std::optional<int> usec;
162162
if (!cursor.eof())
163163
{
164164
usec = parseFragment(3, "Could not parse microseconds from UTCTimeOnly");
@@ -174,7 +174,7 @@ namespace Fixpp
174174
tm.tm_min = min;
175175
tm.tm_sec = sec;
176176

177-
return {tm, msec, usec, mkgmtime(&tm)};
177+
return {mkgmtime(&tm), msec, usec};
178178
}
179179
};
180180

@@ -205,7 +205,7 @@ namespace Fixpp
205205
tm.tm_mon = month - 1;
206206
tm.tm_mday = day;
207207

208-
return {tm, mkgmtime(&tm)};
208+
return {mkgmtime(&tm)};
209209
}
210210
};
211211

@@ -239,15 +239,15 @@ namespace Fixpp
239239

240240
const int sec = parseFragment(2, "Could not parse seconds from UTCTimeOnly");
241241

242-
int msec = 0;
242+
std::optional<int> msec;
243243
if (!cursor.eof())
244244
{
245245
// Literal('.')
246246
cursor.advance(1);
247247
msec = parseFragment(3, "Could not parse milliseconds from UTCTimeOnly");
248248
}
249249

250-
int usec = 0;
250+
std::optional<int> usec;
251251
if (!cursor.eof())
252252
{
253253
usec = parseFragment(3, "Could not parse microseconds from UTCTimeOnly");

include/fixpp/tag.h

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
#pragma once
88

9-
#include <string>
109
#include <cstring>
11-
#include <iostream>
1210
#include <ctime>
13-
#include <tuple>
11+
#include <iostream>
12+
#include <optional>
13+
#include <string>
1414

1515
namespace Fixpp
1616
{
@@ -124,69 +124,37 @@ namespace Fixpp
124124

125125
struct UTCTimestamp
126126
{
127-
struct Tm
127+
struct Time
128128
{
129-
Tm(int msec = 0, int usec = 0)
130-
: m_msec(msec)
131-
, m_usec(usec)
129+
Time()
130+
: m_time(std::time(nullptr))
132131
{ }
133132

134-
Tm(const std::tm& tm, int msec = 0, int usec = 0)
135-
: m_tm(tm)
133+
Time(std::time_t time, std::optional<int> msec = {}, std::optional<int> usec = {})
134+
: m_time(time)
136135
, m_msec(msec)
137136
, m_usec(usec)
138137
{ }
139138

140-
std::tm tm() const
139+
std::time_t time() const
141140
{
142-
return m_tm;
141+
return m_time;
143142
}
144143

145-
int msec() const
144+
std::optional<int> msec() const
146145
{
147146
return m_msec;
148147
}
149148

150-
int usec() const
149+
std::optional<int> usec() const
151150
{
152151
return m_usec;
153152
}
154-
155-
private:
156-
std::tm m_tm{};
157-
int m_msec{};
158-
int m_usec;
159-
};
160-
161-
struct Time
162-
{
163-
Time()
164-
: m_time(std::time(nullptr))
165-
{ }
166-
167-
Time(std::time_t time, int msec = 0, int usec = 0)
168-
: m_time(time)
169-
, m_tm(msec, usec)
170-
{ }
171-
172-
Time(const std::tm& tm, int msec, int usec = 0, std::time_t time = 0)
173-
: m_time(time)
174-
, m_tm(tm, msec, usec)
175-
{ }
176-
177-
std::time_t time() const
178-
{
179-
return m_time;
180-
}
181-
182-
Tm tm() const
183-
{
184-
return m_tm;
185-
}
186153

187154
private:
188155
std::time_t m_time;
189-
Tm m_tm;
156+
std::optional<int> m_msec;
157+
std::optional<int> m_usec;
190158
};
191159

192160
using StorageType = Time;
@@ -204,24 +172,17 @@ namespace Fixpp
204172
: m_time(std::time(nullptr))
205173
{ }
206174

207-
Date(const std::tm& tm, std::time_t time)
175+
Date(std::time_t time)
208176
: m_time(time)
209-
, m_tm(tm)
210177
{ }
211178

212179
std::time_t time() const
213180
{
214181
return m_time;
215182
}
216183

217-
std::tm tm() const
218-
{
219-
return m_tm;
220-
}
221-
222184
private:
223185
std::time_t m_time;
224-
std::tm m_tm{};
225186
};
226187

227188
using StorageType = Date;
@@ -238,7 +199,7 @@ namespace Fixpp
238199
: m_time(std::time(nullptr))
239200
{ }
240201

241-
Time(std::time_t time, int msec = 0, int usec = 0)
202+
Time(std::time_t time, std::optional<int> msec = {}, std::optional<int> usec = {})
242203
: m_time(time)
243204
, m_msec(msec)
244205
, m_usec(usec)
@@ -249,20 +210,20 @@ namespace Fixpp
249210
return m_time;
250211
}
251212

252-
int msec() const
213+
std::optional<int> msec() const
253214
{
254215
return m_msec;
255216
}
256217

257-
int usec() const
218+
std::optional<int> usec() const
258219
{
259220
return m_usec;
260221
}
261222

262223
private:
263224
std::time_t m_time;
264-
int m_msec{};
265-
int m_usec{};
225+
std::optional<int> m_msec;
226+
std::optional<int> m_usec;
266227
};
267228

268229
using StorageType = Time;
@@ -275,17 +236,17 @@ namespace Fixpp
275236
{
276237
auto time = value.time();
277238
char buffer[32];
278-
strftime(buffer, sizeof buffer, "%Y%m%d-%H:%M:%S", std::gmtime(&time));
239+
strftime(buffer, sizeof buffer, "%Y%m%d-%H:%M:%S", std::gmtime(&time));
279240
os << buffer;
280-
if (value.tm().msec())
281-
{
282-
sprintf(buffer, ".%03d", value.tm().msec());
283-
os << buffer;
284-
}
285-
if (value.tm().usec())
241+
if (value.msec())
286242
{
287-
sprintf(buffer, "%03d", value.tm().usec());
243+
sprintf(buffer, ".%03d", value.msec().value());
288244
os << buffer;
245+
if (value.usec())
246+
{
247+
sprintf(buffer, "%03d", value.usec().value());
248+
os << buffer;
249+
}
289250
}
290251
return os;
291252
}
@@ -294,25 +255,25 @@ namespace Fixpp
294255
{
295256
auto time = value.time();
296257
char buffer[32];
297-
strftime(buffer, sizeof buffer, "%Y%m%d", std::gmtime(&time));
258+
strftime(buffer, sizeof buffer, "%Y%m%d", std::gmtime(&time));
298259
return os << buffer;
299260
}
300261

301262
inline std::ostream& operator<<(std::ostream& os, const UTCTimeOnly::Time& value)
302263
{
303264
auto time = value.time();
304265
char buffer[32];
305-
strftime(buffer, sizeof buffer, "%H:%M:%S", std::gmtime(&time));
266+
strftime(buffer, sizeof buffer, "%H:%M:%S", std::gmtime(&time));
306267
os << buffer;
307268
if (value.msec())
308269
{
309-
sprintf(buffer, ".%03d", value.msec());
310-
os << buffer;
311-
}
312-
if (value.usec())
313-
{
314-
sprintf(buffer, "%03d", value.usec());
270+
sprintf(buffer, ".%03d", value.msec().value());
315271
os << buffer;
272+
if (value.usec())
273+
{
274+
sprintf(buffer, "%03d", value.usec().value());
275+
os << buffer;
276+
}
316277
}
317278
return os;
318279
}

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fixpp_test(visitor_test)
2424
fixpp_test(writer_test)
2525

2626
add_executable(run_bench_test bench_test.cc)
27-
configure_file(${CMAKE_SOURCE_DIR}/tests/data/fix42.log ${CMAKE_CURRENT_BINARY_DIR}/data/fix42.log COPYONLY)
27+
# configure_file(${CMAKE_SOURCE_DIR}/tests/data/fix42.log ${CMAKE_CURRENT_BINARY_DIR}/data/fix42.log COPYONLY)
2828

2929
fixpp_bench(serialize_bench)
3030
fixpp_bench(visit_bench)

0 commit comments

Comments
 (0)