Home Explore Blog CI



nushell

3rd chunk of `cookbook/parsing_git_log.md`
5ccd0d07f99b34f7cf2a7e939aeb04dd2e105a150380b91100000001000015ee
# =>  4 │ e3100e6a │ Fix alias in         │ Luca Trevisani   │ lucatrv@hotmail.com    │ Mon, 28 Feb 2022
# =>    │          │ `docs/sample_config/ │                  │                        │ 22:47:14 +0100
# =>    │          │ config.toml`         │                  │                        │
# =>    │          │ (#4669)              │                  │                        │
# => ───┴──────────┴──────────────────────┴──────────────────┴────────────────────────┴──────────────────
```

Ahhh, that looks much better.

Hmmm, that date string is a string. If it were a date vs a string it could be used for sorting by date. The way we do that is we have to convert the datetime to a real datetime and update the column. Try this.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime}
# => ───┬──────────┬──────────────────────────┬──────────────────┬────────────────────────────┬──────────────
# =>  # │  commit  │         subject          │       name       │          email             │     date
# => ───┼──────────┼──────────────────────────┼──────────────────┼────────────────────────────┼──────────────
# =>  0 │ 42f1874a │ Update some examples and │ Justin Ma        │ hustcer@outlook.com        │ 7 hours ago
# =>    │          │ docs (#4682)             │                  │                            │
# =>  1 │ 2a89936b │ Move to latest stable    │ Sophia           │ 547158+sophiajt@users.nore │ 8 hours ago
# =>    │          │ crossterm, with fix      │                  │ ply.github.com             │
# =>    │          │ (#4684)                  │                  │                            │
# =>  2 │ ece5e7db │ dataframe list command   │ Fernando Herrera │ fernando.j.herrera@gmail   │ 8 hours ago
# =>    │          │ (#4681)                  │                  │ .com                       │
# =>  3 │ a6a96b29 │ Add binary literals      │ Sophia           │ 547158+sophiajt@users.nore │ 20 hours ago
# =>    │          │ (#4680)                  │                  │ ply.github.com             │
# =>  4 │ e3100e6a │ Fix alias in             │ Luca Trevisani   │ lucatrv@hotmail.com        │ a day ago
# =>    │          │ `docs/sample_config/conf │                  │                            │
# =>    │          │ ig.toml`                 │                  │                            │
# =>    │          │ (#4669)                  │                  │                            │
# => ───┴──────────┴──────────────────────────┴──────────────────┴────────────────────────────┴──────────────
```

Now this looks more nu-ish

If we want to revert back to a date string we can do something like this with the `nth` command and the `get` command.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | select 3 | get date | format date | get 0
# => Mon, 28 Feb 2022 18:31:53 -0500
```

Cool! Now that we have a real datetime we can do some interesting things with it like `group-by` or `sort-by` or `where`.
Let's try `sort-by` first

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | sort-by date
# => ────┬──────────┬──────────────────────────┬───────────────────┬───────────────────────────┬──────────────
# =>  #  │  commit  │         subject          │       name        │          email            │     date
# => ────┼──────────┼──────────────────────────┼───────────────────┼───────────────────────────┼──────────────
# =>   0 │ 0c3ea636 │ Add support for stderr   │ Sophia            │ 547158+sophiajt@users.nor │ 4 days ago
# =>     │          │ and exit code (#4647)    │                   │ eply.github.com           │
# =>   1 │ ed46f0ea │ fix: add missing         │ Jae-Heon Ji       │ 32578710+jaeheonji@user   │ 3 days ago
# =>     │          │ metadata for `ls_colors` │                   │ s.noreply.github.com      │

Title: Working with Datetimes in Nushell
Summary
This section demonstrates how to convert a string representation of a date into a datetime object in Nushell, enabling operations like sorting and grouping by date. It also shows how to revert back to a formatted date string and uses `sort-by` to sort the data by the datetime column.