* Generates a date string for a git commit using the same format as `git log`. * * @param options The optional options object. * @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`. * * @example * faker.git.commitDate() //
(
options: {
/**
* The date to use as reference point for the commit.
*
* @default faker.defaultRefDate()
*/
refDate?: string | Date | number;
} = {}
)
| 135 | * @since 8.0.0 |
| 136 | */ |
| 137 | commitDate( |
| 138 | options: { |
| 139 | /** |
| 140 | * The date to use as reference point for the commit. |
| 141 | * |
| 142 | * @default faker.defaultRefDate() |
| 143 | */ |
| 144 | refDate?: string | Date | number; |
| 145 | } = {} |
| 146 | ): string { |
| 147 | const { refDate = this.faker.defaultRefDate() } = options; |
| 148 | // Git uses a non-standard date format for commits by default per https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-log.html |
| 149 | // --date=default is the default format, and is based on ctime(3) output. It shows a single line with three-letter day of the week, three-letter month, day-of-month, hour-minute-seconds in "HH:MM:SS" format, followed by 4-digit year, plus timezone information, unless the local time zone is used, e.g. Thu Jan 1 00:00:00 1970 +0000. |
| 150 | // To avoid relying on the Intl global which may not be available in all environments, we implement a custom date format using built-in Javascript date functions. |
| 151 | const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
| 152 | const months = [ |
| 153 | 'Jan', |
| 154 | 'Feb', |
| 155 | 'Mar', |
| 156 | 'Apr', |
| 157 | 'May', |
| 158 | 'Jun', |
| 159 | 'Jul', |
| 160 | 'Aug', |
| 161 | 'Sep', |
| 162 | 'Oct', |
| 163 | 'Nov', |
| 164 | 'Dec', |
| 165 | ]; |
| 166 | |
| 167 | const date = this.faker.date.recent({ days: 1, refDate }); |
| 168 | const day = days[date.getUTCDay()]; |
| 169 | const month = months[date.getUTCMonth()]; |
| 170 | const dayOfMonth = date.getUTCDate(); |
| 171 | const hours = date.getUTCHours().toString().padStart(2, '0'); |
| 172 | const minutes = date.getUTCMinutes().toString().padStart(2, '0'); |
| 173 | const seconds = date.getUTCSeconds().toString().padStart(2, '0'); |
| 174 | const year = date.getUTCFullYear(); |
| 175 | const timezone = this.faker.number.int({ min: -11, max: 12 }); |
| 176 | const timezoneHours = Math.abs(timezone).toString().padStart(2, '0'); |
| 177 | const timezoneMinutes = '00'; |
| 178 | const timezoneSign = timezone >= 0 ? '+' : '-'; |
| 179 | return `${day} ${month} ${dayOfMonth} ${hours}:${minutes}:${seconds} ${year} ${timezoneSign}${timezoneHours}${timezoneMinutes}`; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Generates a random commit sha. |
no test coverage detected