OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4// 1998 by Bidus Yura
5//
6/// \file
7/// TTime class implementation
8///
9/// \note Be sure that you have set your environment variable TZ.
10/// For example, for Pacific coast time, set TZ=PDT8PST.
11/// For other time zones, see your manuals.
12//----------------------------------------------------------------------------
13#include <owl/pch.h>
14
15#include <owl/uimetric.h>
16#include <owl/time.h>
17#include <owl/wsyscls.h>
18#include <time.h>
19#include <stdio.h>
20
21#if defined(__GNUC__) /*JJH*/
22//Jogy 2012-06-21 - is this needed anymore?
25#else
26//extern int _RTLENTRY _EXPDATA _daylight;
27//extern long _RTLENTRY _EXPDATA _timezone;
28#endif
29
30//JJH I moved following line here, it was before definition
31// of daylight and timezone originally
32namespace owl {
33
42
43static const unsigned long SECONDS_IN_DAY = 86400L;
44static const unsigned long SECONDS_IN_HOUR = 3600L;
45static const unsigned SECONDS_IN_MIN = 60;
46
47struct TInitTime
48{
49 TInitTime()
50 { _tzset(); }
51};
52
53static const TInitTime cludgeTime; // To force the call to tzset()
54
55
56const TDate TTime::RefDate( (DayTy)0, (YearTy)0 );
57const TDate TTime::MaxDate( (DayTy)49709L, (YearTy)0 ); // ((2**32)-1)/SECONDS_IN_DAY -1
58static const int SUNDAY = 7;
59
60/// Returns 1 if d is between the earliest valid date (RefDate) and the latest valid
61/// date (MaxDate).
62int TTime::AssertDate( const TDate & date )
63{
64 return date.Between(RefDate,MaxDate);
65}
66
67//----------------------------------------------------------------------------
68// private member functions
69
70//
71/// Adjust for local time zone and Daylight Savings Time.
72//
73ClockTy TTime::LocalSecs() const
74{
75 TTime local_time( Sec - _timezone );
76 if (local_time.IsDST())
77 local_time.Sec += SECONDS_IN_HOUR;
78 return local_time.Sec;
79}
80
81//
82/// Builds the time from a local time, adjusting to GMT. Does *not* adjust for DST.
83//
84TTime TTime::BuildLocal( const TDate & date, HourTy h )
85{
86 return TTime( SECONDS_IN_DAY * (date-RefDate) +
87 SECONDS_IN_HOUR * h +
88 _timezone);
89}
90
91//----------------------------------------------------------------------------
92// public static member functions
93
94//
95/// Return the time at which DST starts for the given year.
96///
97/// Note that the time returned is the time at which DST starts locally,
98/// but it is returned in GMT.
99//
101{
102 if( year > 1986 ){
103 TDate endMarch(31, 3, year);
104 return BuildLocal( endMarch.Previous(SUNDAY)+7, 2 );
105 }
106
107 // Ah, remember those energy conscious years...???
108 if( year==1974 )
109 return BuildLocal( TDate(6,1,1974), 2 );
110 if( year==1975 )
111 return BuildLocal( TDate(23,2,1975), 2 );
112
113 TDate endApril( 30, 4, year );
114 return BuildLocal( endApril.Previous(SUNDAY), 2 );
115}
116
117
118//
119/// Return the time at which DST ends for the given year.
120///
121/// Note that the time returned is the time at which DST ends locally,
122/// but it is returned in GMT.
123//
125{
126 TDate endOctober( 31, 10, year );
127 return BuildLocal( endOctober.Previous(SUNDAY), 1 );
128}
129
130
131//----------------------------------------------------------------------------
132// constructors
133
134//
135/// Construct TTime with current time (seconds since Jan 1, 1901).
136//
138{
139#if 0
141 time(&ltime);
142 struct tm *t = localtime(&ltime);
143
144 // Construct the date. The time struct returns int, so casts are used.
145 //
146 TDate today( (DayTy)t->tm_mday,
147 (MonthTy)(t->tm_mon + 1),
148 (YearTy)t->tm_year );// +1900 //????????
149
150 *this = TTime( today,
151 (HourTy)t->tm_hour,
152 (MinuteTy)t->tm_min,
153 (SecondTy)t->tm_sec );
154#else
156 *this = TTime(TDate(_clk),
157 _clk.GetHour(),
158 _clk.GetMinute(),
159 _clk.GetSecond());
160#endif
161}
162
163//
164/// Specified time and today's date:
165//
167{
168 Sec = TTime( TDate(),h,m,s ).Sec;
169}
170
171//
172/// Construct a Time for the specified (local) Date, hour, minute, and second.
173/// \note this algorithm will fail if DST correction is something other
174/// than an hour.
175/// It is complicated by the DST boundary problem:
176/// - 1) Times in the phantom zone between 2AM and 3AM when DST is invoked are invalid.
177/// - 2) Times in the hour after 1AM when DST ends, are redundant.
178///
179/// Checking for these situations necessitates a lot of jumping back
180/// and forth by an hour to check for the boundary.
181//
183{
184 if( date.IsValid() ){
185 Sec = SECONDS_IN_DAY * (date-RefDate) +
186 SECONDS_IN_HOUR * (h-1L) + /* Note the adjustment by one hour */
187 SECONDS_IN_MIN * m + s;
188 if( Sec )
189 Sec += _timezone; // Adjust to GMT.
190
191 if( IsDST() ){
192 Sec += SECONDS_IN_HOUR;
193 if( IsDST() )
194 Sec -= SECONDS_IN_HOUR;
195 }
196 else{
197 Sec += SECONDS_IN_HOUR;
198 if( IsDST() )
199 Sec = 0; // Invalid "phantom" time.
200 }
201 }
202 else
203 Sec = 0; // Invalid date
204}
205
206//
207//
208//
210{
211 Sec = TTime(TDate(t), t.GetHour(), t.GetMinute(), t.GetSecond()).Sec;
212}
213
214//
215//
216//
219{
220 TDate dt(*this);
221 return TSystemTime(dt.Year(), dt.Month(), dt.DayOfMonth(),
222 Hour(),Minute(), Second(), dt.WeekDay());
223}
224
225//
226//
227//
229{
230 TSystemTime t(tim);
231 Sec = TTime(TDate(t), t.GetHour(), t.GetMinute(), t.GetSecond()).Sec;
232}
233
234//
235//
236//
239{
240 return TFileTime(GetSystemTime());
241}
242
243//----------------------------------------------------------------------------
244// public member functions
245
246/// Compares t to this TTime object and returns 0 if the times are equal, 1 if t is
247/// earlier, and -1 if t is later.
248int TTime::CompareTo( const TTime &t ) const
249{
250 long diff = long(Sec - t.Sec);
251 return diff==0 ? 0 : diff > 0 ? 1 : -1;
252}
253
254//
255/// Hash function:
256/// Returns seconds since January 1, 1901.
257//
258unsigned TTime::Hash() const
259{
260 return (unsigned)Sec;
261}
262
263//
264/// Returns the hour in local time.
265//
267{
268 return HourTy((LocalSecs() % SECONDS_IN_DAY) / SECONDS_IN_HOUR);
269}
270
271//
272/// Returns the hour in Greenwich mean time.
273//
275{
276 return HourTy((Sec % SECONDS_IN_DAY) / SECONDS_IN_HOUR);
277}
278
279//
280/// Returns 1 if the time is in daylight saving time; 0 otherwise.
281//
282int TTime::IsDST() const
283{
284 if( !_daylight )
285 return 0;
286
288 YearTy year = TDate( (DayTy)daycount, (YearTy)0 ).Year();
289
290 // Check to see if the time falls between the starting & stopping DST times.
291 //
292 return *this >= BeginDST( year ) && *this < EndDST( year );
293}
294
295/// Returns either this TTime object or t, whichever is greater.
296TTime TTime::Max( const TTime & t ) const
297{
298 if( *this > t )
299 return *this;
300 else
301 return t;
302}
303
304/// Returns either this TTime object or t, whichever is less.
305TTime TTime::Min( const TTime & t ) const
306{
307 if( *this < t )
308 return *this;
309 else
310 return t;
311}
312
313//
314/// Returns the minute in local time.
315//
317{
318 return MinuteTy(((LocalSecs()%SECONDS_IN_DAY)%SECONDS_IN_HOUR)/SECONDS_IN_MIN);
319}
320
321//
322/// Returns the minute in Greenwich Mean Time.
323//
325{
326 return MinuteTy(((Sec%SECONDS_IN_DAY)%SECONDS_IN_HOUR)/SECONDS_IN_MIN);
327}
328
329//
330/// Returns seconds.
331//
333{
334 return SecondTy(((Sec%SECONDS_IN_DAY)%SECONDS_IN_HOUR)%SECONDS_IN_MIN);
335}
336
337} // OWL namespace
338//==============================================================================
The TDate class represents a date.
Definition date.h:48
int Between(const TDate &d1, const TDate &d2) const
Returns 1 if this TDate object is between d1 and d2, inclusive.
Definition date.h:200
int IsValid() const
Returns 1 if this TDate is valid, 0 otherwise.
Definition date.h:212
TFileTime is a class derived from the structure FILETIME.
Definition wsyscls.h:362
TSystemTime is a class derived from the structure SYSTEMTIME.
Definition wsyscls.h:420
uint16 GetSecond()
Definition wsyscls.h:459
uint16 GetMinute()
Definition wsyscls.h:455
uint16 GetHour()
Definition wsyscls.h:451
static TSystemTime LocalTime()
Definition wsyscls.h:468
The TTime class encapsulates time functions and characteristics.
Definition time.h:38
HourTy HourGMT() const
Returns the hour in Greenwich mean time.
Definition time.cpp:274
TFileTime GetFileTime() const
Definition time.cpp:238
unsigned Hash() const
Hash function: Returns seconds since January 1, 1901.
Definition time.cpp:258
SecondTy Second() const
Returns seconds.
Definition time.cpp:332
MinuteTy Minute() const
Returns the minute in local time.
Definition time.cpp:316
MinuteTy MinuteGMT() const
Returns the minute in Greenwich Mean Time.
Definition time.cpp:324
TTime Max(const TTime &t) const
Returns either this TTime object or t, whichever is greater.
Definition time.cpp:296
TSystemTime GetSystemTime() const
Definition time.cpp:218
static TTime BeginDST(unsigned year)
Return the time at which DST starts for the given year.
Definition time.cpp:100
static const TDate MaxDate
The maximum valid date for TTime objects.
Definition time.h:104
HourTy Hour() const
Returns the hour in local time.
Definition time.cpp:266
friend TDate::TDate(const TTime &)
TTime()
Construct TTime with current time (seconds since Jan 1, 1901).
Definition time.cpp:137
static const TDate RefDate
The minimum valid date for TTime objects: January 1, 1901.
Definition time.h:103
TTime Min(const TTime &t) const
Returns either this TTime object or t, whichever is less.
Definition time.cpp:305
int IsDST() const
Returns 1 if the time is in daylight saving time; 0 otherwise.
Definition time.cpp:282
int CompareTo(const TTime &) const
Compares t to this TTime object and returns 0 if the times are equal, 1 if t is earlier,...
Definition time.cpp:248
static TTime EndDST(unsigned year)
Return the time at which DST ends for the given year.
Definition time.cpp:124
static int AssertDate(const TDate &)
Returns 1 if d is between the earliest valid date (RefDate) and the latest valid date (MaxDate).
Definition time.cpp:62
#define _RTLENTRY
Definition gnuc.h:64
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned HourTy
Definition time.h:30
TimeZone
Definition time.cpp:34
@ USEastern
Definition time.cpp:39
@ Atlantic
Definition time.cpp:39
@ Alaska
Definition time.cpp:40
@ minusSix
Definition time.cpp:35
@ Azores
Definition time.cpp:38
@ two
Definition time.cpp:38
@ CarolineIslands
Definition time.cpp:34
@ Europe
Definition time.cpp:37
@ China
Definition time.cpp:35
@ CaspianSea
Definition time.cpp:36
@ minusSeven
Definition time.cpp:35
@ Greenland
Definition time.cpp:38
@ Turkey
Definition time.cpp:36
@ MarianaIslands
Definition time.cpp:34
@ USPacific
Definition time.cpp:40
@ USCentral
Definition time.cpp:39
@ Finland
Definition time.cpp:37
@ Bearing
Definition time.cpp:41
@ Japan
Definition time.cpp:34
@ Pakistan
Definition time.cpp:36
@ USMountain
Definition time.cpp:40
@ Hawaii
Definition time.cpp:41
@ Greenwich
Definition time.cpp:37
const uint32 SECONDS_IN_DAY
Definition date.cpp:28
unsigned YearTy
Year type.
Definition date.h:32
unsigned SecondTy
Definition time.h:32
unsigned MinuteTy
Definition time.h:31
unsigned DayTy
Day type.
Definition date.h:26
STDAPI __declspec(dllexport) DllRegisterCommand(LPCTSTR cmdLine)
Entry point for complete registration management via command line Don't let any exceptions blow back ...
Definition ocreg.cpp:495
Definition of TUIMetric, a UI metrics provider class.
Classes for window system structure and type encapsulation.