@fr0st/datetime 5.1.6 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fr0st/datetime",
3
- "version": "5.1.6",
3
+ "version": "6.0.0",
4
4
  "description": "FrostDateTime is a free, open-source date manipulation library for JavaScript.",
5
5
  "keywords": [
6
6
  "date",
@@ -29,7 +29,7 @@
29
29
  "scripts": {
30
30
  "build": "npm run js-compile && npm run js-minify",
31
31
  "js-compile": "rollup --config",
32
- "js-lint": "eslint --ext .js .",
32
+ "js-lint": "eslint",
33
33
  "js-minify": "terser --compress passes=2 --mangle --source-map \"content=dist/frost-datetime.js.map\" --output dist/frost-datetime.min.js dist/frost-datetime.js",
34
34
  "test": "mocha --recursive",
35
35
  "locales": "php generate-locales.php > src/formatter/locales.js"
@@ -42,10 +42,10 @@
42
42
  "license": "MIT",
43
43
  "private": false,
44
44
  "devDependencies": {
45
- "eslint": "^8.56.0",
46
- "eslint-config-google": "^0.14.0",
47
- "mocha": "^10.2.0",
48
- "rollup": "^4.9.1",
49
- "terser": "^5.26.0"
45
+ "@fr0st/eslint-config": "^1.0.0",
46
+ "eslint": "^9.5.0",
47
+ "mocha": "^10.4.0",
48
+ "rollup": "^4.18.0",
49
+ "terser": "^5.31.1"
50
50
  }
51
51
  }
@@ -1,4 +1,4 @@
1
- import { makeFormatter } from './../factory.js';
1
+ import { getRelativeFormatter, makeFormatter } from './../factory.js';
2
2
  import { getDayPeriods, getDays, getEras, getMonths, getNumbers } from './values.js';
3
3
 
4
4
  /**
@@ -88,6 +88,23 @@ export function formatOffset(offset, useColon = true, optionalMinutes = false) {
88
88
  return `${sign}${hourString}${colon}${minuteString}`;
89
89
  };
90
90
 
91
+ /**
92
+ * Format a relative duration as a locale string.
93
+ * @param {string} locale The locale.
94
+ * @param {number} amount The amount of duration.
95
+ * @param {string} unit The time unit.
96
+ * @returns {string} The relative duration.
97
+ */
98
+ export function formatRelative(locale, amount, unit) {
99
+ const relativeFormatter = getRelativeFormatter(locale);
100
+
101
+ if (!relativeFormatter) {
102
+ throw new Error('RelativeTimeFormat not supported');
103
+ }
104
+
105
+ return relativeFormatter.format(amount, unit);
106
+ };
107
+
91
108
  /**
92
109
  * Format a time zone as a locale string.
93
110
  * @param {string} locale The locale.
package/src/helpers.js CHANGED
@@ -1,10 +1,103 @@
1
1
  import { getDateFormatter } from './factory.js';
2
- import { thresholds } from './vars.js';
2
+ import { diffMethods, thresholds } from './vars.js';
3
3
 
4
4
  /**
5
5
  * DateTime Helpers
6
6
  */
7
7
 
8
+ export function calculateDiff(date, other, timeUnit, relative = true) {
9
+ other = other.setTimeZone(date.getTimeZone());
10
+
11
+ switch (timeUnit) {
12
+ case 'year':
13
+ return compensateDiff(
14
+ date,
15
+ other.setYear(
16
+ date.getYear(),
17
+ ),
18
+ date.getYear() - other.getYear(),
19
+ !relative,
20
+ -1,
21
+ );
22
+ case 'month':
23
+ return compensateDiff(
24
+ date,
25
+ other.setYear(
26
+ date.getYear(),
27
+ date.getMonth(),
28
+ ),
29
+ (date.getYear() - other.getYear()) * 12 + date.getMonth() - other.getMonth(),
30
+ !relative,
31
+ -1,
32
+ );
33
+ case 'week':
34
+ return compensateDiff(
35
+ date,
36
+ other.setWeekYear(
37
+ date.getWeekYear(),
38
+ date.getWeek(),
39
+ ),
40
+ (date - other) / 604800000,
41
+ relative,
42
+ );
43
+ case 'day':
44
+ return compensateDiff(
45
+ date,
46
+ other.setYear(
47
+ date.getYear(),
48
+ date.getMonth(),
49
+ date.getDate(),
50
+ ),
51
+ (date - other) / 86400000,
52
+ relative,
53
+ );
54
+ case 'hour':
55
+ return compensateDiff(
56
+ date,
57
+ other.setYear(
58
+ date.getYear(),
59
+ date.getMonth(),
60
+ date.getDate(),
61
+ ).setHours(
62
+ date.getHours(),
63
+ ),
64
+ (date - other) / 3600000,
65
+ relative,
66
+ );
67
+ case 'minute':
68
+ return compensateDiff(
69
+ date,
70
+ other.setYear(
71
+ date.getYear(),
72
+ date.getMonth(),
73
+ date.getDate(),
74
+ ).setHours(
75
+ date.getHours(),
76
+ date.getMinutes(),
77
+ ),
78
+ (date - other) / 60000,
79
+ relative,
80
+ );
81
+ case 'second':
82
+ return compensateDiff(
83
+ date,
84
+ other.setYear(
85
+ date.getYear(),
86
+ date.getMonth(),
87
+ date.getDate(),
88
+ ).setHours(
89
+ date.getHours(),
90
+ date.getMinutes(),
91
+ date.getSeconds(),
92
+ ),
93
+ (date - other) / 1000,
94
+ relative,
95
+ );
96
+ default:
97
+ throw new Error('Invalid time unit supplied');
98
+ }
99
+ };
100
+
8
101
  /**
9
102
  * Compensate the difference between two dates.
10
103
  * @param {DateTime} date The DateTime.
@@ -14,7 +107,7 @@ import { thresholds } from './vars.js';
14
107
  * @param {number} [compensation=1] The compensation offset.
15
108
  * @return {number} The compensated amount.
16
109
  */
17
- export function compensateDiff(date, other, amount, compensate = true, compensation = 1) {
110
+ function compensateDiff(date, other, amount, compensate = true, compensation = 1) {
18
111
  if (amount > 0) {
19
112
  amount = Math.floor(amount);
20
113
 
@@ -40,13 +133,15 @@ export function compensateDiff(date, other, amount, compensate = true, compensat
40
133
  */
41
134
  export function getBiggestDiff(date, other) {
42
135
  let lastResult;
43
- for (const timeUnit of ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']) {
44
- const relativeDiff = date.diff(other, { timeUnit });
136
+ for (const [timeUnit, diffMethod] of Object.entries(diffMethods)) {
137
+ const relativeDiff = date[diffMethod](other);
138
+
45
139
  if (lastResult && thresholds[timeUnit] && Math.abs(relativeDiff) >= thresholds[timeUnit]) {
46
140
  return lastResult;
47
141
  }
48
142
 
49
- const actualDiff = date.diff(other, { timeUnit, relative: false });
143
+ const actualDiff = date[diffMethod](other, { relative: false });
144
+
50
145
  if (actualDiff) {
51
146
  return [relativeDiff, timeUnit];
52
147
  }
@@ -90,57 +185,6 @@ export function getOffsetTime(date) {
90
185
  return date.getTime() - (date.getTimeZoneOffset() * 60000);
91
186
  };
92
187
 
93
- /**
94
- * Modify a DateTime by a duration.
95
- * @param {DateTime} date The DateTime.
96
- * @param {number} amount The amount to modify the date by.
97
- * @param {string} [timeUnit] The unit of time.
98
- * @return {DateTime} The DateTime object.
99
- */
100
- export function modify(date, amount, timeUnit) {
101
- timeUnit = timeUnit.toLowerCase();
102
-
103
- switch (timeUnit) {
104
- case 'second':
105
- case 'seconds':
106
- return date.setTime(
107
- date.getTime() + (amount * 1000),
108
- );
109
- case 'minute':
110
- case 'minutes':
111
- return date.setTime(
112
- date.getTime() + (amount * 60000),
113
- );
114
- case 'hour':
115
- case 'hours':
116
- return date.setTime(
117
- date.getTime() + (amount * 3600000),
118
- );
119
- case 'week':
120
- case 'weeks':
121
- return date.setDate(
122
- date.getDate() + (amount * 7),
123
- );
124
- case 'day':
125
- case 'days':
126
- return date.setDate(
127
- date.getDate() + amount,
128
- );
129
- case 'month':
130
- case 'months':
131
- return date.setMonth(
132
- date.getMonth() + amount,
133
- );
134
- case 'year':
135
- case 'years':
136
- return date.setYear(
137
- date.getYear() + amount,
138
- );
139
- default:
140
- throw new Error('Invalid time unit supplied');
141
- }
142
- };
143
-
144
188
  /**
145
189
  * Compare a literal format string with a date string.
146
190
  * @param {string} formatString The literal format string.
package/src/index.js CHANGED
@@ -3,9 +3,10 @@ import { fromArray, fromDate, fromFormat, fromISOString, fromTimestamp, now } fr
3
3
  import { dayOfYear, daysInMonth as _daysInMonth, daysInYear as _daysInYear, getDefaultLocale, getDefaultTimeZone, isLeapYear as _isLeapYear, setDateClamping, setDefaultLocale, setDefaultTimeZone } from './static/utility.js';
4
4
  import { getDate, getDay, getDayOfYear, getHours, getMilliseconds, getMinutes, getMonth, getQuarter, getSeconds, getTimestamp, getWeek, getWeekDay, getWeekDayInMonth, getWeekOfMonth, getWeekYear, getYear } from './prototype/attributes-get.js';
5
5
  import { setDate, setDay, setDayOfYear, setHours, setMilliseconds, setMinutes, setMonth, setQuarter, setSeconds, setTimestamp, setWeek, setWeekDay, setWeekDayInMonth, setWeekOfMonth, setWeekYear, setYear } from './prototype/attributes-set.js';
6
- import { add, endOf, startOf, sub } from './prototype/manipulate.js';
6
+ import { diff, diffInDays, diffInHours, diffInMinutes, diffInMonths, diffInSeconds, diffInWeeks, diffInYears, humanDiff, humanDiffInDays, humanDiffInHours, humanDiffInMinutes, humanDiffInMonths, humanDiffInSeconds, humanDiffInWeeks, humanDiffInYears, isAfter, isAfterDay, isAfterHour, isAfterMinute, isAfterMonth, isAfterSecond, isAfterWeek, isAfterYear, isBefore, isBeforeDay, isBeforeHour, isBeforeMinute, isBeforeMonth, isBeforeSecond, isBeforeWeek, isBeforeYear, isBetween, isBetweenDay, isBetweenHour, isBetweenMinute, isBetweenMonth, isBetweenSecond, isBetweenWeek, isBetweenYear, isSame, isSameDay, isSameHour, isSameMinute, isSameMonth, isSameSecond, isSameWeek, isSameYear, isSameOrAfter, isSameOrAfterDay, isSameOrAfterHour, isSameOrAfterMinute, isSameOrAfterMonth, isSameOrAfterSecond, isSameOrAfterWeek, isSameOrAfterYear, isSameOrBefore, isSameOrBeforeDay, isSameOrBeforeHour, isSameOrBeforeMinute, isSameOrBeforeMonth, isSameOrBeforeSecond, isSameOrBeforeWeek, isSameOrBeforeYear } from './prototype/comparisons.js';
7
+ import { addDay, addDays, addHour, addHours, addMinute, addMinutes, addMonth, addMonths, addSecond, addSeconds, addWeek, addWeeks, addYear, addYears, endOfDay, endOfHour, endOfMinute, endOfMonth, endOfQuarter, endOfSecond, endOfWeek, endOfYear, startOfDay, startOfHour, startOfMinute, startOfMonth, startOfQuarter, startOfSecond, startOfWeek, startOfYear, subDay, subDays, subHour, subHours, subMinute, subMinutes, subMonth, subMonths, subSecond, subSeconds, subWeek, subWeeks, subYear, subYears } from './prototype/manipulate.js';
7
8
  import { format, toDateString, toISOString, toString, toTimeString, toUTCString } from './prototype/output.js';
8
- import { dayName, dayPeriod, daysInMonth, daysInYear, diff, era, humanDiff, isAfter, isBefore, isBetween, isDST, isLeapYear, isSame, isSameOrAfter, isSameOrBefore, monthName, timeZoneName, weeksInYear } from './prototype/utility.js';
9
+ import { dayName, dayPeriod, daysInMonth, daysInYear, era, isDST, isLeapYear, monthName, timeZoneName, weeksInYear } from './prototype/utility.js';
9
10
 
10
11
  DateTime.dayOfYear = dayOfYear;
11
12
  DateTime.daysInMonth = _daysInMonth;
@@ -25,13 +26,40 @@ DateTime.setDefaultTimeZone = setDefaultTimeZone;
25
26
 
26
27
  const proto = DateTime.prototype;
27
28
 
28
- proto.add = add;
29
+ proto.addDay = addDay;
30
+ proto.addDays = addDays;
31
+ proto.addHour = addHour;
32
+ proto.addHours = addHours;
33
+ proto.addMinute = addMinute;
34
+ proto.addMinutes = addMinutes;
35
+ proto.addMonth = addMonth;
36
+ proto.addMonths = addMonths;
37
+ proto.addSecond = addSecond;
38
+ proto.addSeconds = addSeconds;
39
+ proto.addWeek = addWeek;
40
+ proto.addWeeks = addWeeks;
41
+ proto.addYear = addYear;
42
+ proto.addYears = addYears;
29
43
  proto.dayName = dayName;
30
44
  proto.dayPeriod = dayPeriod;
31
45
  proto.daysInMonth = daysInMonth;
32
46
  proto.daysInYear = daysInYear;
33
47
  proto.diff = diff;
34
- proto.endOf = endOf;
48
+ proto.diffInDays = diffInDays;
49
+ proto.diffInHours = diffInHours;
50
+ proto.diffInMinutes = diffInMinutes;
51
+ proto.diffInMonths = diffInMonths;
52
+ proto.diffInSeconds = diffInSeconds;
53
+ proto.diffInWeeks = diffInWeeks;
54
+ proto.diffInYears = diffInYears;
55
+ proto.endOfDay = endOfDay;
56
+ proto.endOfHour = endOfHour;
57
+ proto.endOfMinute = endOfMinute;
58
+ proto.endOfMonth = endOfMonth;
59
+ proto.endOfQuarter = endOfQuarter;
60
+ proto.endOfSecond = endOfSecond;
61
+ proto.endOfWeek = endOfWeek;
62
+ proto.endOfYear = endOfYear;
35
63
  proto.era = era;
36
64
  proto.format = format;
37
65
  proto.getDate = getDate;
@@ -51,14 +79,63 @@ proto.getWeekOfMonth = getWeekOfMonth;
51
79
  proto.getWeekYear = getWeekYear;
52
80
  proto.getYear = getYear;
53
81
  proto.humanDiff = humanDiff;
82
+ proto.humanDiffInDays = humanDiffInDays;
83
+ proto.humanDiffInHours = humanDiffInHours;
84
+ proto.humanDiffInMinutes = humanDiffInMinutes;
85
+ proto.humanDiffInMonths = humanDiffInMonths;
86
+ proto.humanDiffInSeconds = humanDiffInSeconds;
87
+ proto.humanDiffInWeeks = humanDiffInWeeks;
88
+ proto.humanDiffInYears = humanDiffInYears;
54
89
  proto.isAfter = isAfter;
90
+ proto.isAfterDay = isAfterDay;
91
+ proto.isAfterHour = isAfterHour;
92
+ proto.isAfterMinute = isAfterMinute;
93
+ proto.isAfterMonth = isAfterMonth;
94
+ proto.isAfterSecond = isAfterSecond;
95
+ proto.isAfterWeek = isAfterWeek;
96
+ proto.isAfterYear = isAfterYear;
55
97
  proto.isBefore = isBefore;
98
+ proto.isBeforeDay = isBeforeDay;
99
+ proto.isBeforeHour = isBeforeHour;
100
+ proto.isBeforeMinute = isBeforeMinute;
101
+ proto.isBeforeMonth = isBeforeMonth;
102
+ proto.isBeforeSecond = isBeforeSecond;
103
+ proto.isBeforeWeek = isBeforeWeek;
104
+ proto.isBeforeYear = isBeforeYear;
56
105
  proto.isBetween = isBetween;
106
+ proto.isBetweenDay = isBetweenDay;
107
+ proto.isBetweenHour = isBetweenHour;
108
+ proto.isBetweenMinute = isBetweenMinute;
109
+ proto.isBetweenMonth = isBetweenMonth;
110
+ proto.isBetweenSecond = isBetweenSecond;
111
+ proto.isBetweenWeek = isBetweenWeek;
112
+ proto.isBetweenYear = isBetweenYear;
57
113
  proto.isDST = isDST;
58
114
  proto.isLeapYear = isLeapYear;
59
115
  proto.isSame = isSame;
116
+ proto.isSameDay = isSameDay;
117
+ proto.isSameHour = isSameHour;
118
+ proto.isSameMinute = isSameMinute;
119
+ proto.isSameMonth = isSameMonth;
120
+ proto.isSameSecond = isSameSecond;
121
+ proto.isSameWeek = isSameWeek;
122
+ proto.isSameYear = isSameYear;
60
123
  proto.isSameOrAfter = isSameOrAfter;
124
+ proto.isSameOrAfterDay = isSameOrAfterDay;
125
+ proto.isSameOrAfterHour = isSameOrAfterHour;
126
+ proto.isSameOrAfterMinute = isSameOrAfterMinute;
127
+ proto.isSameOrAfterMonth = isSameOrAfterMonth;
128
+ proto.isSameOrAfterSecond = isSameOrAfterSecond;
129
+ proto.isSameOrAfterWeek = isSameOrAfterWeek;
130
+ proto.isSameOrAfterYear = isSameOrAfterYear;
61
131
  proto.isSameOrBefore = isSameOrBefore;
132
+ proto.isSameOrBeforeDay = isSameOrBeforeDay;
133
+ proto.isSameOrBeforeHour = isSameOrBeforeHour;
134
+ proto.isSameOrBeforeMinute = isSameOrBeforeMinute;
135
+ proto.isSameOrBeforeMonth = isSameOrBeforeMonth;
136
+ proto.isSameOrBeforeSecond = isSameOrBeforeSecond;
137
+ proto.isSameOrBeforeWeek = isSameOrBeforeWeek;
138
+ proto.isSameOrBeforeYear = isSameOrBeforeYear;
62
139
  proto.monthName = monthName;
63
140
  proto.setDate = setDate;
64
141
  proto.setDay = setDay;
@@ -76,8 +153,28 @@ proto.setWeekDayInMonth = setWeekDayInMonth;
76
153
  proto.setWeekOfMonth = setWeekOfMonth;
77
154
  proto.setWeekYear = setWeekYear;
78
155
  proto.setYear = setYear;
79
- proto.startOf = startOf;
80
- proto.sub = sub;
156
+ proto.startOfDay = startOfDay;
157
+ proto.startOfHour = startOfHour;
158
+ proto.startOfMinute = startOfMinute;
159
+ proto.startOfMonth = startOfMonth;
160
+ proto.startOfQuarter = startOfQuarter;
161
+ proto.startOfSecond = startOfSecond;
162
+ proto.startOfWeek = startOfWeek;
163
+ proto.startOfYear = startOfYear;
164
+ proto.subDay = subDay;
165
+ proto.subDays = subDays;
166
+ proto.subHour = subHour;
167
+ proto.subHours = subHours;
168
+ proto.subMinute = subMinute;
169
+ proto.subMinutes = subMinutes;
170
+ proto.subMonth = subMonth;
171
+ proto.subMonths = subMonths;
172
+ proto.subSecond = subSecond;
173
+ proto.subSeconds = subSeconds;
174
+ proto.subWeek = subWeek;
175
+ proto.subWeeks = subWeeks;
176
+ proto.subYear = subYear;
177
+ proto.subYears = subYears;
81
178
  proto.timeZoneName = timeZoneName;
82
179
  proto.toDateString = toDateString;
83
180
  proto.toISOString = toISOString;
@@ -95,7 +95,7 @@ export function getTimestamp() {
95
95
  * @return {number} The local week. (1, 53)
96
96
  */
97
97
  export function getWeek() {
98
- const thisWeek = this.startOf('day').setWeekDay(1);
98
+ const thisWeek = this.startOfDay().setWeekDay(1);
99
99
  const firstWeek = thisWeek.setWeek(1, 1);
100
100
 
101
101
  return 1 +