2006/06/16
[barry.git] / src / time.h
blob36218390f22e0dabc3ddbf12ffc6df341171cfcb
1 ///
2 /// \file time.h
3 /// Conversion between time_t and cenmin_t and back.
4 /// time_t is the POSIX time.
5 /// min1900_t is the minutes from Jan 1, 1900
6 ///
8 /*
9 Copyright (C) 2005-2006, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #ifndef __BARRY_TIME_H__
25 #define __BARRY_TIME_H__
27 #include <time.h>
30 // Calculate the number of minutes between Jan 01, 1900 and Jan 01, 1970
32 // There are 17 leap years between 1900 and 1970
33 // (1969-1900) / 4 = 17.25
35 // 1900 itself is not a leap year (not divisible by 400)
37 #define DAY_MINUTES (24 * 60)
38 #define YEAR_MINUTES (365 * DAY_MINUTES)
39 #define LEAP_YEAR_COUNT ((1970-1901) / 4)
40 #define YEAR_COUNT (1970 - 1900)
42 // therefore, the difference between standard C's time and min1900_t's
43 // time in minutes:
44 #define STDC_MIN1900_DIFF (YEAR_COUNT * YEAR_MINUTES + LEAP_YEAR_COUNT * DAY_MINUTES)
46 namespace Barry {
48 typedef long min1900_t;
50 inline min1900_t time2min(time_t t)
52 return t / 60 + STDC_MIN1900_DIFF;
55 inline time_t min2time(min1900_t m)
57 return (m - STDC_MIN1900_DIFF) * 60;
61 } // namespace Barry
63 #endif