Commit 21a54d4
Add %J and %Q format specifiers for compact date/time formatting (nushell#16588)
## Summary
Adds two new format specifiers to the `format date` and `into datetime`
commands that provide compact, human-readable date and time formatting
with improved modularity:
- **%J**: "Joined date" format - expands to `%Y%m%d` producing compact
dates like `20211022`
- **%Q**: "seQuential time" format - expands to `%H%M%S` producing
compact times like `200012`
- **Use case**: Ideal for flexible timestamp formatting, backup files,
logs, and sortable date/time components
- **Rationale**: Replaces single `%K` format with modular date/time
components after maintainer feedback
- **Flexibility**: Can be used individually or combined (e.g., `%J_%Q`,
`%J-%Q`) for custom formats
- **Consistency**: Both `format date` and `into datetime` use the same
format specifications
## Examples
```nushell
# Individual date formatting
"2021-10-22 20:00:12 +01:00" | format date "%J"
# Output: 20211022
# Individual time formatting
"2021-10-22 20:00:12 +01:00" | format date "%Q"
# Output: 200012
# Combined for full timestamp (equivalent to previous %K)
"2021-10-22 20:00:12 +01:00" | format date "%J_%Q"
# Output: 20211022_200012
# Parse compact date with into datetime
"20211022" | into datetime --format "%J"
# Parse compact time with into datetime
"200012" | into datetime --format "%Q"
# Round-trip functionality
"2021-10-22 20:00:12 +01:00" | format date "%J_%Q" | into datetime --format "%J_%Q" | format date "%J_%Q"
# Output: 20211022_200012
# Custom separators for different use cases
date now | format date "backup_%J-%Q.sql"
# Output: backup_20250918-131144.sql
# Date-only file naming
date now | format date "report_%J.txt"
# Output: report_20250918.txt
```
## Release notes summary - What our users need to know
The `format date` and `into datetime` commands now support two new
format specifiers for creating compact, sortable date and time
components:
- **%J** produces compact dates in `YYYYMMDD` format (e.g., `20250918`)
- **%Q** produces compact times in `HHMMSS` format (e.g., `131144`)
These can be used individually for date-only or time-only formatting, or
combined for full timestamps (`%J_%Q` produces `20250918_131144`),
providing more flexibility than a single combined format.
Perfect for:
- Backup file naming with custom separators
- Log file timestamps with flexible formatting
- Date-only or time-only compact representations
- Any scenario requiring sortable, human-readable date/time components
Both commands use the same format specifications, ensuring consistency
when parsing and formatting dates.
This addition is fully backward compatible - all existing format
specifiers continue to work unchanged.
## Implementation Details
- **Preprocessing approach**: Replaces `%J` with `%Y%m%d` and `%Q` with
`%H%M%S` before passing to chrono
- **Help integration**: Both specifiers appear in `format date -l` with
proper example generation
- **Letter choice rationale**:
- `%J` for "Joined date" - unused in major standards (lowercase %j is
day-of-year)
- `%Q` for "seQuential time" - unused in major time formatting standards
- **Modular design**: Users can format dates and times independently or
combine as needed
- **Consistent behavior**: Same format handling in both `format date`
and `into datetime`
- **No breaking changes**: Existing functionality remains unchanged
- **Performance**: Minimal overhead, only processes when specifiers are
used
## Test Results
- ✅ `format date -l` shows both new specifications correctly
- ✅ `%J` format produces expected date format `YYYYMMDD`
- ✅ `%Q` format produces expected time format `HHMMSS`
- ✅ `%J_%Q` combined produces expected timestamp format
- ✅ `into datetime --format "%J"` parses compact dates correctly
- ✅ `into datetime --format "%Q"` parses compact times correctly
- ✅ `into datetime --format "%J_%Q"` parses compact timestamps correctly
- ✅ Round-trip functionality between both commands works for all formats
- ✅ Individual and combined format specifiers work with current time
- ✅ No regression in existing format specifiers
- ✅ All existing tests pass with updated test suite
- ✅ Comprehensive test coverage for new functionality
---------
Co-authored-by: claude <[email protected]>
Co-authored-by: Claude <[email protected]>1 parent 9ebb02d commit 21a54d4
File tree
5 files changed
+113
-12
lines changed- crates/nu-command
- src
- conversions/into
- date
- strings/format
- tests/commands
- date
5 files changed
+113
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
422 | 422 | | |
423 | 423 | | |
424 | 424 | | |
425 | | - | |
426 | | - | |
427 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
428 | 434 | | |
429 | 435 | | |
430 | 436 | | |
| |||
464 | 470 | | |
465 | 471 | | |
466 | 472 | | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
| 473 | + | |
| 474 | + | |
471 | 475 | | |
472 | 476 | | |
473 | 477 | | |
| |||
478 | 482 | | |
479 | 483 | | |
480 | 484 | | |
481 | | - | |
| 485 | + | |
482 | 486 | | |
483 | 487 | | |
484 | 488 | | |
485 | 489 | | |
486 | 490 | | |
487 | | - | |
| 491 | + | |
488 | 492 | | |
489 | | - | |
| 493 | + | |
490 | 494 | | |
491 | 495 | | |
492 | 496 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
250 | 258 | | |
251 | 259 | | |
252 | 260 | | |
| |||
264 | 272 | | |
265 | 273 | | |
266 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
267 | 282 | | |
268 | 283 | | |
269 | 284 | | |
270 | | - | |
| 285 | + | |
271 | 286 | | |
272 | 287 | | |
273 | 288 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
203 | 207 | | |
204 | 208 | | |
205 | 209 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
12 | 49 | | |
13 | 50 | | |
14 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
0 commit comments