mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
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>
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <codecvt>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
@@ -2253,11 +2254,18 @@ namespace jwt {
|
||||
typename json_traits::string_type as_string() const { return json_traits::as_string(val); }
|
||||
|
||||
/**
|
||||
* Get the contained JSON value as a date
|
||||
* \brief Get the contained JSON value as a date
|
||||
*
|
||||
* If the value is a decimal, it is rounded up to the closest integer
|
||||
*
|
||||
* \return content as date
|
||||
* \throw std::bad_cast Content was not a date
|
||||
*/
|
||||
date as_date() const { return std::chrono::system_clock::from_time_t(as_int()); }
|
||||
date as_date() const {
|
||||
using std::chrono::system_clock;
|
||||
if (get_type() == json::type::number) return system_clock::from_time_t(std::round(as_number()));
|
||||
return system_clock::from_time_t(as_int());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contained JSON value as an array
|
||||
|
||||
Reference in New Issue
Block a user