From 5058c8bbeff6b659787a8ab482ac89fc4a194076 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Sat, 15 Apr 2023 10:37:45 -0400 Subject: init spotify Change-Id: I6796976649ee59f4e647148e77d76effa7315f73 --- src/db.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'src/db.cpp') diff --git a/src/db.cpp b/src/db.cpp index cea15c0..73a7f02 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -81,3 +81,68 @@ namespace db { } } + +#include <142bot/date.h> + +#include <142bot/iso_week.h> +#include +#include +#include // std::invalid_argument + +namespace asdf +{ + timestamp from_iso8601_str( const std::string& s ) + { + timestamp ts; + if( !from_iso8601_str( s, ts ) ) + throw std::invalid_argument{ + "failed to parse " + + s + + " as an ISO 8601 timestamp" + }; + return ts; + } + + bool from_iso8601_str( const std::string& s, timestamp& ts ) + { + std::istringstream stream{ s }; + stream >> date::parse( "%F %T%z", ts ); + return !stream.fail(); + } + + std::string to_iso8601_str( const timestamp& ts ) + { + return date::format( "%F %T%z", ts ); + } + + std::string to_http_ts_str( const timestamp& ts ) + { + std::stringstream weekday_abbreviation; + weekday_abbreviation << static_cast< iso_week::year_weeknum_weekday >( + std::chrono::time_point_cast< date::days >( ts ) + ).weekday(); + + return ( + weekday_abbreviation.str() + // timestamps serialize to UTC/GMT by default + + date::format( + " %d-%m-%Y %H:%M:%S GMT", + std::chrono::time_point_cast< std::chrono::seconds >( ts ) + ) + ); + } + + timestamp from_unix_time( unsigned int unix_time ) + { + return timestamp{ std::chrono::duration_cast< + std::chrono::microseconds + >( std::chrono::seconds{ unix_time } ) }; + } + + unsigned int to_unix_time( const timestamp& ts ) + { + return std::chrono::duration_cast< + std::chrono::seconds + >( ts.time_since_epoch() ).count(); + } +} \ No newline at end of file -- cgit v1.2.3