Parkmanne/Park_time.cpp
TinyAtoms 8160f84e20 db
2019-06-29 15:23:38 -03:00

56 lines
1.6 KiB
C++

#include"headers/Park_time.h"
#include <iostream>
#include <ctime>
Park_time::Park_time(int c_id, int s_id)
: customer_id { c_id }
, spot_id { s_id }
, duration { 0 }
, start { high_resolution_clock::now() }
{
}
void Park_time::clock_out(int c_id, int s_id)
{
if (c_id != customer_id) {
std::cout << "wrong customer id, you are at the wrong location";
return;
}
if (s_id != spot_id) {
std::cout << "Wrong spot id, you're at the wrong location";
return;
}
if (!duration) {
end = high_resolution_clock::now();
duration = duration_cast<seconds>(end - start).count(); // use mins later
} else {
std::cout << "Already clocked out. Something is wrong \n";
}
}
std::ostream& operator<<(std::ostream& os, const Park_time & pt){
std::time_t start_ = system_clock::to_time_t(pt.start);
std::time_t end_ = system_clock::to_time_t(pt.end);
os << "- - - - - - - - - - - - - - - - - - - -\n";
os << "Clocked in :" << std::ctime(&start_);
os << "clocked out : " << std::ctime(&end_);
os << "duration : " << pt.duration << "\n";
os << "- - - - - - - - - - - - - - - - - - - -\n";
return os;
}
void Park_time::debug() {
auto start_to_epoch = start.time_since_epoch();
auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int start_seconds = start_value.count();
auto end_to_epoch = end.time_since_epoch();
auto end_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int end_seconds = end_value.count();
std::cout << "<" << start_seconds << "-" << end_seconds << ">" ;
}