• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kcalendarsystemjalali.cpp
1 /*
2  Copyright (C) 2002-2003 Arash Bijanzadeh and FarsiKDE Project <www.farsikde.org>
3  Contact: Arash Bijanzadeh <a.bijanzadeh@linuxiran.org>
4 
5  This program is part of FarsiKDE
6 
7  FarsiKDE is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  FarsiKDE is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21  */
22 
23 
24 #include <tqdatetime.h>
25 #include <tqstring.h>
26 #include <tqstringlist.h>
27 #include <math.h>
28 
29 #include <tdeglobal.h>
30 #include <tdelocale.h>
31 #include <kdebug.h>
32 #include <stdio.h>
33 
34 #include "kcalendarsystemjalali.h"
35 
36 static const int gMonthDay[2][13]={
37  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
38  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
39 };
40 
41 static const int jMonthDay[2][13] = {
42  {0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29},
43  {0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30},
44 };
45 
46 typedef struct {
47  int day;
48  int mon;
49  int year;
50  } SDATE;
51 // converting funcs from
52 
53 static int Ceil(float number)
54 {
55  int ret;
56  if(number>0)
57  number += 0.5;
58  ret =(int) number;
59  return ret;
60 }
61 
62 static long jalali_jdn(int year, int month, int day)
63 {
64  const long PERSIAN_EPOCH = 1948321; /* The JDN of 1 Farvardin 1*/
65  int epbase;
66  long epyear;
67  long mdays;
68  long jdn;
69  epbase = year - 474;
70  epyear = 474 + (epbase % 2820);
71  if (month <= 7)
72  mdays = (month - 1) * 31;
73  else
74  mdays = (month - 1) * 30 + 6;
75  jdn = day + mdays ;
76  jdn += (((epyear * 682) - 110) / 2816) ;
77  jdn += (epyear - 1) * 365;
78  jdn += (epbase / 2820) * 1029983 ;
79  jdn += (PERSIAN_EPOCH - 1);
80  return jdn;
81 }
82 
83 
84 static SDATE jdn_jalali(long jdn)
85 {
86  static SDATE ret;
87  int day, month, year;
88  int iYear, iMonth, iDay;
89  int depoch;
90  int cycle;
91  int cyear;
92  int ycycle;
93  int aux1, aux2;
94  int yday;
95  day = 1;
96  month = 1;
97  year = 475;
98  depoch = jdn - jalali_jdn(year,month, day);
99  cycle = (int) (depoch / 1029983);
100  cyear = depoch % 1029983;
101  if( cyear == 1029982)
102  ycycle = 2820;
103  else{
104  aux1 = cyear / 366;
105  aux2 = cyear % 366;
106  ycycle = (((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1;
107  }
108  iYear = ycycle + (2820 * cycle) + 474;
109  if (iYear <= 0)
110  iYear = iYear - 1;
111  year = iYear;
112  yday = (jdn - jalali_jdn(year, month, day)) + 1;
113  if(yday <= 186 )
114  iMonth = Ceil((yday-1) / 31);
115  else
116  iMonth = Ceil((yday - 7) / 30);
117  iMonth++;
118  month = iMonth;
119  iDay = (jdn - jalali_jdn(year, month, day)) + 1;
120  ret.day = iDay;
121  ret.mon = iMonth;
122  ret.year = iYear;
123  return ret;
124 }
125 
126 
127 
128 static long civil_jdn(int year, int month, int day)
129 {
130  long jdn = ((1461 * (year + 4800 + ((month - 14) / 12))) / 4)
131  + ((367 * (month - 2 - 12 * (((month - 14) / 12)))) / 12)
132  - ((3 * (((year + 4900 + ((month - 14) / 12)) / 100))) / 4)
133  + day - 32075;
134  return jdn;
135 }
136 
137 static SDATE jdn_civil(long jdn)
138 {
139  long l, n, i, j;
140  static SDATE ret;
141  int iday, imonth, iyear;
142  l = jdn + 68569;
143  n = ((4 * l) / 146097);
144  l = l - ((146097 * n + 3) / 4);
145  i = ((4000 * (l + 1)) / 1461001);
146  l = l - ((1461 * i) / 4) + 31;
147  j = ((80 * l) / 2447);
148  iday = l - ((2447 * j) / 80);
149  l = (j / 11);
150  imonth = j + 2 - 12 * l;
151  iyear = 100 * (n - 49) + i + l;
152  ret.day = iday;
153  ret.mon = imonth;
154  ret.year = iyear;
155  return (ret);
156 }
157 
158 static SDATE *jalaliToGregorian(int y,int m,int d)
159 {
160 static SDATE sd;
161 long jday = jalali_jdn(y,m,d);
162 sd= jdn_civil(jday);
163 return (&sd);
164 }
165 static SDATE *gregorianToJalali(int y,int m, int d)
166 {
167  static SDATE sd;
168  long jdn = civil_jdn(y,m,d);//TQDate::gregorianToJulian(y, m, d);
169  sd = jdn_jalali(jdn);
170  return(&sd);
171 }
172 static void gregorianToJalali(const TQDate & date, int * pYear, int * pMonth,
173  int * pDay)
174 {
175  SDATE *sd;
176  sd = gregorianToJalali(date.year(), date.month(), date.day());
177  if (pYear)
178  *pYear = sd->year;
179  if (pMonth)
180  *pMonth = sd->mon;
181  if (pDay)
182  *pDay = sd->day;
183 
184 }
185 
186 // End of converting functions
187 
188 static int isJalaliLeap(int year)
189 {
190  int tmp;
191  tmp = year % 33;
192  if (tmp == 1 || tmp == 5||tmp==9||tmp==13||tmp==17||tmp==22||tmp==26||tmp==30)
193  return 1;
194 else
195  return 0;
196 }
197 static int hndays(int m,int y)
198 {
199  return jMonthDay[isJalaliLeap(y)][m];
200 }
201 
202 
203 KCalendarSystemJalali::KCalendarSystemJalali(const TDELocale * locale)
204  : KCalendarSystem(locale)
205 {
206 }
207 
208 KCalendarSystemJalali::~KCalendarSystemJalali()
209 {
210 }
211 
212 int KCalendarSystemJalali::year(const TQDate& date) const
213 
214 {
215  kdDebug(5400) << "Jalali year..." << endl;
216 int y;
217  gregorianToJalali(date, &y, 0, 0);
218  return y;
219 }
220 
221 int KCalendarSystemJalali::month (const TQDate& date) const
222 
223 {
224  kdDebug(5400) << "Jalali month..." << endl;
225 int m;
226  gregorianToJalali(date, 0 , &m, 0);
227  return m;
228 }
229 
230 int KCalendarSystemJalali::day(const TQDate& date) const
231 
232 {
233  kdDebug(5400) << "Jalali day..." << endl;
234 int d;
235  gregorianToJalali(date, 0, 0, &d);
236  return d;
237 }
238 
239 int KCalendarSystemJalali::dayOfWeek(const TQDate& date) const
240 {
241 //same same I think?!
242  return date.dayOfWeek();
243 
244 }
245 
246 //NOT TESTED YET
247 int KCalendarSystemJalali::dayOfYear(const TQDate & date) const
248 {
249  TQDate first;
250  setYMD(first, year(date), 1, 1);
251 
252  return first.daysTo(date) + 1;
253 }
254 
255 //MAY BE BUGGY
256 bool KCalendarSystemJalali::setYMD(TQDate & date, int y, int m, int d) const
257 {
258  // range checks
259  if ( y < minValidYear() || y > maxValidYear() )
260  return false;
261 
262  if ( m < 1 || m > 12 )
263  return false;
264 
265  if ( d < 1 || d > hndays(m, y) )
266  return false;
267 
268  SDATE *gd =jalaliToGregorian( y, m, d);
269 
270  return date.setYMD(gd->year, gd->mon, gd->day);
271 }
272 
273 TQDate KCalendarSystemJalali::addYears( const TQDate & date, int nyears ) const
274 {
275  TQDate result = date;
276  int y = year(date) + nyears;
277  setYMD( result, y, month(date), day(date) );
278 
279  return result;
280 }
281 
282 TQDate KCalendarSystemJalali::addMonths( const TQDate & date, int nmonths ) const
283 {
284  TQDate result = date;
285  int m = month(date);
286  int y = year(date);
287 
288  if ( nmonths < 0 )
289  {
290  m += 12;
291  y -= 1;
292  }
293 
294  --m; // this only works if we start counting at zero
295  m += nmonths;
296  y += m / 12;
297  m %= 12;
298  ++m;
299 
300  setYMD( result, y, m, day(date) );
301 
302  return result;
303 }
304 
305 TQDate KCalendarSystemJalali::addDays( const TQDate & date, int ndays ) const
306 {
307  return date.addDays( ndays );
308 }
309 
310 int KCalendarSystemJalali::monthsInYear( const TQDate & date ) const
311 {
312  Q_UNUSED( date )
313 
314  return 12;
315 }
316 
317 int KCalendarSystemJalali::daysInYear(const TQDate & date) const
318 {
319 Q_UNUSED(date);
320 int result;
321 //SDATE *sd = gregorianToJalali(year(date),month(date),day(date));
322 //if (isJalaliLeap(sd->year))
323  result=366;
324 //else
325 // result=365;
326 return result;
327 }
328 
329 int KCalendarSystemJalali::daysInMonth(const TQDate & date) const
330 {
331 SDATE *sd = gregorianToJalali(date.year(),date.month(),date.day());
332 return hndays(sd->mon,sd->year);
333 }
334 
335 int KCalendarSystemJalali::weeksInYear(int year) const
336 
337 {
338  Q_UNUSED(year);
339 // couldn't understand it!
340 return 52;
341 }
342 
343 int KCalendarSystemJalali::weekNumber(const TQDate& date, int * yearNum) const
344 {
345  TQDate firstDayWeek1, lastDayOfYear;
346  int y = year(date);
347  int week;
348  int weekDay1, dayOfWeek1InYear;
349 
350  // let's guess 1st day of 1st week
351  setYMD(firstDayWeek1, y, 1, 1);
352  weekDay1 = dayOfWeek(firstDayWeek1);
353 
354  // iso 8601: week 1 is the first containing thursday and week starts on
355  // monday
356  if (weekDay1 > 4 /*Thursday*/)
357  firstDayWeek1 = addDays(firstDayWeek1 , 7 - weekDay1 + 1); // next monday
358 
359  dayOfWeek1InYear = dayOfYear(firstDayWeek1);
360 
361  if ( dayOfYear(date) < dayOfWeek1InYear ) // our date in prev year's week
362  {
363  if ( yearNum )
364  *yearNum = y - 1;
365  return weeksInYear(y - 1);
366  }
367  // let' check if its last week belongs to next year
368  setYMD(lastDayOfYear, y, 12, hndays(12, y));
369  if ( (dayOfYear(date) >= daysInYear(date) - dayOfWeek(lastDayOfYear) + 1)
370  // our date is in last week
371  && dayOfWeek(lastDayOfYear) < 4) // 1st week in next year has thursday
372  {
373  if ( yearNum )
374  *yearNum = y + 1;
375  week = 1;
376  }
377  else
378  week = firstDayWeek1.daysTo(date) / 7 + 1;
379 
380  return week;
381 }
382 
383 TQString KCalendarSystemJalali::monthName(int month, int year, bool shortName)
384  const
385 {
386  Q_UNUSED(year);
387 
388  if (shortName)
389  switch ( month )
390  {
391  case 1:
392  return locale()->translate("Far");
393  case 2:
394  return locale()->translate("Ord");
395  case 3:
396  return locale()->translate("Kho");
397  case 4:
398  return locale()->translate("Tir");
399  case 5:
400  return locale()->translate("Mor");
401  case 6:
402  return locale()->translate("Sha");
403  case 7:
404  return locale()->translate("Meh");
405  case 8:
406  return locale()->translate("Aba");
407  case 9:
408  return locale()->translate("Aza");
409  case 10:
410  return locale()->translate("Dei");
411  case 11:
412  return locale()->translate("Bah");
413  case 12:
414  return locale()->translate("Esf");
415  }
416  else
417  switch ( month )
418  {
419  case 1:
420  return locale()->translate("Farvardin");
421  case 2:
422  return locale()->translate("Ordibehesht");
423  case 3:
424  return locale()->translate("Khordad");
425  case 4:
426  return locale()->translate("Tir");
427  case 5:
428  return locale()->translate("Mordad");
429  case 6:
430  return locale()->translate("Shahrivar");
431  case 7:
432  return locale()->translate("Mehr");
433  case 8:
434  return locale()->translate("Aban");
435  case 9:
436  return locale()->translate("Azar");
437  case 10:
438  return locale()->translate("Dei");
439  case 11:
440  return locale()->translate("Bahman");
441  case 12:
442  return locale()->translate("Esfand");
443  }
444 
445  return TQString::null;
446 }
447 
448 TQString KCalendarSystemJalali::monthName(const TQDate& date, bool shortName)
449  const
450 {
451  int mon;
452  gregorianToJalali(date,0,&mon,0);
453  //SDATE *sd = gregorianToJalali(date.year(),date.month(),date.day());
454  return (monthName(mon, 0, shortName));
455 }
456 
457 TQString KCalendarSystemJalali::monthNamePossessive(const TQDate& date,
458  bool shortName ) const
459 {
460  return monthName(date,shortName);
461 }
462 
463 TQString KCalendarSystemJalali::monthNamePossessive(int month, int year,
464  bool shortName ) const
465 {
466  return monthName(month,year,shortName);
467 }
468 
469 
470 TQString KCalendarSystemJalali::weekDayName(int day, bool shortName) const
471 {
472  if ( shortName )
473  switch (day)
474  {
475  case 1:
476  return locale()->translate("2sh");
477  case 2:
478  return locale()->translate("3sh");
479  case 3:
480  return locale()->translate("4sh");
481  case 4:
482  return locale()->translate("5sh");
483  case 5:
484  return locale()->translate("Jom");
485  case 6:
486  return locale()->translate("shn");
487  case 7:
488  return locale()->translate("1sh");
489  }
490  else
491  switch ( day )
492  {
493  case 1:
494  return locale()->translate("Do shanbe");
495  case 2:
496  return locale()->translate("Se shanbe");
497  case 3:
498  return locale()->translate("Chahar shanbe");
499  case 4:
500  return locale()->translate("Panj shanbe");
501  case 5:
502  return locale()->translate("Jumee");
503  case 6:
504  return locale()->translate("Shanbe");
505  case 7:
506  return locale()->translate("Yek-shanbe");
507  }
508 
509  return TQString::null;
510 }
511 
512 TQString KCalendarSystemJalali::weekDayName(const TQDate &date,bool shortName)
513  const
514 {
515  return weekDayName(dayOfWeek(date), shortName);
516 }
517 
518 // Min valid year that may be converted to QDate
519 int KCalendarSystemJalali::minValidYear() const
520 {
521  TQDate date(1753, 1, 1);
522 
523  return year(date);
524 }
525 
526 // Max valid year that may be converted to QDate
527 int KCalendarSystemJalali::maxValidYear() const
528 {
529 /*
530  TQDate date(8000, 1, 1);
531 
532  SDATE *sd = toJalali(date);
533 
534  return sd->year;
535  */
536  return 10000;
537 }
538 int KCalendarSystemJalali::weekDayOfPray() const
539 {
540  return 5; // friday
541 }
542 TQString KCalendarSystemJalali::calendarName() const
543 {
544  return TQString::fromLatin1("jalali");
545 }
546 
547 bool KCalendarSystemJalali::isLunar() const
548 {
549  return false;
550 }
551 
552 bool KCalendarSystemJalali::isLunisolar() const
553 {
554  return false;
555 }
556 
557 bool KCalendarSystemJalali::isSolar() const
558 {
559  return true;
560 }
KCalendarSystemJalali::dayOfYear
virtual int dayOfYear(const TQDate &date) const
Gets specific calendar type day number of year for a given date.
Definition: kcalendarsystemjalali.cpp:247
KCalendarSystemJalali::daysInYear
virtual int daysInYear(const TQDate &date) const
Gets the number of days in date whose years specified.
Definition: kcalendarsystemjalali.cpp:317
KCalendarSystemJalali::month
virtual int month(const TQDate &date) const
Gets specific calendar type month for a given gregorian date.
Definition: kcalendarsystemjalali.cpp:221
KCalendarSystemJalali::isSolar
virtual bool isSolar() const
Gets if the calendar is solar based.
Definition: kcalendarsystemjalali.cpp:557
KCalendarSystemJalali::maxValidYear
virtual int maxValidYear() const
Gets the maximum year value supported by specific calendar type algorithms (TQDate,...
Definition: kcalendarsystemjalali.cpp:527
KCalendarSystemJalali::monthNamePossessive
virtual TQString monthNamePossessive(const TQDate &date, bool shortName=false) const
Returns a string containing the possessive form of the month name.
Definition: kcalendarsystemjalali.cpp:457
KCalendarSystemJalali::weekNumber
virtual int weekNumber(const TQDate &date, int *yearNum=0) const
Gets specific calendar type week number for a given date.
Definition: kcalendarsystemjalali.cpp:343
KCalendarSystemJalali::isLunar
virtual bool isLunar() const
Gets if the calendar is lunar based.
Definition: kcalendarsystemjalali.cpp:547
KCalendarSystemJalali::weekDayName
virtual TQString weekDayName(int weekDay, bool shortName=false) const
Gets specific calendar type week day name If an invalid week day is specified, TQString::null is retu...
Definition: kcalendarsystemjalali.cpp:470
KCalendarSystemJalali::calendarName
virtual TQString calendarName() const
Gets the string representing the calendar.
Definition: kcalendarsystemjalali.cpp:542
KCalendarSystemJalali::weekDayOfPray
virtual int weekDayOfPray() const
Gets the day of the week traditionaly associated with pray.
Definition: kcalendarsystemjalali.cpp:538
KCalendarSystemJalali::minValidYear
virtual int minValidYear() const
Gets the first year value supported by specific calendar type algorithms.
Definition: kcalendarsystemjalali.cpp:519
KCalendarSystemJalali::isLunisolar
virtual bool isLunisolar() const
Gets if the calendar is lunisolar based.
Definition: kcalendarsystemjalali.cpp:552
KCalendarSystemJalali::day
virtual int day(const TQDate &date) const
Gets specific calendar type day number of month for a given date.
Definition: kcalendarsystemjalali.cpp:230
KCalendarSystemJalali::addMonths
virtual TQDate addMonths(const TQDate &date, int nmonths) const
Returns a TQDate object containing a date nmonths later.
Definition: kcalendarsystemjalali.cpp:282
KCalendarSystemJalali::addDays
virtual TQDate addDays(const TQDate &date, int ndays) const
Returns a TQDate object containing a date ndays later.
Definition: kcalendarsystemjalali.cpp:305
KCalendarSystemJalali::year
virtual int year(const TQDate &date) const
Gets specific calendar type year for a given gregorian date.
Definition: kcalendarsystemjalali.cpp:212
KCalendarSystemJalali::monthsInYear
virtual int monthsInYear(const TQDate &date) const
Gets specific calendar type number of month for a given year.
Definition: kcalendarsystemjalali.cpp:310
KCalendarSystemJalali::KCalendarSystemJalali
KCalendarSystemJalali(const TDELocale *locale=0)
Constructor.
Definition: kcalendarsystemjalali.cpp:203
KCalendarSystemJalali::daysInMonth
virtual int daysInMonth(const TQDate &date) const
Gets specific calendar type number of days in month for a given date.
Definition: kcalendarsystemjalali.cpp:329
KCalendarSystemJalali::setYMD
virtual bool setYMD(TQDate &date, int y, int m, int d) const
Changes the date's year, month and day.
Definition: kcalendarsystemjalali.cpp:256
KCalendarSystemJalali::monthName
virtual TQString monthName(const TQDate &date, bool shortName=false) const
Gets specific calendar type month name for a given gregorian date.
Definition: kcalendarsystemjalali.cpp:448
KCalendarSystemJalali::addYears
virtual TQDate addYears(const TQDate &date, int nyears) const
Returns a TQDate object containing a date nyears later.
Definition: kcalendarsystemjalali.cpp:273
KCalendarSystemJalali::dayOfWeek
virtual int dayOfWeek(const TQDate &date) const
Gets specific calendar type number of day of week number for a given date.
Definition: kcalendarsystemjalali.cpp:239
KCalendarSystemJalali::weeksInYear
virtual int weeksInYear(int year) const
Gets the number of weeks in a specified year.
Definition: kcalendarsystemjalali.cpp:335
KCalendarSystem
CalendarSystem abstract class, default derived kde gregorian class and factory class.
Definition: kcalendarsystem.h:43
KCalendarSystem::locale
const TDELocale * locale() const
Gets the locale the calendar uses for translations.
Definition: kcalendarsystem.cpp:47
TDELocale
TDELocale provides support for country specific stuff like the national language.
Definition: tdelocale.h:124
TDELocale::translate
TQString translate(const char *index) const
Translates the string into the corresponding string in the national language, if available.
Definition: tdelocale.cpp:768
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
tdelocale.h

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.