30 const QString &rmonth,
const QString &rday,
const QTime &rtime,
const double &dh ) {
32 if ( smonth !=
"0" ) {
33 StartMonth = initMonth( smonth );
34 RevertMonth = initMonth( rmonth );
36 if ( StartMonth && RevertMonth && initDay( sday, StartDay, StartWeek ) &&
37 initDay( rday, RevertDay, RevertWeek ) && stime.isValid() && rtime.isValid() ) {
42 kWarning() << i18n(
"Error parsing TimeZoneRule, setting to empty rule." ) ;
50 void TimeZoneRule::setEmpty() {
65 kDebug() << i18n(
"Daylight Saving Time active" );
68 kDebug() << i18n(
"Daylight Saving Time inactive" );
73 int TimeZoneRule::initMonth(
const QString &mn ) {
75 QString ml = mn.toLower();
76 if ( ml ==
"jan" )
return 1;
77 else if ( ml ==
"feb" )
return 2;
78 else if ( ml ==
"mar" )
return 3;
79 else if ( ml ==
"apr" )
return 4;
80 else if ( ml ==
"may" )
return 5;
81 else if ( ml ==
"jun" )
return 6;
82 else if ( ml ==
"jul" )
return 7;
83 else if ( ml ==
"aug" )
return 8;
84 else if ( ml ==
"sep" )
return 9;
85 else if ( ml ==
"oct" )
return 10;
86 else if ( ml ==
"nov" )
return 11;
87 else if ( ml ==
"dec" )
return 12;
89 kWarning() << i18n(
"Could not parse %1 as a valid month code.", mn );
93 bool TimeZoneRule::initDay(
const QString &dy,
int &Day,
int &Week ) {
97 int day = dy.toInt( &ok );
104 QString dl = dy.toLower();
107 if ( dl ==
"mon" ) { Day = 1; Week = 5;
return true; }
108 else if ( dl ==
"tue" ) { Day = 2; Week = 5;
return true; }
109 else if ( dl ==
"wed" ) { Day = 3; Week = 5;
return true; }
110 else if ( dl ==
"thu" ) { Day = 4; Week = 5;
return true; }
111 else if ( dl ==
"fri" ) { Day = 5; Week = 5;
return true; }
112 else if ( dl ==
"sat" ) { Day = 6; Week = 5;
return true; }
113 else if ( dl ==
"sun" ) { Day = 7; Week = 5;
return true; }
117 int wn = dl.left(1).toInt();
118 if ( wn >0 && wn <4 ) {
119 QString dm = dl.mid( 1, dl.length() ).toLower();
120 if ( dm ==
"mon" ) { Day = 1; Week = wn;
return true; }
121 else if ( dm ==
"tue" ) { Day = 2; Week = wn;
return true; }
122 else if ( dm ==
"wed" ) { Day = 3; Week = wn;
return true; }
123 else if ( dm ==
"thu" ) { Day = 4; Week = wn;
return true; }
124 else if ( dm ==
"fri" ) { Day = 5; Week = wn;
return true; }
125 else if ( dm ==
"sat" ) { Day = 6; Week = wn;
return true; }
126 else if ( dm ==
"sun" ) { Day = 7; Week = wn;
return true; }
129 kWarning() << i18n(
"Could not parse %1 as a valid day code.", dy );
141 if ( StartWeek==0 )
return StartDay;
144 else if ( StartWeek==5 ) {
145 for ( test = QDate( d.date().year(), d.date().month(), d.date().daysInMonth() );
146 test.day() > 21; test = test.addDays( -1 ) )
147 if ( test.dayOfWeek() == StartDay )
break;
149 for ( test = QDate( d.date().year(), d.date().month(), (StartWeek-1)*7 + 1 );
150 test.day() < 7*StartWeek; test = test.addDays( 1 ) )
151 if ( test.dayOfWeek() == StartDay )
break;
164 if ( RevertWeek==0 )
return RevertDay;
167 else if ( RevertWeek==5 ) {
168 for ( test = QDate( d.date().year(), d.date().month(), d.date().daysInMonth() );
169 test.day() > 21; test = test.addDays( -1 ) )
170 if ( test.dayOfWeek() == RevertDay )
break;
172 for ( test = QDate( d.date().year(), d.date().month(), (RevertWeek-1)*7 + 1 );
173 test.day() < 7*RevertWeek; test = test.addDays( 1 ) )
174 if ( test.dayOfWeek() == StartDay )
break;
186 int month = date.date().month();
188 if ( StartMonth < RevertMonth ) {
189 if ( month < StartMonth || month > RevertMonth )
return false;
191 if ( month < StartMonth && month > RevertMonth )
return false;
196 int day = date.date().day();
198 if ( month == StartMonth ) {
199 int sday = findStartDay( date );
200 if ( day < sday )
return false;
201 if ( day==sday && date.time() < StartTime )
return false;
202 }
else if ( month == RevertMonth ) {
203 int rday = findRevertDay( date );
204 if ( day > rday )
return false;
205 if ( day==rday && date.time() > RevertTime )
return false;
223 int y = date.date().year();
224 if ( RevertMonth < date.date().month() ) ++y;
226 result =
KStarsDateTime( QDate( y, RevertMonth, 1 ), RevertTime );
227 result =
KStarsDateTime( QDate( y, RevertMonth, findRevertDay( result ) ), RevertTime );
234 int y = date.date().year();
235 if ( StartMonth < date.date().month() ) ++y;
238 result =
KStarsDateTime( QDate( y, StartMonth, findStartDay( result ) ), StartTime );
241 kDebug() << i18n(
"Next Daylight Savings Time change (Local Time): " ) << result.toString();
242 next_change_ltime = result;
246 void TimeZoneRule::previousDSTChange_LTime(
const KStarsDateTime &date ) {
257 int y = date.date().year();
258 if ( StartMonth > date.date().month() ) --y;
261 result =
KStarsDateTime( QDate( y, StartMonth, findStartDay( result ) ), StartTime );
263 }
else if ( StartMonth ) {
268 int y = date.date().year();
269 if ( RevertMonth > date.date().month() ) --y;
271 result =
KStarsDateTime( QDate( y, RevertMonth, 1 ), RevertTime );
272 result =
KStarsDateTime( QDate( y, RevertMonth, findRevertDay( result ) ), RevertTime );
275 kDebug() << i18n(
"Previous Daylight Savings Time change (Local Time): " ) << result.toString();
276 next_change_ltime = result;
284 kDebug() << i18n(
"Next Daylight Savings Time change (UTC): " ) << result.toString();
285 next_change_utc = result;
289 void TimeZoneRule::previousDSTChange(
const KStarsDateTime &local_date,
const double TZoffset ) {
294 if ( result.date().month() == RevertMonth )
295 result = result.
addSecs(
int(HourOffset * -3600) );
297 kDebug() << i18n(
"Previous Daylight Savings Time change (UTC): " ) << result.toString();
298 next_change_utc = result;
302 const bool automaticDSTchange ) {
322 bool wasDSTactive(
false);
335 if ( active_with_houroffset != active_normal && ValidLTime.date().month() == StartMonth ) {
337 kDebug() <<
"Current time = Starttime: invalid local time due to daylight saving time";
341 if ( automaticDSTchange ) {
345 if ( wasDSTactive ) {
347 ValidLTime = ltime.
addSecs(
int( HourOffset * - 3600) );
351 ValidLTime = ltime.
addSecs(
int( HourOffset * 3600) );
356 if ( wasDSTactive ) {
359 ValidLTime = ltime.
addSecs(
int( HourOffset * 3600) );
363 ValidLTime = ltime.
addSecs(
int( HourOffset * -3600) );
371 if ( active_with_houroffset != active_normal && RevertMonth == ValidLTime.date().month() ) {
373 kDebug() <<
"Current time = Reverttime";
377 if ( automaticDSTchange ) {
393 if (time_runs_forward) {
399 previousDSTChange_LTime( ValidLTime );
400 previousDSTChange( next_change_ltime, TZoffset );
407 if ( StartDay == r->StartDay && RevertDay == r->RevertDay &&
408 StartWeek == r->StartWeek && RevertWeek == r->RevertWeek &&
409 StartMonth == r->StartMonth && RevertMonth == r->RevertMonth &&
410 StartTime == r->StartTime && RevertTime == r->RevertTime &&
void setDST(bool activate=true)
Toggle DST on/off.
KStarsDateTime nextDSTChange_LTime()
This class provides the information needed to determine whether Daylight Savings Time (DST; a...
KStarsDateTime nextDSTChange()
KStarsDateTime addSecs(double s) const
Extension of KDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day...
void reset_with_ltime(KStarsDateTime <ime, const double TZoffset, const bool time_runs_forward, const bool automaticDSTchange=false)
Recalculate next dst change and if DST is active by a given local time with timezone offset and time ...
TimeZoneRule()
Default Constructor.
bool equals(TimeZoneRule *r)
bool isDSTActive(const KStarsDateTime &date)
Determine whether DST is in effect for the given DateTime, according to this rule.