4
* Author: Lukas Zeller (luz@synthesis.ch)
6
* conversion from/to linear time scale.
8
* Copyright (c) 2002-2009 by Synthesis AG (www.synthesis.ch)
10
* 2002-04-14 : luz : created from pascal source (plani.ch)
18
#include "prefix_file.h"
19
#include "generic_types.h"
22
#ifndef PLATFORM_LINEARTIMEDEF
24
// Standard lineartime_t definition as 64bit integer,
25
// in milliseconds since -4712-01-01 00:00:00
26
// --------------------------------------------------
32
// Linear date and time types
33
typedef sInt32 lineardate_t;
34
typedef sInt64 lineartime_t;
36
// max and min constants
37
const lineartime_t noLinearTime = 0x0; ///< undefined lineartime value
39
const lineartime_t maxLinearTime = 0x7FFFFFFFFFFFFFFFi64; ///< maximum future lineartime (signed 64 bit)
41
const lineartime_t maxLinearTime = 0x7FFFFFFFFFFFFFFFLL; ///< maximum future lineartime (signed 64 bit)
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
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
57
#endif // not PLATFORM_LINEARTIMEDEF
60
// the platform specific definitions of the time support
61
// Note: if PLATFORM_LINEARTIMEDEF is set, this must define lineartime_t and related
63
// If it defines PLATFORM_LINEARDATE2DATE etc.,
64
// implementation of these routines must be implemented platform-specific as well.
65
#include "platform_time.h"
75
// convert between different time formats and zones
76
int timeConv(int argc, const char *argv[]);
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.)
85
// Generic utility factors and routines
86
// ------------------------------------
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;
96
/// @brief conversion factor for lineardate_t to lineartime_t
97
const lineartime_t linearDateToTimeFactor = (secondToLinearTimeFactor*SecsPerHour*HoursPerDay);
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 =
104
2440588 // offset between algorithm base and 1970-01-01
105
- linearDateOriginOffset // offset between lineardate_t base and algorithm base
106
) * linearDateToTimeFactor;
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 =
113
2451911 // offset between algorithm base and 2001-01-01
114
- linearDateOriginOffset // offset between lineardate_t base and algorithm base
115
) * linearDateToTimeFactor;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
190
// Implementation of the following routines is platform specific
191
// -------------------------------------------------------------
193
/// @brief fine resolution sleep support
194
/// @param[in] aHowLong desired time to wait in lineartime_t units
195
void sleepLineartime(lineartime_t aHowLong);
199
} // namespace sysync
202
#endif // LINEARTIME_H