net: tcp_client_socket connection state routines
[quarnos.git] / libs / date.cpp
blob0fadf0ab838af1c3210fead4bd6b9eef6b72a044
1 /* Quarn OS
3 * Date
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "date.h"
25 date::date() : timestamp(-1) {}
27 int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0 };
29 date::date(int ts) {
30 timestamp = ts / (24 * 60 * 60);
33 date::date(int day, int month, int year) {
34 timestamp = (year - 1970) * 365;
35 for (; month - 1 > 0; month--)
36 timestamp += months[month - 1];
37 timestamp += day + (year - 1970) / 4;
40 date::operator int() {
41 return timestamp * 24 * 60 * 60;
44 int date::get_year() const {
45 return 1970 + (timestamp + timestamp / 365 / 4)/ 365;
48 int date::get_month() const {
49 int month = 0;
50 if (get_year() % 4 == 0)
51 months[1]++;
53 for (int i = 0, ts = (timestamp - timestamp / 365 / 4) % 365; ts >= months[i]; i++) {
54 month++;
55 ts -= months[i];
58 if (get_year() % 4 == 0)
59 months[1]--;
61 return month + 1;
64 int date::get_day() const {
65 int month = 0;
66 if (get_year() % 4 == 0)
67 months[1]++;
68 int ts = (timestamp - (timestamp / 365 + 2) / 4) % 365;
69 for (int i = 0; ts >= months[i]; i++) {
70 month++;
71 ts -= months[i];
74 if (get_year() % 4 == 0)
75 months[1]--;
76 _ts = ts;
78 return ts + 1;
81 time::time() : timestamp(-1) {}
83 time::time(int ts) {
84 timestamp = ts % (24 * 60 * 60);
87 time::time(int hour, int minute, int second) {
88 timestamp = hour * 3600 + minute * 60 + second;
91 time::operator int() {
92 return timestamp;
95 int time::get_second() const {
96 return timestamp % 60;
99 int time::get_minute() const {
100 return timestamp / 60 % 60;
103 int time::get_hour() const {
104 return timestamp / 3600;