aboutsummaryrefslogtreecommitdiff
path: root/src/db.cpp
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-04-15 10:37:45 -0400
committerCara Salter <cara@devcara.com>2023-04-15 10:37:45 -0400
commit5058c8bbeff6b659787a8ab482ac89fc4a194076 (patch)
treeef768f7aa54482469c38e5bb818f96740dae85a0 /src/db.cpp
parent8edf3bf0ca81b942a165cde3bdea11ca755da955 (diff)
download142bot-5058c8bbeff6b659787a8ab482ac89fc4a194076.tar.gz
142bot-5058c8bbeff6b659787a8ab482ac89fc4a194076.zip
init spotify
Change-Id: I6796976649ee59f4e647148e77d76effa7315f73
Diffstat (limited to 'src/db.cpp')
-rw-r--r--src/db.cpp65
1 files changed, 65 insertions, 0 deletions
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 <iomanip>
+#include <sstream>
+#include <stdexcept> // 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