The Pedigree Project 0.1
Conversion.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/processor/types.h"
21#include "pedigree/kernel/time/Time.h"
22#include "pedigree/kernel/utilities/assert.h"
23
24static const uint16_t cumulativeDays[] = {0, 31, 59, 90, 120, 151, 181,
25 212, 243, 273, 304, 334, 365};
26
27namespace Time {
28namespace Conversion {
29Timestamp toUnix(size_t second, size_t minute, size_t hour, size_t dom, size_t month, size_t year) {
30 if (year < 1970 || month < 1 || month > 12 || dom < 1 || dom > 31) {
31 assert(false);
32 return 0;
33 }
34 assert(year >= 1970);
35 assert(month >= 1 && month <= 12);
36 assert(dom >= 1 && dom <= 31);
37
38 --dom;
39
40 // # of leap days.
41 size_t leaps = (year / 4) - (year / 100) + (year / 400);
42 // We only care about leap days since the epoch.
43 leaps -= (1970 / 4) - (1970 / 100) + (1970 / 400);
44 // # of days so far this year.
45 size_t cumuldays = cumulativeDays[month - 1];
46
47 Time::Timestamp result = 0;
48 result += second;
49 result += minute * 60;
50 result += hour * 60 * 60;
51 result += dom * 24 * 60 * 60;
52 result += cumuldays * 24 * 60 * 60;
53 result += leaps * 24 * 60 * 60;
54 result += (year - 1970) * 365 * 24 * 60 * 60;
55 return result;
56}
57
58} // namespace Conversion
59
60} // namespace Time