Debian Codegraph @

src/imports/Components/2.0/dateUtils.js

raw
1 /*
2 * Copyright 2013-2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 /*!
18 \qmltype dateUtils
19 \inqmlmodule Lomiri.Components
20 \ingroup lomiri
21 \brief Various date utility functions.
22 */
23
24 .pragma library
25
26 /*
27 Extending Date with few prototypes
28 */
29 Date.msPerDay = 86400e3
30 Date.msPerWeek = Date.msPerDay * 7
31
32 /*!
33 \qmlmethod Date::midnight()
34 The function returns a Date object with the current date and hour set to
35 midnight.
36 */
37 Date.prototype.midnight = function() {
38 this.setHours(0, 0, 0, 0);
39 return this;
40 }
41
42 /*!
43 \qmlmethod Date::getInvalidDate()
44 The function returns an invalid date object.
45 Example of use:
46 \code
47 var invalidDate = Date.prototype.getInvalidDate.call();
48 var otherInvalidDate = (new Date()).getInvalidDate();
49 \endcode
50 */
51 Date.prototype.getInvalidDate = function() {
52 return new Date(-1, -1);
53 }
54
55 /*!
56 \qmlmethod Date::isValid()
57 The function checks whether the date object is a valid one, meaning the year,
58 month and date fields are positive numbers
59 */
60 Date.prototype.isValid = function() {
61 if (Object.prototype.toString.call(this) !== "[object Date]") {
62 return false;
63 }
64 return (this.getFullYear() > 0) && (this.getMonth() >= 0) && (this.getDate() > 0);
65 }
66
67 /*!
68 \qmlmethod Date::daysInMonth()
69 The function returns the number of days in the month set in the Date object.
70 */
71 Date.prototype.daysInMonth = function() {
72 return [
73 31/*an*/, 28/*Feb*/, 31/*Mar*/, 30/*Apr*/, 31/*May*/, 30/*Jun*/,
74 31/*Jul*/, 31/*Aug*/, 30/*Sep*/, 31/*Oct*/, 30/*Nov*/, 31/*Dec*/
75 ][this.getMonth()] + (this.getMonth() === 1) * this.leapYear();
76 }
77
78 /*!
79 \qmlmethod Date::leapYear()
80 The function checks whether the year in the Date object is a leap year or not.
81 */
82 Date.prototype.leapYear = function() {
83 var year = this.getFullYear();
84 return year % 400 == 0 || (year % 100 !== 0 && year % 4 == 0);
85 }
86
87 /*!
88 \qmlmethod Date::monthsTo(date)
89 The function returns the distance in months (not calendaristic months) between
90 the Date object and the given one as parameter.
91 */
92 Date.prototype.monthsTo = function(target) {
93 return target.getMonth() - this.getMonth() + (12 * (target.getFullYear() - this.getFullYear()));
94 }
95
96 /*!
97 \qmlmethod Date::daysTo(date)
98 Same as monthsTo, but returns the distance in days.
99 */
100 Date.prototype.daysTo = function(target) {
101 return !target.isValid() ? 0 : Math.ceil((target - this) / Date.msPerDay);
102 }
103
104 /*!
105 \qmlmethod Date::hoursTo(date)
106 Same as monthsTo, but returns the distance in hours.
107 */
108 Date.prototype.hoursTo = function(target) {
109 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / (1000 * 60 * 60));
110 }
111
112 /*!
113 \qmlmethod Date::minutesTo(date)
114 Same as monthsTo, but returns the distance in minutes.
115 */
116 Date.prototype.minutesTo = function(target) {
117 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / (1000 * 60));
118 }
119
120 /*!
121 \qmlmethod Date::secondsTo(date)
122 Same as monthsTo, but returns the distance in seconds.
123 */
124 Date.prototype.secondsTo = function(target) {
125 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / 1000);
126 }
127
128 /*!
129 \qmlmethod Date::getWeek()
130 The function returns the week number of the date stored in the object.
131 */
132 Date.prototype.getWeek = function() {
133 // Copy date so don't modify original
134 var date = new Date(this);
135 date.setHours(0, 0, 0, 0);
136 // Set to nearest Thursday: current date + 4 - current day number
137 // Make Sunday's day number 7
138 date.setDate(date.getDate() + 4 - (date.getDay() || 7));
139 // Get first day of year
140 var yearStart = new Date(date.getFullYear(), 0, 1);
141 // Calculate full weeks to nearest Thursday
142 var weekNo = Math.ceil((((date - yearStart) / 86400000) + 1) / 7);
143 // Return array of year and week number
144 return weekNo;
145 }