DAW JSON Link
Loading...
Searching...
No Matches
daw_json_parse_iso8601_utils.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
12
16
17#include <daw/daw_arith_traits.h>
18#include <daw/daw_attributes.h>
19#include <daw/daw_cpp_feature_check.h>
20#include <daw/daw_cxmath.h>
21#include <daw/daw_not_null.h>
22#include <daw/daw_string_view.h>
23#include <daw/daw_traits.h>
24#include <daw/daw_uint_buffer.h>
25
26#include <cassert>
27#include <chrono>
28#include <cstdint>
29#include <type_traits>
30
31namespace daw::json {
32 inline namespace DAW_JSON_VER {
33 namespace parse_utils {
34 template<typename Result, std::size_t count>
35 constexpr Result parse_unsigned( daw::not_null<char const *> digit_str ) {
36 auto result = UInt64( );
37 for( std::size_t n = 0; n < count; ++n ) {
38 auto const dig =
39 to_uint64( json_details::parse_digit( digit_str[n] ) );
40 if( dig >= 10 ) {
41 break;
42 }
43 result *= 10U;
44 result += dig;
45 }
46 return static_cast<Result>( result );
47 }
48
49 template<typename Result>
50 constexpr Result
51 parse_unsigned2( daw::not_null<char const *> digit_str ) {
52 auto result = UInt64( );
53 unsigned dig = json_details::parse_digit( *digit_str );
54 while( dig < 10 ) {
55 result *= 10U;
56 result += dig;
57 ++digit_str;
58 dig = json_details::parse_digit( *digit_str );
59 }
60 return static_cast<Result>( result );
61 }
62
63 DAW_ATTRIB_FLATINLINE constexpr bool is_number( char c ) {
64 return json_details::parse_digit( c ) < 10U;
65 }
66 } // namespace parse_utils
67
68 namespace datetime {
69 namespace datetime_details {
70 template<typename Result>
71 constexpr Result parse_number( daw::string_view sv ) {
72 static_assert( daw::digits10<Result> >= 4 );
73 daw_json_ensure( not sv.empty( ), ErrorReason::InvalidNumber );
74 Result result = 0;
75 Result sign = 1;
76 if( sv.front( ) == '-' ) {
77 if constexpr( daw::is_signed_v<Result> ) {
78 sign = -1;
79 }
80 sv.remove_prefix( );
81 } else if( sv.front( ) == '+' ) {
82 sv.remove_prefix( );
83 }
84 while( not sv.empty( ) ) {
85 auto const dig = json_details::parse_digit( sv.pop_front( ) );
86 daw_json_ensure( dig < 10U, ErrorReason::InvalidNumber );
87 result *= 10;
88 result += static_cast<Result>( dig );
89 }
90 return result * sign;
91 }
92 } // namespace datetime_details
93 // See:
94 // https://stackoverflow.com/questions/16773285/how-to-convert-stdchronotime-point-to-stdtm-without-using-time-t
95 template<typename TP = std::chrono::time_point<std::chrono::system_clock,
96 std::chrono::milliseconds>>
97 constexpr TP civil_to_time_point( std::int32_t yr, std::uint32_t mo,
98 std::uint32_t dy, std::uint32_t hr,
99 std::uint32_t mn, std::uint32_t se,
100 std::uint64_t ns ) {
101 using Clock = typename TP::clock;
102 using Duration = typename TP::duration;
103 DAW_CPP23_STATIC_LOCAL constexpr auto calc =
104 []( std::int32_t y,
105 std::uint32_t m,
106 std::uint32_t d,
107 std::uint32_t h,
108 std::uint32_t min,
109 std::uint32_t s,
110 std::uint64_t nano ) DAW_JSON_CPP23_STATIC_CALL_OP {
111 y -= static_cast<std::int32_t>( m ) <= 2;
112 std::int32_t const era = ( y >= 0 ? y : y - 399 ) / 400;
113 auto const yoe = static_cast<std::uint32_t>(
114 static_cast<std::int32_t>( y ) - era * 400 ); // [0, 399]
115 auto const doy = static_cast<std::uint32_t>(
116 ( 153 * ( static_cast<std::int32_t>( m ) +
117 ( static_cast<std::int32_t>( m ) > 2 ? -3 : 9 ) ) +
118 2 ) /
119 5 +
120 static_cast<std::int32_t>( d ) - 1 ); // [0, 365]
121 std::uint32_t const doe =
122 yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
123 auto const days_since_epoch =
124 static_cast<int64_t>( era ) * 146097LL +
125 static_cast<std::int64_t>( doe ) - 719468LL;
126
127 using Days = std::chrono::duration<std::int32_t, std::ratio<86400>>;
128 auto const dur =
129 std::chrono::floor<Duration>( std::chrono::nanoseconds( nano ) );
130 return std::chrono::time_point<std::chrono::system_clock,
131 Duration>{ } +
132 ( Days( days_since_epoch ) + std::chrono::hours( h ) +
133 std::chrono::minutes( min ) +
134 std::chrono::seconds( static_cast<std::uint32_t>( s ) ) +
135 dur );
136 };
137 // Not all clocks have the same epoch. This should account for the
138 // offset and adjust the time_point so that the days prior are in
139 // relation to unix epoch. If system_clock is used, as is the default
140 // for the return value, it will be zero and should be removed by the
141 // compiler
142 auto result = calc( yr, mo, dy, hr, mn, se, ns );
143
144 if constexpr( std::is_same_v<Clock, std::chrono::system_clock> ) {
145 return result;
146 } else {
147#if defined( __cpp_lib_chrono ) and __cpp_lib_chrono >= 201907
148 // We have clock_cast
149 auto const match_duration =
150 std::chrono::time_point_cast<Duration>( result );
151 auto const match_clock =
152 std::chrono::clock_cast<Clock>( match_duration );
153 return match_clock;
154#else
155 // This is a guess and will not be constexpr
156
157 // System epoch is unix epoch on(gcc/clang/msvc)
158 auto const system_epoch = std::chrono::floor<std::chrono::hours>(
159 std::chrono::system_clock::now( ).time_since_epoch( ) +
160 std::chrono::minutes( 30 ) );
161 auto const clock_epoch = std::chrono::floor<std::chrono::hours>(
162 Clock::now( ).time_since_epoch( ) + std::chrono::minutes( 30 ) );
163
164 DAW_CPP23_STATIC_LOCAL constexpr auto offset =
165 std::chrono::duration_cast<std::chrono::milliseconds>(
166 clock_epoch - system_epoch );
167 return std::chrono::duration_cast<Duration>( result + offset );
168#endif
169 }
170 }
171
172 struct date_parts {
173 std::int32_t year;
174 std::uint32_t month;
175 std::uint32_t day;
176 };
177
178 constexpr date_parts
179 parse_iso_8601_date( daw::string_view timestamp_str ) {
180 auto result = date_parts{ 0, 0, 0 };
181 result.day = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
182 std::data( timestamp_str.pop_back( 2U ) ) );
183 daw_json_ensure( result.day >= 1 and result.day <= 31,
184 ErrorReason::InvalidTimestamp );
185 if( not parse_utils::is_number( timestamp_str.back( ) ) ) {
186 timestamp_str.remove_suffix( );
187 }
188 result.month = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
189 std::data( timestamp_str.pop_back( 2U ) ) );
190 daw_json_ensure( result.month >= 1 and result.month <= 12,
191 ErrorReason::InvalidTimestamp );
192 if( not parse_utils::is_number( timestamp_str.back( ) ) ) {
193 timestamp_str.remove_suffix( );
194 }
195 result.year =
196 datetime_details::parse_number<std::int_least32_t>( timestamp_str );
197 return result;
198 }
199
200 struct time_parts {
201 std::uint_least32_t hour;
202 std::uint_least32_t minute;
203 std::uint_least32_t second;
204 std::uint64_t nanosecond;
205 };
206
207 constexpr time_parts
208 parse_iso_8601_time( daw::string_view timestamp_str ) {
209 auto result = time_parts{ 0, 0, 0, 0 };
210 result.hour = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
211 std::data( timestamp_str.pop_front( 2 ) ) );
212 daw_json_ensure( result.hour <= 24, ErrorReason::InvalidTimestamp );
213 if( not parse_utils::is_number( timestamp_str.front( ) ) ) {
214 timestamp_str.remove_prefix( );
215 }
216 result.minute = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
217 std::data( timestamp_str.pop_front( 2 ) ) );
218 daw_json_ensure( result.minute <= 59, ErrorReason::InvalidTimestamp );
219 if( timestamp_str.empty( ) ) {
220 return result;
221 }
222 if( not parse_utils::is_number( timestamp_str.front( ) ) ) {
223 timestamp_str.remove_prefix( );
224 }
225 result.second = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
226 std::data( timestamp_str.pop_front( 2 ) ) );
227 daw_json_ensure( result.second <= 60, ErrorReason::InvalidTimestamp );
228 if( timestamp_str.empty( ) ) {
229 return result;
230 }
231 if( not parse_utils::is_number( timestamp_str.front( ) ) ) {
232 timestamp_str.remove_prefix( );
233 }
234 auto const nanosecond_str = timestamp_str.substr(
235 0, std::min( timestamp_str.size( ), std::size_t{ 9 } ) );
236 result.nanosecond =
237 datetime_details::parse_number<std::uint64_t>( nanosecond_str );
238 result.nanosecond *= daw::cxmath::pow10( 9 - timestamp_str.size( ) );
239 return result;
240 }
241
242 template<typename TP>
243 constexpr TP parse_iso8601_timestamp( daw::string_view ts ) {
244 DAW_CPP23_STATIC_LOCAL constexpr daw::string_view t_str = "T";
245 auto const date_str = ts.pop_front_until( t_str );
246 if( ts.empty( ) ) {
248 ErrorReason::InvalidTimestamp ); // Invalid timestamp,
249 // missing T separator
250 }
251
252 date_parts const ymd = parse_iso_8601_date( date_str );
253 auto time_str =
254 ts.pop_front_until( []( char c ) DAW_JSON_CPP23_STATIC_CALL_OP {
255 return not( parse_utils::is_number( c ) | ( c == ':' ) |
256 ( c == '.' ) );
257 } );
258 // TODO: verify or parse timezone
259 auto hms = parse_iso_8601_time( time_str );
260 if( not( ts.empty( ) or ts.front( ) == 'Z' ) ) {
261 daw_json_ensure( std::size( ts ) == 5 or std::size( ts ) == 6,
262 ErrorReason::InvalidTimestamp );
263 // The format will be (+|-)hh[:]mm
264 bool sign = false;
265 daw_json_ensure( not ts.empty( ), ErrorReason::InvalidTimestamp );
266 switch( ts.front( ) ) {
267 case '+':
268 sign = true;
269 break;
270 case '-':
271 break;
272 default:
273 daw_json_error( daw::json::ErrorReason::InvalidTimestamp );
274 }
275 ts.remove_prefix( );
276 auto hr_offset = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
277 std::data( ts ) );
278 daw_json_ensure( hr_offset <= 24,
279 daw::json::ErrorReason::InvalidTimestamp );
280 if( ts.front( ) == ':' ) {
281 ts.remove_prefix( );
282 }
283 auto mn_offset = parse_utils::parse_unsigned<std::uint_least32_t, 2>(
284 std::data( ts ) );
285 daw_json_ensure( mn_offset <= 61,
286 daw::json::ErrorReason::InvalidTimestamp );
287 // Want to subtract offset from current time, we are converting to UTC
288 if( sign ) {
289 // Positive offset
290 hms.hour -= hr_offset;
291 hms.minute -= mn_offset;
292 } else {
293 // Negative offset
294 hms.hour += hr_offset;
295 hms.minute += mn_offset;
296 }
297 }
298 return civil_to_time_point<TP>( ymd.year,
299 ymd.month,
300 ymd.day,
301 hms.hour,
302 hms.minute,
303 hms.second,
304 hms.nanosecond );
305 }
306 struct ymdhms {
307 std::int_least32_t year;
308 std::uint_least32_t month;
309 std::uint_least32_t day;
310 std::uint_least32_t hour;
311 std::uint_least32_t minute;
312 std::uint_least32_t second;
313 std::uint64_t nanosecond;
314 };
315
316 template<typename Clock, typename Duration>
317 constexpr ymdhms
318 time_point_to_civil( std::chrono::time_point<Clock, Duration> const tp ) {
319 auto dur_from_epoch = tp.time_since_epoch( );
320 using Days =
321 std::chrono::duration<std::int_least32_t, std::ratio<86400>>;
322 auto const days_since_epoch =
323 std::chrono::duration_cast<Days>( dur_from_epoch );
324 std::int_least32_t z = days_since_epoch.count( );
325 z += 719468;
326 std::int_least32_t const era = ( z >= 0 ? z : z - 146096 ) / 146097;
327 auto const doe =
328 static_cast<std::uint_least32_t>( z - era * 146097 ); // [0, 146096]
329 std::uint_least32_t const yoe =
330 ( doe - doe / 1460 + doe / 36524 - doe / 146096 ) / 365; // [0, 399]
331 std::int_least32_t const y =
332 static_cast<std::int_least32_t>( yoe ) + era * 400;
333 std::uint_least32_t const doy =
334 doe - ( 365 * yoe + yoe / 4 - yoe / 100 ); // [0, 365]
335 std::uint_least32_t const mp = ( 5 * doy + 2 ) / 153; // [0, 11]
336 std::uint_least32_t const d = doy - ( 153 * mp + 2 ) / 5 + 1; // [1, 31]
337 auto const m = static_cast<std::uint_least32_t>(
338 static_cast<std::int_least32_t>( mp ) +
339 ( static_cast<std::int_least32_t>( mp ) < 10 ? 3 : -9 ) ); // [1, 12]
340
341 dur_from_epoch -= days_since_epoch;
342 auto const hrs =
343 std::chrono::duration_cast<std::chrono::hours>( dur_from_epoch );
344 dur_from_epoch -= hrs;
345 auto const min =
346 std::chrono::duration_cast<std::chrono::minutes>( dur_from_epoch );
347 dur_from_epoch -= min;
348 auto const sec =
349 std::chrono::duration_cast<std::chrono::seconds>( dur_from_epoch );
350 dur_from_epoch -= sec;
351 auto const dur = std::chrono::duration_cast<std::chrono::nanoseconds>(
352 dur_from_epoch );
353 return ymdhms{ y + ( m <= 2 ),
354 m,
355 d,
356 static_cast<std::uint_least32_t>( hrs.count( ) ),
357 static_cast<std::uint_least32_t>( min.count( ) ),
358 static_cast<std::uint_least32_t>( sec.count( ) ),
359 static_cast<std::uint64_t>( dur.count( ) ) };
360 }
361
362 constexpr std::string_view month_short_name( unsigned m ) {
363 switch( m ) {
364 case 1:
365 return { "Jan" };
366 case 2:
367 return { "Feb" };
368 case 3:
369 return { "Mar" };
370 case 4:
371 return { "Apr" };
372 case 5:
373 return { "May" };
374 case 6:
375 return { "Jun" };
376 case 7:
377 return { "Jul" };
378 case 8:
379 return { "Aug" };
380 case 9:
381 return { "Sep" };
382 case 10:
383 return { "Oct" };
384 case 11:
385 return { "Nov" };
386 case 12:
387 return { "Dec" };
388 default:
389 DAW_UNLIKELY_BRANCH
390 daw_json_error( ErrorReason::InvalidTimestamp ); // Invalid month
391 }
392 }
393
394 // Formula from
395 // http://howardhinnant.github.io/date_algorithms.html#weekday_from_days
396 template<typename Duration>
397 constexpr std::string_view short_day_of_week(
398 std::chrono::time_point<std::chrono::system_clock, Duration> tp ) {
399 using days = std::chrono::duration<long, std::ratio<86400>>;
400 auto const z =
401 std::chrono::duration_cast<days>( tp.time_since_epoch( ) ).count( );
402 auto const dow = z >= -4L ? ( z + 4L ) % 7L : ( z + 5L ) % 7L + 6L;
403 switch( dow ) {
404 case 0:
405 return { "Sun" };
406 case 1:
407 return { "Mon" };
408 case 2:
409 return { "Tue" };
410 case 3:
411 return { "Wed" };
412 case 4:
413 return { "Thu" };
414 case 5:
415 return { "Fri" };
416 case 6:
417 return { "Sat" };
418 default:
419 DAW_UNLIKELY_BRANCH
420 daw_json_error( ErrorReason::InvalidTimestamp ); // Invalid month
421 }
422 }
423
424 namespace datetime_details {
425 constexpr std::uint_least32_t month2num( std::string_view ts ) {
426 daw_json_ensure( std::size( ts ) >= 3,
427 ErrorReason::InvalidTimestamp );
428 auto const b0 = static_cast<std::uint_least32_t>(
429 static_cast<unsigned char>( ts[0] ) );
430 auto const b1 = static_cast<std::uint_least32_t>(
431 static_cast<unsigned char>( ts[1] ) );
432 auto const b2 = static_cast<std::uint_least32_t>(
433 static_cast<unsigned char>( ts[2] ) );
434 return ( b0 << 16U ) | ( b1 << 8U ) | b2;
435 }
436 } // namespace datetime_details
437
438 constexpr unsigned parse_short_month( std::string_view ts ) {
439 // Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
440 switch( datetime_details::month2num( ts ) ) {
441 case datetime_details::month2num( "Jan" ):
442 return 1;
443 case datetime_details::month2num( "Feb" ):
444 return 2;
445 case datetime_details::month2num( "Mar" ):
446 return 3;
447 case datetime_details::month2num( "Apr" ):
448 return 4;
449 case datetime_details::month2num( "May" ):
450 return 5;
451 case datetime_details::month2num( "Jun" ):
452 return 6;
453 case datetime_details::month2num( "Jul" ):
454 return 7;
455 case datetime_details::month2num( "Aug" ):
456 return 8;
457 case datetime_details::month2num( "Sep" ):
458 return 9;
459 case datetime_details::month2num( "Oct" ):
460 return 10;
461 case datetime_details::month2num( "Nov" ):
462 return 11;
463 case datetime_details::month2num( "Dec" ):
464 return 12;
465 default:
466 DAW_UNLIKELY_BRANCH
467 daw_json_error( ErrorReason::InvalidTimestamp ); // Invalid month
468 }
469 }
470 } // namespace datetime
471 } // namespace DAW_JSON_VER
472} // namespace daw::json
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_JSON_CPP23_STATIC_CALL_OP
DAW_ATTRIB_NOINLINE void daw_json_error(ErrorReason reason)
constexpr std::string_view short_day_of_week(std::chrono::time_point< std::chrono::system_clock, Duration > tp)
constexpr TP civil_to_time_point(std::int32_t yr, std::uint32_t mo, std::uint32_t dy, std::uint32_t hr, std::uint32_t mn, std::uint32_t se, std::uint64_t ns)
constexpr ymdhms time_point_to_civil(std::chrono::time_point< Clock, Duration > const tp)
constexpr Result parse_unsigned(daw::not_null< char const * > digit_str)
constexpr Result parse_unsigned2(daw::not_null< char const * > digit_str)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20