Skip to content

Commit 6830943

Browse files
ProsperohProsperohThalhammer
authored
Fix as_date()'s std::bad_cast with decimal value (#240)
Signed-off-by: Dominik Thalhammer <dominik@thalhammer.it> Co-authored-by: Prosperoh <prosperoh@protonmail.com> Co-authored-by: Dominik Thalhammer <dominik@thalhammer.it>
1 parent 3ed4ff9 commit 6830943

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

include/jwt-cpp/jwt.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <algorithm>
2525
#include <chrono>
26+
#include <cmath>
2627
#include <codecvt>
2728
#include <functional>
2829
#include <iterator>
@@ -2253,11 +2254,18 @@ namespace jwt {
22532254
typename json_traits::string_type as_string() const { return json_traits::as_string(val); }
22542255

22552256
/**
2256-
* Get the contained JSON value as a date
2257+
* \brief Get the contained JSON value as a date
2258+
*
2259+
* If the value is a decimal, it is rounded up to the closest integer
2260+
*
22572261
* \return content as date
22582262
* \throw std::bad_cast Content was not a date
22592263
*/
2260-
date as_date() const { return std::chrono::system_clock::from_time_t(as_int()); }
2264+
date as_date() const {
2265+
using std::chrono::system_clock;
2266+
if (get_type() == json::type::number) return system_clock::from_time_t(std::round(as_number()));
2267+
return system_clock::from_time_t(as_int());
2268+
}
22612269

22622270
/**
22632271
* Get the contained JSON value as an array

0 commit comments

Comments
 (0)