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 
24 static const uint16_t cumulativeDays[] = {0, 31, 59, 90, 120, 151, 181,
25  212, 243, 273, 304, 334, 365};
26 
27 namespace Time
28 {
29 namespace Conversion
30 {
31 Timestamp toUnix(
32  size_t second, size_t minute, size_t hour, size_t dom, size_t month,
33  size_t year)
34 {
35  assert(year >= 1970);
36  assert(month >= 1 && month <= 12);
37  assert(dom >= 1 && dom <= 31);
38 
39  --dom;
40 
41  // # of leap days.
42  size_t leaps = (year / 4) - (year / 100) + (year / 400);
43  // We only care about leap days since the epoch.
44  leaps -= (1970 / 4) - (1970 / 100) + (1970 / 400);
45  // # of days so far this year.
46  size_t cumuldays = cumulativeDays[month - 1];
47 
48  Time::Timestamp result = 0;
49  result += second;
50  result += minute * 60;
51  result += hour * 60 * 60;
52  result += dom * 24 * 60 * 60;
53  result += cumuldays * 24 * 60 * 60;
54  result += leaps * 24 * 60 * 60;
55  result += (year - 1970) * 365 * 24 * 60 * 60;
56  return result;
57 }
58 
59 } // namespace Conversion
60 
61 } // namespace Time
#define assert(x)
Definition: assert.h:37
Definition: fat.h:143
Definition: Stopwatch.h:25