feat[prep]: move time function into a module

This commit is contained in:
Jorens Shtekels 2024-03-10 18:59:20 +02:00
parent 42d1f3b908
commit 0a61c08a0e
5 changed files with 35 additions and 12 deletions

View File

@ -1 +1 @@
add_library(prepLib prep.cpp prep.hh)
add_library(prepLib prep.cpp prep.hh timeUtils.cpp timeUtils.hh)

View File

@ -1,5 +1,14 @@
#include "prep.hh"
#include "timeUtils.hh"
int add(int a, int b) {
return a + b;
}
Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status):
id(id),
title(title),
status(status) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt);
}

View File

@ -61,17 +61,7 @@ struct Room {
std::tm *utcTimestampCreatedAt;
RoomStatus status;
Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt,
RoomStatus status)
: id(id), title(title), status(status) {
// Convert the timestamp into a time_point object
std::chrono::seconds sec(utcTimestampCreatedAt);
std::chrono::time_point<std::chrono::system_clock> tp(sec);
// Convert to time_t for formatting
std::time_t time_object = std::chrono::system_clock::to_time_t(tp);
// Convert to tm as UTC time
this->utcTimestampCreatedAt = std::gmtime(&time_object);
}
Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status);
};
struct Event {

15
src/prep/timeUtils.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "timeUtils.hh"
#include <chrono>
#include <cstdint>
#include <ctime>
std::tm *createUTCTimestamp(uint32_t timestamp) {
// Convert the timestamp into a time_point object
std::chrono::seconds sec(timestamp);
std::chrono::time_point<std::chrono::system_clock> tp(sec);
// Convert to time_t for formatting
std::time_t time_object = std::chrono::system_clock::to_time_t(tp);
// Convert to tm as UTC time
return std::gmtime(&time_object);
}

9
src/prep/timeUtils.hh Normal file
View File

@ -0,0 +1,9 @@
#include <cstdint>
#include <ctime>
#ifndef TIMEUTILS_H
# define TIMEUTILS_H
std::tm *createUTCTimestamp(uint32_t timestamp);
#endif