aboutsummaryrefslogtreecommitdiffstats
path: root/util/draft/date.h
blob: 13d86032b7a6026ceab51f8115ecfceba46f6c3a (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once

#include <util/stream/output.h> 
#include <util/stream/input.h> 
#include <util/generic/string.h>
#include <util/datetime/constants.h>

#include <ctime>

// XXX: uses system calls for trivial things. may be very slow therefore.

time_t GetDateStart(time_t ts);

// Local date (without time zone)
class TDate {
    // XXX: wrong: must store number of days since epoch
    time_t Timestamp;

public:
    TDate()
        : Timestamp(0)
    {
    }

    // XXX: wrong. Should be replace with two methods: TodayGmt() and TodayLocal()
    static TDate Today() {
        return TDate(time(nullptr));
    }

    TDate(const char* yyyymmdd);
    TDate(const TString& yyyymmdd);
    TDate(unsigned year, unsigned month, unsigned monthDay); // month from 01, monthDay from 01
    TDate(const TString& date, const TString& format);

    explicit TDate(time_t t);

    time_t GetStart() const {
        return Timestamp;
    }

    time_t GetStartUTC() const;

    TString ToStroka(const char* format = "%Y%m%d") const;

    TDate& operator++() {
        Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2));
        return *this;
    }

    TDate& operator--() {
        Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2);
        return *this;
    }

    TDate& operator+=(unsigned days) {
        Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
        return *this;
    }

    TDate& operator-=(unsigned days) {
        Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
        return *this;
    }

    TDate operator+(unsigned days) const {
        return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
    }

    TDate operator-(unsigned days) const {
        return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
    }

    unsigned GetWeekDay() const; // days since Sunday

    unsigned GetYear() const;
    unsigned GetMonth() const;    // from 01
    unsigned GetMonthDay() const; // from 01

    friend bool operator<(const TDate& left, const TDate& right);
    friend bool operator>(const TDate& left, const TDate& right);
    friend bool operator<=(const TDate& left, const TDate& right);
    friend bool operator>=(const TDate& left, const TDate& right);
    friend bool operator==(const TDate& left, const TDate& right);
    friend int operator-(const TDate& left, const TDate& right);

    friend IInputStream& operator>>(IInputStream& left, TDate& right); 
    friend IOutputStream& operator<<(IOutputStream& left, const TDate& right); 
};

Y_DECLARE_PODTYPE(TDate);

inline bool operator<(const TDate& left, const TDate& right) {
    return left.Timestamp < right.Timestamp;
}

inline bool operator>(const TDate& left, const TDate& right) {
    return left.Timestamp > right.Timestamp;
}

inline bool operator<=(const TDate& left, const TDate& right) {
    return left.Timestamp <= right.Timestamp;
}

inline bool operator>=(const TDate& left, const TDate& right) {
    return left.Timestamp >= right.Timestamp;
}

inline bool operator==(const TDate& left, const TDate& right) {
    return left.Timestamp == right.Timestamp;
}

inline int operator-(const TDate& left, const TDate& right) {
    if (left < right) {
        return -(right - left);
    }
    return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY);
}

inline IInputStream& operator>>(IInputStream& left, TDate& right) { 
    TString stroka;
    left >> stroka;
    TDate date(stroka.c_str());
    right = date;
    return left;
}

inline IOutputStream& operator<<(IOutputStream& left, const TDate& right) { 
    return left << right.ToStroka();
}