461833bc72c7e34940cde871677b2429cd7734d7
[platform/framework/web/web-ui-fw.git] / libs / js / globalize / generator / HijriCalendar.js
1                     // Adapted to Script from System.Globalization.HijriCalendar
2                     ticks1970: 62135596800000,
3                     // number of days leading up to each month
4                     monthDays: [0, 30, 59, 89, 118, 148, 177, 207, 236, 266, 295, 325, 355],
5                     minDate: -42521673600000,
6                     maxDate: 253402300799999,
7                     // The number of days to add or subtract from the calendar to accommodate the variances
8                     // in the start and the end of Ramadan and to accommodate the date difference between
9                     // countries/regions. May be dynamically adjusted based on user preference, but should
10                     // remain in the range of -2 to 2, inclusive.
11                     hijriAdjustment: %HIJRIADJUSTMENT%,
12                     toGregorian: function(hyear, hmonth, hday) {
13                         var daysSinceJan0101 = this.daysToYear(hyear) + this.monthDays[hmonth] + hday - 1 - this.hijriAdjustment;
14                         // 86400000 = ticks per day
15                         var gdate = new Date(daysSinceJan0101 * 86400000 - this.ticks1970);
16                         // adjust for timezone, because we are interested in the gregorian date for the same timezone
17                         // but ticks in javascript is always from GMT, unlike the server were ticks counts from the base
18                         // date in the current timezone.
19                         gdate.setMinutes(gdate.getMinutes() + gdate.getTimezoneOffset());
20                         return gdate;
21                     },
22                     fromGregorian: function(gdate) {
23                         if ((gdate < this.minDate) || (gdate > this.maxDate)) return null;
24                         var ticks = this.ticks1970 + (gdate-0) - gdate.getTimezoneOffset() * 60000,
25                             daysSinceJan0101 = Math.floor(ticks / 86400000) + 1 + this.hijriAdjustment;
26                         // very particular formula determined by someone smart, adapted from the server-side implementation.
27                         // it approximates the hijri year.
28                         var hday, hmonth, hyear = Math.floor(((daysSinceJan0101 - 227013) * 30) / 10631) + 1,
29                             absDays = this.daysToYear(hyear),
30                             daysInYear = this.isLeapYear(hyear) ? 355 : 354;
31                         // hyear is just approximate, it may need adjustment up or down by 1.
32                         if (daysSinceJan0101 < absDays) {
33                             hyear--;
34                             absDays -= daysInYear;
35                         }
36                         else if (daysSinceJan0101 === absDays) {
37                             hyear--;
38                             absDays = this.daysToYear(hyear);
39                         }
40                         else {
41                             if (daysSinceJan0101 > (absDays + daysInYear)) {
42                                 absDays += daysInYear;
43                                 hyear++;
44                             }
45                         }
46                         // determine month by looking at how many days into the hyear we are
47                         // monthDays contains the number of days up to each month.
48                         hmonth = 0;
49                         var daysIntoYear = daysSinceJan0101 - absDays;
50                         while (hmonth <= 11 && daysIntoYear > this.monthDays[hmonth]) {
51                             hmonth++;
52                         }
53                         hmonth--;
54                         hday = daysIntoYear - this.monthDays[hmonth];
55                         return [hyear, hmonth, hday];
56                     },
57                     daysToYear: function(year) {
58                         // calculates how many days since Jan 1, 0001
59                         var yearsToYear30 = Math.floor((year - 1) / 30) * 30,
60                             yearsInto30 = year - yearsToYear30 - 1,
61                             days = Math.floor((yearsToYear30 * 10631) / 30) + 227013;
62                         while (yearsInto30 > 0) {
63                             days += (this.isLeapYear(yearsInto30) ? 355 : 354);
64                             yearsInto30--;
65                         }
66                         return days;
67                     },
68                     isLeapYear: function(year) {
69                         return ((((year * 11) + 14) % 30) < 11);
70                     }