Imported Upstream version 1.0beta2a
[platform/upstream/syncevolution.git] / src / synthesis / src / sysync / lineartime.h
1 /*
2  *  File:         lineartime.h
3  *
4  *  Author:                       Lukas Zeller (luz@synthesis.ch)
5  *
6  *  conversion from/to linear time scale.
7  *
8  *  Copyright (c) 2002-2009 by Synthesis AG (www.synthesis.ch)
9  *
10  *  2002-04-14 : luz : created from pascal source (plani.ch)
11  *
12  */
13
14
15 #ifndef LINEARTIME_H
16 #define LINEARTIME_H
17
18 #include "prefix_file.h"
19 #include "generic_types.h"
20
21
22 #ifndef PLATFORM_LINEARTIMEDEF
23
24 // Standard lineartime_t definition as 64bit integer,
25 // in milliseconds since -4712-01-01 00:00:00
26 // --------------------------------------------------
27
28 #ifdef __cplusplus
29   namespace sysync {
30 #endif
31
32 // Linear date and time types
33 typedef sInt32 lineardate_t;
34 typedef sInt64 lineartime_t;
35
36 // max and min constants
37 const lineartime_t  noLinearTime = 0x0; ///< undefined lineartime value
38 #ifdef _MSC_VER
39 const lineartime_t maxLinearTime = 0x7FFFFFFFFFFFFFFFi64; ///< maximum future lineartime (signed 64 bit)
40 #else
41 const lineartime_t maxLinearTime = 0x7FFFFFFFFFFFFFFFLL; ///< maximum future lineartime (signed 64 bit)
42 #endif
43
44 // date origin definition relative to algorithm's origin -4712-01-01 00:00:00
45 const lineardate_t linearDateOriginOffset=0; ///< offset between algorithm's origin (-4712-01-01) and lineardate_t's zero
46 const sInt16 linearDateOriginWeekday=1; ///< weekday of lineartime origin: Monday
47
48 // scaling of lineartime relative to seconds
49 const lineartime_t secondToLinearTimeFactor = 1000; ///< how many lineartime_t units make a seconds
50 const lineartime_t nanosecondsPerLinearTime = 1000000; ///< duration of one lineartime_t in nanoseconds
51
52
53 #ifdef __cplusplus
54   } // namespace sysync
55 #endif
56
57 #endif // not PLATFORM_LINEARTIMEDEF
58
59
60 // the platform specific definitions of the time support
61 // Note: if PLATFORM_LINEARTIMEDEF is set, this must define lineartime_t and related
62 //       constants.
63 //       If it defines PLATFORM_LINEARDATE2DATE etc.,
64 //       implementation of these routines must be implemented platform-specific as well.
65 #include "platform_time.h"
66
67
68 #ifdef __cplusplus
69   namespace sysync {
70 #endif
71
72
73 #ifdef SYSYNC_TOOL
74
75 // convert between different time formats and zones
76 int timeConv(int argc, const char *argv[]);
77
78 #endif
79
80 // Time context type definition. Defined here to avoid mutual inclusion need of
81 // this file and timezones.h.
82 typedef uInt32 timecontext_t; ///< define a time context (dateonly,time zone, etc.)
83
84
85 // Generic utility factors and routines
86 // ------------------------------------
87
88 /// useful time/date definitions
89 const int SecsPerMin = 60;
90 const int MinsPerHour= 60;
91 const int SecsPerHour= SecsPerMin*MinsPerHour;
92 const int HoursPerDay= 24;
93 const int DaysPerWk  =  7;
94
95
96 /// @brief conversion factor for lineardate_t to lineartime_t
97 const lineartime_t linearDateToTimeFactor = (secondToLinearTimeFactor*SecsPerHour*HoursPerDay);
98
99 /// @brief offset from lineartime_t to UNIX time(), which is based 1970-01-01 00:00:00
100 /// @Note units of this constants are still lineartime_t units and need
101 ///       division by secondToLinearTimeFactor to get actual UNIX time in seconds
102 const lineartime_t UnixToLineartimeOffset =
103   (
104     2440588 // offset between algorithm base and 1970-01-01
105     - linearDateOriginOffset // offset between lineardate_t base and algorithm base
106   ) * linearDateToTimeFactor;
107
108 /// @brief offset from lineartime_t to NSDate reference time, which is based 2001-01-01 00:00:00
109 /// @Note units of this constants are still lineartime_t units and need
110 ///       division by secondToLinearTimeFactor to get actual NSDate in seconds
111 const lineartime_t NSDateToLineartimeOffset =
112   (
113     2451911 // offset between algorithm base and 2001-01-01
114     - linearDateOriginOffset // offset between lineardate_t base and algorithm base
115   ) * linearDateToTimeFactor;
116
117
118 /// @brief convert date to linear date
119 /// @return specified date converted to lineardate_t (unit=days)
120 /// @param[in] aYear,aMonth,aDay : date specification
121 lineardate_t date2lineardate(sInt16 aYear, sInt16 aMonth, sInt16 aDay);
122
123 /// @brief convert date to linear time
124 /// @return specified date converted to lineartime_t (unit=lineartime units)
125 /// @param[in] aYear,aMonth,aDay : date specification
126 lineartime_t date2lineartime(sInt16 aYear, sInt16 aMonth, sInt16 aDay);
127
128 /// @brief convert time to linear time
129 /// @return specified time converted to lineartime_t units
130 /// @param[in] aMinute,aSecond,aMS : time specification
131 lineartime_t time2lineartime(sInt16 aHour, sInt16 aMinute, sInt16 aSecond, sInt16 aMS);
132
133 /// @brief convert lineardate to weekday
134 /// @return 0=sunday, 1=monday ... 6=saturday
135 /// @param[in] aLinearDate linear date (in days)
136 sInt16 lineardate2weekday(lineardate_t aLinearDate);
137
138 /// @brief convert lineartime to weekday
139 /// @return 0=sunday, 1=monday ... 6=saturday
140 /// @param[in] aLinearTime linear time (in lineartime_t units)
141 sInt16 lineartime2weekday(lineartime_t aLinearTime);
142
143 /// @brief convert lineardate to year/month/day
144 /// @param[in] aLinearDate linear date (in days)
145 /// @param[out] aYearP,aMonthP,aDayP : date components, may be NULL if component not needed
146 void lineardate2date(lineardate_t aLinearDate,sInt16 *aYearP, sInt16 *aMonthP, sInt16 *aDayP);
147
148 /// @brief convert lineartime to year/month/day
149 /// @param[in] aLinearTime linear time (in lineartime_t units)
150 /// @param[out] aYearP,aMonthP,aDayP : date components, may be NULL if component not needed
151 void lineartime2date(lineartime_t aLinearTime, sInt16 *aYearP, sInt16 *aMonthP, sInt16 *aDayP);
152
153 /// @brief get number of days in a month
154 /// @return number of days in month (28..31)
155 /// @param[in] aLinearDate linear date (in days)
156 sInt16 getMonthDays(lineardate_t aLinearDate);
157
158 /// @brief convert lineartime to h,m,s,ms
159 /// @param[in] aLinearTime linear time (in lineartime_t units)
160 /// @param[out] aHourP,aMinP,aSecP,aMSP : time components, may be NULL if component not needed
161 void lineartime2time(lineartime_t aLinearTime, sInt16 *aHourP, sInt16 *aMinP, sInt16 *aSecP, sInt16 *aMSP);
162
163 /// @brief convert seconds to linear time
164 /// @return number of lineartime_t units
165 /// @param[in] aSeconds a number of seconds
166 lineartime_t seconds2lineartime(sInt32 aSeconds);
167
168 /// @brief convert linear time to seconds
169 /// @return number of seconds
170 /// @param[in] aLinearTime lineartime_t units
171 sInt32 lineartime2seconds(lineartime_t aLinearTime);
172
173 /// @brief get time-only part of a linear time
174 /// @return time only in lineartime_t units since midnight
175 /// @param[in] aLinearTime a date/timestamp in lineartime_t units
176 lineartime_t lineartime2timeonly(lineartime_t aLinearTime);
177
178 /// @brief get date-only part of a linear time
179 /// @return date only in lineardate_t units (days)
180 /// @param[in] aLinearTime a date/timestamp in lineartime_t units
181 lineardate_t lineartime2dateonly(lineartime_t aLinearTime);
182
183 /// @brief get date-only part, but IN LINEARTIME
184 /// @return date only in lineartime_t units
185 /// @param[in] aLinearTime a date/timestamp in lineartime_t units
186 lineartime_t lineartime2dateonlyTime(lineartime_t aLinearTime);
187
188
189
190 // Implementation of the following routines is platform specific
191 // -------------------------------------------------------------
192
193 /// @brief fine resolution sleep support
194 /// @param[in] aHowLong desired time to wait in lineartime_t units
195 void sleepLineartime(lineartime_t aHowLong);
196
197
198 #ifdef __cplusplus
199   } // namespace sysync
200 #endif
201
202 #endif // LINEARTIME_H
203
204 /* eof */