playbook_ui 13.2.0.pre.alpha.PLAY986dateTimeRounding1150 → 13.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29507bd994a736c71238be60bea9b2b269b5f1a40ebabac51280e93f20d493b2
4
- data.tar.gz: fb81b0db3f2615422f9d827859951373dd50d34b5a94aa06f9bc42324d7942a5
3
+ metadata.gz: 6c03b530a4b2b16b5dcf7ea348a4aa6a66db48f68359fed8469e47746c6fad2c
4
+ data.tar.gz: 5695a441ba55870888683da55c49814c94de842a952614d711325bfd1813fe35
5
5
  SHA512:
6
- metadata.gz: ed2c60ef1307bb8c3ba61cb9d8983a219c6e82568e86ec9e8608ae7efe057094f6307da32f77c4ad5a08d11f9fd5ee5f2b568c1502d3426d2a82416795c846ff
7
- data.tar.gz: e96648e381e82365ec5b439cdc3166f08c6c33e7cfc71d42a79415f46a5e3c1f696ccda273952633e858c7802a6746ff45a009ac164e5211ab8af18f7646805d
6
+ metadata.gz: cb2dd83ad7e0030f750adb29cab24f33d979922ac9763e35de399c8df427053ae501e0b24a168116c6fccf876885bf7fc9e7e0356b94bae6ef7ccb14cf7bf45c
7
+ data.tar.gz: 51fa2673e72139fdfce189b16a2364430eb8518fb9480d5b8c203bd66956478654506a20e358fbd6e6de955c97270503ed69f92857fe51789ddc30595daff9b3
@@ -121,61 +121,30 @@ export const toIso = (newDate: Date | string): string => {
121
121
  }
122
122
 
123
123
  export const fromNow = (newDate: Date | string): string => {
124
- const today = new Date()
125
- const formattedDate = formatDate(newDate)
126
-
127
- const startDate = formattedDate.getTime()
128
- const endDate = today.getTime()
124
+ const startDate = formatDate(newDate).getTime()
125
+ const endDate = new Date().getTime()
129
126
  const elapsedTime = endDate - startDate
130
-
131
- // For years/months, don't use elapsedTime due to rounding
132
- const differenceInYears = today.getFullYear() - formattedDate.getFullYear()
133
- const differenceInMonths = () => {
134
- let months = differenceInYears * 12
135
- months -= formattedDate.getMonth()
136
- months += today.getMonth()
137
- return months
138
- }
139
-
140
- const FOURTY_FIVE_SECONDS = 45000
141
- const NINETY_SECONDS = 90000
142
-
143
- const FOURTY_FIVE_MINUTES = 2670000
144
- const NINETY_MINUTES = 5400000
145
-
146
- const TWENTY_TWO_HOURS = 77400000
147
- const THIRTY_SIX_HOURS = 129600000
148
-
149
- const TWENTY_SIX_DAYS = 2203200000
150
- const FOURTY_FIVE_DAYS = 3888000000
151
-
152
- const TEN_AND_A_HALF_MONTHS = 27560000000
153
-
154
- const MILLISECONDS_IN_A_MINUTE = 60000
155
- const MILLISECONDS_IN_A_HOUR = 3600000
156
- const MILLISECONDS_IN_A_DAY = 86400000
157
-
158
- let elapsedTimeString = differenceInYears === 1 ? `${differenceInYears} year ago` : `${differenceInYears} years ago` // 320 days to 1+ year: "x year(s) ago"
159
-
160
- // Inspiration: https://momentjs.com/docs/#/displaying/fromnow/
161
- const intervals = [
162
- { min: 0, max: FOURTY_FIVE_SECONDS, value: "a few seconds ago" }, // 0-44.99 seconds
163
- { min: FOURTY_FIVE_SECONDS, max: NINETY_SECONDS, value: "a minute ago" }, // 45-89.99 seconds
164
- { min: NINETY_SECONDS, max: FOURTY_FIVE_MINUTES, value: `${Math.round(elapsedTime / MILLISECONDS_IN_A_MINUTE)} minutes ago` }, // 90s-44.49 minutes: "2 minutes ago ... 44 minutes ago"
165
- { min: FOURTY_FIVE_MINUTES, max: NINETY_MINUTES, value: "an hour ago" }, // 44.5-89.99 minutes
166
- { min: NINETY_MINUTES, max: TWENTY_TWO_HOURS, value: `${Math.round(elapsedTime / MILLISECONDS_IN_A_HOUR)} hours ago` }, // 90m-21.49 hours: "2 hours ago ... 21 hours ago"
167
- { min: TWENTY_TWO_HOURS, max: THIRTY_SIX_HOURS, value: "a day ago" }, // 21.5-35.99 hours
168
- { min: THIRTY_SIX_HOURS, max: TWENTY_SIX_DAYS, value: `${Math.round(elapsedTime / MILLISECONDS_IN_A_DAY)} days ago` }, // 36h-25.49 days: "2 days ago ... 25 days ago"
169
- { min: TWENTY_SIX_DAYS, max: FOURTY_FIVE_DAYS, value: "a month ago" }, // 25.5-44.99 days
170
- { min: FOURTY_FIVE_DAYS, max: TEN_AND_A_HALF_MONTHS, value: `${differenceInMonths()} months ago` }, // 45 days to 319 days: "2 months ago ... 10 months ago"
171
- ]
172
-
173
- for (const interval of intervals) {
174
- const { min, max, value } = interval
175
-
176
- if (elapsedTime >= min && elapsedTime < max) {
177
- elapsedTimeString = value
178
- break
127
+ let elapsedTimeString = `${Math.round(elapsedTime / (365.25 * 24 * 60 * 60 * 1000))} years ago`; // 730+ days
128
+
129
+ const MILLISECONDS_IN_A_MONTH = 30.44 * 24 * 60 * 60 * 1000
130
+
131
+ const elapsedTimeData = [
132
+ { min: 0, max: 44999, value: "a few seconds ago" }, // 0-44 seconds
133
+ { min: 45000, max: 89999, value: "a minute ago" }, // 45-89 seconds
134
+ { min: 90000, max: 2649999, value: `${Math.round(elapsedTime / 60000)} minutes ago`}, // 90s-44 minutes
135
+ { min: 2650000, max: 7299999, value: "an hour ago" }, // 45-120 minutes
136
+ { min: 7300000, max: 75699999, value: `${Math.round(elapsedTime / 3600000)} hours ago`}, // 2-21 hours
137
+ { min: 75700000, max: 172899999, value: "a day ago" }, // 22-48 hours
138
+ { min: 172900000, max: 2169999999, value: `${Math.round(elapsedTime / 86400000)} days ago`}, // 2-25 days
139
+ { min: 2170000000, max: 5184999999, value: "a month ago"}, // 26-60 days
140
+ { min: 5185000000, max: 27561699999, value: `${Math.round(elapsedTime / MILLISECONDS_IN_A_MONTH)} months ago`}, // 60-319 days
141
+ { min: 27561700000, max: 63072999999, value: "a year ago"}, // 320-730 days
142
+ ];
143
+
144
+ for (const timeDate of elapsedTimeData) {
145
+ if (elapsedTime >= timeDate.min && elapsedTime <= timeDate.max) {
146
+ elapsedTimeString = timeDate.value;
147
+ break;
179
148
  }
180
149
  }
181
150