-
Notifications
You must be signed in to change notification settings - Fork 16
/
Julian.h
70 lines (59 loc) · 2.71 KB
/
Julian.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include <iostream>
#include <mysql.h>
#include <mysql_time.h>
namespace MySQLWrap {
class JulianException : std::exception {
public:
JulianException(const std::string &msg) : _message(msg) {}
virtual ~JulianException() throw() {}
virtual const char *what() const throw() { return _message.c_str(); }
private:
std::string _message;
};
enum class TimeType { Date, Time, DateTime };
struct GregorianBreakdown {
int year; unsigned int month; unsigned int day;
unsigned int hour; unsigned int minute; unsigned int second; unsigned int millisecond;
int minutes_west_utc;
TimeType time_type;
GregorianBreakdown();
GregorianBreakdown(int year, unsigned int month, unsigned int day,
unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms, int minutes_west_utc);
GregorianBreakdown(int year, unsigned int month, unsigned int day);
GregorianBreakdown(unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms);
GregorianBreakdown(const MYSQL_TIME &time, int minutes_west_utc);
MYSQL_TIME to_mysql_time() const;
};
enum class DayOfWeek : uint8_t { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6 };
std::ostream &operator<<(std::ostream &out, const GregorianBreakdown &gb);
class Julian {
public:
Julian();
Julian(double value, TimeType time_type);
Julian(int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms);
Julian(int year, unsigned int month, unsigned int day);
Julian(unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms);
Julian(const GregorianBreakdown &gb);
Julian(const Julian &) = default;
Julian &operator=(const Julian &) = default;
double Value() const;
TimeType Type() const;
GregorianBreakdown to_gregorian(int minutes_west_utc) const;
DayOfWeek Weekday(int minutes_west_utc) const;
protected:
static double calculate_utc(int year, unsigned int month, unsigned int day,
unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms);
static double calculate_jdn(int year, unsigned int month, unsigned int day);
static double calculate_time(unsigned int hour, unsigned int minute, unsigned int second, unsigned int ms);
private:
double _julian;
TimeType _time_type;
};
std::ostream &operator<<(std::ostream &out, const Julian &julian);
bool operator<(const Julian &left, const Julian &right);
Julian operator+(const Julian &left, const Julian &right);
Julian operator+(const Julian &left, double right);
Julian operator-(const Julian &left, const Julian &right);
Julian operator-(const Julian &left, double right);
}