geostat/cache.cpp

47 wiersze
1.5 KiB
C++
Czysty Zwykły widok Historia

2019-09-08 16:42:10 +00:00
#include "cache.h"
#include <cmath>
#include <ctime>
2019-09-08 16:42:10 +00:00
Position Cache::home;
static float degtorad(float x) {
return x * M_PI / 180;
}
2019-09-10 12:03:01 +00:00
2019-09-08 16:42:10 +00:00
void Cache::show() const {
std::cout << "Cache:\t" << code << ' ' << name << " (type: " << type << ", owned by " << owner << ")\n";
std::cout << '\t' << pos.lat << " " << pos.lon << "\t\tD/T: " << diff << '/' << terr << "\t\tSize: " << size << "\t\tFound on " << date << '\n';
2019-09-08 16:42:10 +00:00
}
std::string Cache::link() const {
return "http://coord.eu/" + code;
}
2019-09-08 16:42:10 +00:00
float Cache::distance() const {
return 2 * Earth_radius * asin(sqrt(pow(sin(degtorad((pos.lat - home.lat) / 2)), 2) + cos(degtorad(pos.lat)) * cos(degtorad(home.lat)) * pow(sin(degtorad((pos.lon - home.lon) / 2)), 2)));
2019-09-10 12:03:01 +00:00
}
2019-09-08 16:42:10 +00:00
2020-01-04 17:26:07 +00:00
void Cache::set_date(const std::tm* t) {
char tmp[20];
date_tm = *t;
2019-11-17 18:42:32 +00:00
std::mktime(&date_tm);
2020-01-04 18:27:04 +00:00
year = std::to_string(1900 + date_tm.tm_year);
std::strftime(tmp, 20, "(%m) %B", &date_tm);
mon = tmp;
day = std::to_string(date_tm.tm_mday);
hour = std::to_string(date_tm.tm_hour);
2019-11-17 18:42:32 +00:00
std::strftime(tmp, 20, "(%u) %A", &date_tm);
day_of_week = tmp;
2019-11-10 19:59:21 +00:00
std::strftime(tmp, 20, "%F", &date_tm);
date = tmp;
2020-01-04 18:27:04 +00:00
//date += std::to_string(date_tm.tm_hour) + ":" + std::to_string(date_tm.tm_min) + ":" + std::to_string(date_tm.tm_sec);
2019-11-10 19:59:21 +00:00
}
float cache_distance(const Cache& a, const Cache& b) {
return 2 * Earth_radius * asin(sqrt(pow(sin(degtorad((a.pos.lat - b.pos.lat) / 2)), 2) + cos(degtorad(a.pos.lat)) * cos(degtorad(b.pos.lat)) * pow(sin(degtorad((a.pos.lon - b.pos.lon) / 2)), 2)));
}