|
| 1 | +import Locale from '../Locale'; |
| 2 | + |
| 3 | +var i18nLib = Locale.lib(); |
| 4 | + |
| 5 | +global._Date = global._Date || Date; |
| 6 | + |
| 7 | +var DateUtil = { |
| 8 | + // localEpoch: function(epochSeconds) { |
| 9 | + // var d = new Date(0); // The 0 there is the key, which sets the date to the epoch |
| 10 | + // d.setUTCSeconds(epochSeconds); |
| 11 | + // return d; |
| 12 | + // }, |
| 13 | + |
| 14 | + now: function() { |
| 15 | + return new _Date(); |
| 16 | + }, |
| 17 | + |
| 18 | + minutes: function(number) { |
| 19 | + return number * 60 * 1000; |
| 20 | + }, |
| 21 | + |
| 22 | + hours: function(number) { |
| 23 | + return number * this.minutes(60); |
| 24 | + }, |
| 25 | + |
| 26 | + days: function(number) { |
| 27 | + return number * this.hours(24); |
| 28 | + }, |
| 29 | + |
| 30 | + timeAgoInWords: function(milliseconds) { |
| 31 | + if (milliseconds < (new Date('January 01, 1970 00:00:00')).getTime()) { |
| 32 | + console.warn('timeAgoInSeconds requires milliseconds >= Epoch'); |
| 33 | + return null; |
| 34 | + } |
| 35 | + const now = this.now().getTime(), |
| 36 | + minutesAgo = Math.floor((now - milliseconds) / 1000 / 60); |
| 37 | + |
| 38 | + if (minutesAgo < 1) return i18n.t('time_ago_less_than'); |
| 39 | + if (minutesAgo === 1) return i18n.t('time_ago_min'); |
| 40 | + if (minutesAgo < 60) return i18n.t('time_ago_mins', {minutes: minutesAgo}); |
| 41 | + |
| 42 | + const hoursAgo = Math.floor(minutesAgo / 60); |
| 43 | + if (hoursAgo === 1) return i18n.t('time_ago_hour'); |
| 44 | + if (hoursAgo < 24) return i18n.t('time_ago_hours', {hours: hoursAgo}); |
| 45 | + |
| 46 | + const daysAgo = Math.floor(hoursAgo / 24); |
| 47 | + if (daysAgo === 1) return i18n.t('time_ago_day'); |
| 48 | + if (daysAgo < 7) return i18n.t('time_ago_days', {days: daysAgo}); |
| 49 | + |
| 50 | + const weeksAgo = Math.floor(daysAgo / 7); |
| 51 | + if (weeksAgo === 1) return i18n.t('time_ago_week'); |
| 52 | + return i18n.t('time_ago_weeks', {weeks: weeksAgo}); |
| 53 | + }, |
| 54 | + |
| 55 | + isToday(date) { |
| 56 | + var parsedDate = this.parse(date); |
| 57 | + if (!parsedDate) return false; |
| 58 | + |
| 59 | + return parsedDate.toDateString() === this.now().toDateString(); |
| 60 | + }, |
| 61 | + |
| 62 | + isPast(date) { |
| 63 | + var parsedDate = this.parse(date); |
| 64 | + if (!parsedDate) return false; |
| 65 | + |
| 66 | + return parsedDate < this.now(); |
| 67 | + }, |
| 68 | + |
| 69 | + isFuture(date) { |
| 70 | + return !this.isPast(date); |
| 71 | + }, |
| 72 | + |
| 73 | + // use this one instead of strftime, please. it's better for i18n |
| 74 | + format: function(date, name) { |
| 75 | + var pattern = i18nLib.t('datetime.' + name); |
| 76 | + return this.strftime(date, pattern); |
| 77 | + }, |
| 78 | + |
| 79 | + // strftime stuff we support: |
| 80 | + // https://github.com/fnando/i18n-js#date-formatting |
| 81 | + // %+A : Today, Tomorrow, Monday |
| 82 | + strftime: function(date, pattern) { |
| 83 | + date = this.parse(date); |
| 84 | + if (!date) return null; |
| 85 | + |
| 86 | + if (pattern.indexOf('%+A') >= 0) { |
| 87 | + // %+A : Today, Tomorrow, Monday |
| 88 | + var dayName = '%A'; // normal one |
| 89 | + var today = new _Date(); |
| 90 | + var tomorrow = new _Date(); |
| 91 | + tomorrow.setDate(tomorrow.getDate() + 1); |
| 92 | + |
| 93 | + if (date.toDateString() === today.toDateString()) { |
| 94 | + dayName = i18n.t('today'); |
| 95 | + } |
| 96 | + else if (date.toDateString() === tomorrow.toDateString()) { |
| 97 | + dayName = i18n.t('tomorrow'); |
| 98 | + } |
| 99 | + |
| 100 | + pattern = pattern.replace(/%\+A/g, dayName); |
| 101 | + } |
| 102 | + |
| 103 | + return i18nLib.strftime(date, pattern); |
| 104 | + }, |
| 105 | + |
| 106 | + localISOString: function(date) { |
| 107 | + var d = this.parse(date); |
| 108 | + d.setHours(0, -d.getTimezoneOffset(), 0, 0); |
| 109 | + |
| 110 | + return d.toISOString().slice(0, 10); |
| 111 | + }, |
| 112 | + |
| 113 | + dayWindowName: function(date) { |
| 114 | + date = this.parse(date); |
| 115 | + if (!date) return null; |
| 116 | + |
| 117 | + var hour = date.getHours(); |
| 118 | + if (hour < 12) { |
| 119 | + return i18n.t('morning'); |
| 120 | + } |
| 121 | + else if (hour < 16) { |
| 122 | + return i18n.t('afternoon'); |
| 123 | + } |
| 124 | + else { |
| 125 | + return i18n.t('evening'); |
| 126 | + } |
| 127 | + }, |
| 128 | + |
| 129 | + parse: function(intOrDateOrString) { |
| 130 | + if (intOrDateOrString === null) return null; |
| 131 | + |
| 132 | + // have to know if it's an number if it's in millseconds or seconds |
| 133 | + if (typeof intOrDateOrString === 'number') { |
| 134 | + // using number of digits to decide (12 digits is 1973) |
| 135 | + if (intOrDateOrString.toString().length <= 12) { |
| 136 | + intOrDateOrString = intOrDateOrString * 1000; |
| 137 | + } |
| 138 | + } |
| 139 | + return i18nLib.parseDate(intOrDateOrString); |
| 140 | + }, |
| 141 | + |
| 142 | + toEpoch: function(intOrDateOrString) { |
| 143 | + var date = this.parse(intOrDateOrString); |
| 144 | + if (date === null) return null; |
| 145 | + |
| 146 | + return Math.round(date.getTime() / 1000); |
| 147 | + }, |
| 148 | +}; |
| 149 | + |
| 150 | +var i18n = Locale.key('DateUtil', { |
| 151 | + morning: 'Morning', |
| 152 | + afternoon: 'Afternoon', |
| 153 | + evening: 'Evening', |
| 154 | + today: 'Today', |
| 155 | + tomorrow: 'Tomorrow', |
| 156 | + time_ago_less_than: 'less than 1 min ago', |
| 157 | + time_ago_min: '1 min ago', |
| 158 | + time_ago_mins: '%{minutes} mins ago', |
| 159 | + time_ago_hour: '1 hour ago', |
| 160 | + time_ago_hours: '%{hours} hours ago', |
| 161 | + time_ago_day: '1 day ago', |
| 162 | + time_ago_days: '%{days} days ago', |
| 163 | + time_ago_week: '1 week ago', |
| 164 | + time_ago_weeks: '%{weeks} weeks ago', |
| 165 | +}); |
| 166 | + |
| 167 | +export default DateUtil; |
0 commit comments