summaryrefslogtreecommitdiff
path: root/src/utils/datetime.hpp
blob: 27511f7224f80fbaed3dd6dfb4fa6a6d72ee0547 (plain)
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
#pragma once

#include <chrono>
#include <string>

#include <logger/logger.hpp>

class DateTime
{
public:
  enum class Engine {
    Postgresql,
    Sqlite3,
  } engine{Engine::Sqlite3};

  using time_point = std::chrono::system_clock::time_point;

  DateTime():
      s{},
      t{}
  { }

  DateTime(std::time_t t):
      t(std::chrono::seconds(t))
  {}

  DateTime(std::string s):
      s(std::move(s))
  {}

  DateTime& operator=(const std::string& s)
  {
    this->s = s;
    return *this;
  }

  DateTime& operator=(const time_point t)
  {
    this->t = t;
    return *this;
  }

  const std::string& to_string() const
  {
    return this->s;
  }

  time_point::duration epoch() const
  {
    return this->t.time_since_epoch();
  }

  long double julianday() const
  {
    log_debug("ici?");
    auto res = ((static_cast<long double>(this->epoch().count()) / std::chrono::system_clock::period::den) / 86400) + 2440587.5;
    return res;
  }

private:
  std::string s;
  time_point t;
};

inline long double to_julianday(std::time_t t)
{
  return static_cast<long double>(t) / 86400.0 + 2440587.5;
}