|
| 1 | +import type { Faker } from '.'; |
| 2 | + |
| 3 | +export class _Date { |
| 4 | + constructor(private readonly faker: Faker) { |
| 5 | + // Bind `this` so namespaced is working correctly |
| 6 | + for (const name of Object.getOwnPropertyNames(_Date.prototype)) { |
| 7 | + if (name === 'constructor' || typeof this[name] !== 'function') { |
| 8 | + continue; |
| 9 | + } |
| 10 | + this[name] = this[name].bind(this); |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * past |
| 16 | + * |
| 17 | + * @method faker.date.past |
| 18 | + * @param years |
| 19 | + * @param refDate |
| 20 | + */ |
| 21 | + past(years?: number, refDate?: string): Date { |
| 22 | + let date = new Date(); |
| 23 | + if (typeof refDate !== 'undefined') { |
| 24 | + date = new Date(Date.parse(refDate)); |
| 25 | + } |
| 26 | + |
| 27 | + const range = { |
| 28 | + min: 1000, |
| 29 | + max: (years || 1) * 365 * 24 * 3600 * 1000, |
| 30 | + }; |
| 31 | + |
| 32 | + let past = date.getTime(); |
| 33 | + past -= this.faker.datatype.number(range); // some time from now to N years ago, in milliseconds |
| 34 | + date.setTime(past); |
| 35 | + |
| 36 | + return date; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * future |
| 41 | + * |
| 42 | + * @method faker.date.future |
| 43 | + * @param years |
| 44 | + * @param refDate |
| 45 | + */ |
| 46 | + future(years?: number, refDate?: string): Date { |
| 47 | + let date = new Date(); |
| 48 | + if (typeof refDate !== 'undefined') { |
| 49 | + date = new Date(Date.parse(refDate)); |
| 50 | + } |
| 51 | + |
| 52 | + const range = { |
| 53 | + min: 1000, |
| 54 | + max: (years || 1) * 365 * 24 * 3600 * 1000, |
| 55 | + }; |
| 56 | + |
| 57 | + let future = date.getTime(); |
| 58 | + future += this.faker.datatype.number(range); // some time from now to N years later, in milliseconds |
| 59 | + date.setTime(future); |
| 60 | + |
| 61 | + return date; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * between |
| 66 | + * |
| 67 | + * @method faker.date.between |
| 68 | + * @param from |
| 69 | + * @param to |
| 70 | + */ |
| 71 | + between(from: string, to: string): Date { |
| 72 | + const fromMilli = Date.parse(from); |
| 73 | + const dateOffset = this.faker.datatype.number(Date.parse(to) - fromMilli); |
| 74 | + |
| 75 | + const newDate = new Date(fromMilli + dateOffset); |
| 76 | + |
| 77 | + return newDate; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * betweens |
| 82 | + * |
| 83 | + * @method faker.date.between |
| 84 | + * @param from |
| 85 | + * @param to |
| 86 | + * @param num |
| 87 | + */ |
| 88 | + betweens(from: string, to: string, num?: number): Date[] { |
| 89 | + if (typeof num == 'undefined') { |
| 90 | + num = 3; |
| 91 | + } |
| 92 | + const newDates: Date[] = []; |
| 93 | + let fromMilli = Date.parse(from); |
| 94 | + const dateOffset = (Date.parse(to) - fromMilli) / (num + 1); |
| 95 | + let lastDate: string | Date = from; |
| 96 | + for (let i = 0; i < num; i++) { |
| 97 | + // TODO @Shinigami92 2022-01-11: It may be a bug that `lastDate` is passed to parse if it's a `Date` not a `string` |
| 98 | + // @ts-expect-error |
| 99 | + fromMilli = Date.parse(lastDate); |
| 100 | + lastDate = new Date(fromMilli + dateOffset); |
| 101 | + newDates.push(lastDate); |
| 102 | + } |
| 103 | + return newDates; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * recent |
| 108 | + * |
| 109 | + * @method faker.date.recent |
| 110 | + * @param days |
| 111 | + * @param refDate |
| 112 | + */ |
| 113 | + recent(days?: number, refDate?: string): Date { |
| 114 | + let date = new Date(); |
| 115 | + if (typeof refDate !== 'undefined') { |
| 116 | + date = new Date(Date.parse(refDate)); |
| 117 | + } |
| 118 | + |
| 119 | + const range = { |
| 120 | + min: 1000, |
| 121 | + max: (days || 1) * 24 * 3600 * 1000, |
| 122 | + }; |
| 123 | + |
| 124 | + let future = date.getTime(); |
| 125 | + future -= this.faker.datatype.number(range); // some time from now to N days ago, in milliseconds |
| 126 | + date.setTime(future); |
| 127 | + |
| 128 | + return date; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * soon |
| 133 | + * |
| 134 | + * @method faker.date.soon |
| 135 | + * @param days |
| 136 | + * @param refDate |
| 137 | + */ |
| 138 | + soon(days?: number, refDate?: string): Date { |
| 139 | + let date = new Date(); |
| 140 | + if (typeof refDate !== 'undefined') { |
| 141 | + date = new Date(Date.parse(refDate)); |
| 142 | + } |
| 143 | + |
| 144 | + const range = { |
| 145 | + min: 1000, |
| 146 | + max: (days || 1) * 24 * 3600 * 1000, |
| 147 | + }; |
| 148 | + |
| 149 | + let future = date.getTime(); |
| 150 | + future += this.faker.datatype.number(range); // some time from now to N days later, in milliseconds |
| 151 | + date.setTime(future); |
| 152 | + |
| 153 | + return date; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * month |
| 158 | + * |
| 159 | + * @method faker.date.month |
| 160 | + * @param options |
| 161 | + */ |
| 162 | + month(options?: { abbr?: boolean; context?: boolean }) { |
| 163 | + options = options || {}; |
| 164 | + |
| 165 | + let type = 'wide'; |
| 166 | + if (options.abbr) { |
| 167 | + type = 'abbr'; |
| 168 | + } |
| 169 | + if ( |
| 170 | + options.context && |
| 171 | + typeof this.faker.definitions.date.month[type + '_context'] !== |
| 172 | + 'undefined' |
| 173 | + ) { |
| 174 | + type += '_context'; |
| 175 | + } |
| 176 | + |
| 177 | + const source = this.faker.definitions.date.month[type]; |
| 178 | + |
| 179 | + return this.faker.random.arrayElement(source); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * weekday |
| 184 | + * |
| 185 | + * @method faker.date.weekday |
| 186 | + * @param options |
| 187 | + */ |
| 188 | + weekday(options?: { abbr?: boolean; context?: boolean }) { |
| 189 | + options = options || {}; |
| 190 | + |
| 191 | + let type = 'wide'; |
| 192 | + if (options.abbr) { |
| 193 | + type = 'abbr'; |
| 194 | + } |
| 195 | + if ( |
| 196 | + options.context && |
| 197 | + typeof this.faker.definitions.date.weekday[type + '_context'] !== |
| 198 | + 'undefined' |
| 199 | + ) { |
| 200 | + type += '_context'; |
| 201 | + } |
| 202 | + |
| 203 | + const source = this.faker.definitions.date.weekday[type]; |
| 204 | + |
| 205 | + return this.faker.random.arrayElement(source); |
| 206 | + } |
| 207 | +} |
0 commit comments