|
| 1 | +/* |
| 2 | +* xtime_cpp - Library for work with time. |
| 3 | +* |
| 4 | +* Copyright (c) 2018 Yaroslav Barabanov. Email: elektroyar@yandex.ru |
| 5 | +* |
| 6 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +* of this software and associated documentation files (the "Software"), to deal |
| 8 | +* in the Software without restriction, including without limitation the rights |
| 9 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +* copies of the Software, and to permit persons to whom the Software is |
| 11 | +* furnished to do so, subject to the following conditions: |
| 12 | +* |
| 13 | +* The above copyright notice and this permission notice shall be included in |
| 14 | +* all copies or substantial portions of the Software. |
| 15 | +* |
| 16 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +* SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "xtime.hpp" |
| 26 | + |
| 27 | +namespace FunctionsForTime { |
| 28 | + |
| 29 | + unsigned long long getUnixTime(int day, int month, int year, int hour, int minutes, int seconds) { |
| 30 | + unsigned long long _secs; |
| 31 | + long _mon, _year; |
| 32 | + long _days; |
| 33 | + _mon = month - 1; |
| 34 | + const long _TBIAS_YEAR = 1900; |
| 35 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 36 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 37 | + _year = year - _TBIAS_YEAR; |
| 38 | + _days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1; |
| 39 | + _days += 365 * _year; |
| 40 | + _days += day; |
| 41 | + const long _TBIAS_DAYS = 25567; |
| 42 | + _days -= _TBIAS_DAYS; |
| 43 | + _secs = 3600 * hour; |
| 44 | + _secs += 60 * minutes; |
| 45 | + _secs += seconds; |
| 46 | + _secs += _days * 86400; |
| 47 | + return _secs; |
| 48 | + } |
| 49 | + |
| 50 | + cTime::cTime() {}; |
| 51 | + |
| 52 | + cTime::cTime(int day, int month, int year) { |
| 53 | + cTime::day = day; |
| 54 | + cTime::month = month; |
| 55 | + cTime::year = year; |
| 56 | + cTime::hour = 0; |
| 57 | + cTime::minutes = 0; |
| 58 | + cTime::seconds = 0; |
| 59 | + } |
| 60 | + |
| 61 | + cTime::cTime(int day, int month, int year, int hour, int minutes, int seconds) { |
| 62 | + cTime::day = day; |
| 63 | + cTime::month = month; |
| 64 | + cTime::year = year; |
| 65 | + cTime::hour = hour; |
| 66 | + cTime::minutes = minutes; |
| 67 | + cTime::seconds = seconds; |
| 68 | + } |
| 69 | + |
| 70 | + cTime::cTime(unsigned long long timestamp) { |
| 71 | + unsigned long long _secs; |
| 72 | + long _mon, _year; |
| 73 | + long _days; |
| 74 | + long i; |
| 75 | + |
| 76 | + _secs = timestamp; |
| 77 | + const long _TBIAS_DAYS = 25567; |
| 78 | + _days = _TBIAS_DAYS; |
| 79 | + |
| 80 | + _days += _secs / 86400; _secs = _secs % 86400; |
| 81 | + hour = _secs / 3600; _secs %= 3600; |
| 82 | + minutes = _secs / 60; seconds = _secs % 60; |
| 83 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 84 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 85 | + |
| 86 | + for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; } |
| 87 | + _days -= i; |
| 88 | + const long _TBIAS_YEAR = 1900; |
| 89 | + year = _year + _TBIAS_YEAR; |
| 90 | + |
| 91 | + if(((_year) & 03) || ((_year) == 0)) { |
| 92 | + // mos |
| 93 | + for (_mon = 12; _days < mos[--_mon]; ); |
| 94 | + month = _mon + 1; |
| 95 | + day = _days - mos[_mon] + 1; |
| 96 | + } else { |
| 97 | + for (_mon = 12; _days < lmos[--_mon]; ); |
| 98 | + month = _mon + 1; |
| 99 | + day = _days - lmos[_mon] + 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + unsigned long long cTime::getUnixTime() { |
| 104 | + unsigned long long _secs; |
| 105 | + long _mon, _year; |
| 106 | + long _days; |
| 107 | + _mon = month - 1; |
| 108 | + const long _TBIAS_YEAR = 1900; |
| 109 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 110 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 111 | + _year = year - _TBIAS_YEAR; |
| 112 | + _days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1; |
| 113 | + _days += 365 * _year; |
| 114 | + _days += day; |
| 115 | + const long _TBIAS_DAYS = 25567; |
| 116 | + _days -= _TBIAS_DAYS; |
| 117 | + _secs = 3600 * hour; |
| 118 | + _secs += 60 * minutes; |
| 119 | + _secs += seconds; |
| 120 | + _secs += _days * 86400; |
| 121 | + return _secs; |
| 122 | + } |
| 123 | + |
| 124 | + void cTime::setUnixTime(unsigned long long timestamp) { |
| 125 | + unsigned long long _secs; |
| 126 | + long _mon, _year; |
| 127 | + long _days; |
| 128 | + long i; |
| 129 | + |
| 130 | + _secs = timestamp; |
| 131 | + const long _TBIAS_DAYS = 25567; |
| 132 | + _days = _TBIAS_DAYS; |
| 133 | + |
| 134 | + _days += _secs / 86400; _secs = _secs % 86400; |
| 135 | + hour = _secs / 3600; _secs %= 3600; |
| 136 | + minutes = _secs / 60; seconds = _secs % 60; |
| 137 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 138 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 139 | + |
| 140 | + for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; } |
| 141 | + _days -= i; |
| 142 | + const long _TBIAS_YEAR = 1900; |
| 143 | + year = _year + _TBIAS_YEAR; |
| 144 | + |
| 145 | + if(((_year) & 03) || ((_year) == 0)) { |
| 146 | + // mos |
| 147 | + for (_mon = 12; _days < mos[--_mon]; ); |
| 148 | + month = _mon + 1; |
| 149 | + day = _days - mos[_mon] + 1; |
| 150 | + } else { |
| 151 | + for (_mon = 12; _days < lmos[--_mon]; ); |
| 152 | + month = _mon + 1; |
| 153 | + day = _days - lmos[_mon] + 1; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + unsigned long long getUnixTime(cTime& timedata) { |
| 158 | + unsigned long long _secs; |
| 159 | + long _mon, _year; |
| 160 | + long _days; |
| 161 | + _mon = timedata.month - 1; |
| 162 | + const long _TBIAS_YEAR = 1900; |
| 163 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 164 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 165 | + _year = timedata.year - _TBIAS_YEAR; |
| 166 | + _days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1; |
| 167 | + _days += 365 * _year; |
| 168 | + _days += timedata.day; |
| 169 | + const long _TBIAS_DAYS = 25567; |
| 170 | + _days -= _TBIAS_DAYS; |
| 171 | + _secs = 3600 * timedata.hour; |
| 172 | + _secs += 60 * timedata.minutes; |
| 173 | + _secs += timedata.seconds; |
| 174 | + _secs += _days * 86400; |
| 175 | + return _secs; |
| 176 | + } |
| 177 | + |
| 178 | + cTime getTime(unsigned long long timestamp) { |
| 179 | + cTime outTime; |
| 180 | + unsigned long long _secs; |
| 181 | + long _mon, _year; |
| 182 | + long _days; |
| 183 | + long i; |
| 184 | + |
| 185 | + _secs = timestamp; |
| 186 | + const long _TBIAS_DAYS = 25567; |
| 187 | + _days = _TBIAS_DAYS; |
| 188 | + |
| 189 | + _days += _secs / 86400; _secs = _secs % 86400; |
| 190 | + outTime.hour = _secs / 3600; _secs %= 3600; |
| 191 | + outTime.minutes = _secs / 60; outTime.seconds = _secs % 60; |
| 192 | + const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 193 | + const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 194 | + |
| 195 | + for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; } |
| 196 | + _days -= i; |
| 197 | + const long _TBIAS_YEAR = 1900; |
| 198 | + outTime.year = _year + _TBIAS_YEAR; |
| 199 | + |
| 200 | + if(((_year) & 03) || ((_year) == 0)) { |
| 201 | + // mos |
| 202 | + for (_mon = 12; _days < mos[--_mon]; ); |
| 203 | + outTime.month = _mon + 1; |
| 204 | + outTime.day = _days - mos[_mon] + 1; |
| 205 | + } else { |
| 206 | + for (_mon = 12; _days < lmos[--_mon]; ); |
| 207 | + outTime.month = _mon + 1; |
| 208 | + outTime.day = _days - lmos[_mon] + 1; |
| 209 | + } |
| 210 | + return outTime; |
| 211 | + } |
| 212 | + |
| 213 | + int getWday(int day, int month, int year) { |
| 214 | + int a, y, m, R; |
| 215 | + a = ( 14 - month ) / 12; |
| 216 | + y = year - a; |
| 217 | + m = month + 12 * a - 2; |
| 218 | + R = 7000 + ( day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12 ); |
| 219 | + return R % 7; |
| 220 | + } |
| 221 | + |
| 222 | + int getWday(unsigned long long unix) { |
| 223 | + cTime temp = getTime(unix); |
| 224 | + return getWday(temp.day, temp.month, temp.year); |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments