Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seq date: generalize to allow any duration for --increment argument #14903

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

pyz4
Copy link
Contributor

@pyz4 pyz4 commented Jan 24, 2025

Description

This PR seeks to generalize the seq date command so that it can receive any duration as an --increment. Whereas the current command can only output a list of dates spaced at least 1 day apart, the new command can output a list of datetimes that are spaced apart by any duration.

For example:

> seq date --begin-date 2025-01-01 --end-date 2025-01-02 --increment 6hr --output-format "%Y-%m-%d %H:%M:%S"
╭───┬─────────────────────╮
│ 0 │ 2025-01-01 00:00:00 │
│ 1 │ 2025-01-01 06:00:00 │
│ 2 │ 2025-01-01 12:00:00 │
│ 3 │ 2025-01-01 18:00:00 │
│ 4 │ 2025-01-02 00:00:00 │
╰───┴─────────────────────╯

Note that the default behavior remains unchanged:

> seq date --begin-date 2025-01-01 --end-date 2025-01-02
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-02 │
╰───┴────────────╯

The default output format also remains unchanged:

> seq date --begin-date 2025-01-01 --end-date 2025-01-02 --increment 6hr
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-01 │
│ 2 │ 2025-01-01 │
│ 3 │ 2025-01-01 │
│ 4 │ 2025-01-02 │
╰───┴────────────╯

User-Facing Changes

Breaking Changes

  • The --increment argument no longer accepts just an integer and requires a duration
# NEW BEHAVIOR
> seq date --begin-date 2025-01-01 --end-date 2025-01-02 --increment 1

Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #13:1:68]
 1 │ seq date --begin-date 2025-01-01 --end-date 2025-01-02 --increment 1
   ·                                                                    ┬
   ·                                                                    ╰── expected duration with valid units
   ╰────

EDIT: Break Change is mitigated. --increment accepts either an integer or duration.

Bug Fix

  • The --days argument had an off-by-one error and would print 1 too many elements in the output. For example,
# OLD BEHAVIOR
> seq date -b 2025-01-01 --days 5 --increment 1
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-02 │
│ 2 │ 2025-01-03 │
│ 3 │ 2025-01-04 │
│ 4 │ 2025-01-05 │
│ 5 │ 2025-01-06 │ <-- Extra element
╰───┴────────────╯

# NEW BEHAVIOR
> seq date -b 2025-01-01 --days 5 --increment 1day
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-02 │
│ 2 │ 2025-01-03 │
│ 3 │ 2025-01-04 │
│ 4 │ 2025-01-05 │
╰───┴────────────╯

New Argument

  • A --periods argument is introduced to indicate the number of output elements, regardless of the --increment value. Importantly, the --days argument is ignored when --periods is set.
# NEW BEHAVIOR
> seq date -b 2025-01-01 --days 5 --periods 10 --increment 1day
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-02 │
│ 2 │ 2025-01-03 │
│ 3 │ 2025-01-04 │
│ 4 │ 2025-01-05 │
│ 5 │ 2025-01-06 │
│ 6 │ 2025-01-07 │
│ 7 │ 2025-01-08 │
│ 8 │ 2025-01-09 │
│ 9 │ 2025-01-10 │
╰───┴────────────╯

Note that the --days and --periods arguments differ in their functions. The --periods value determines the number of elements in the output that are always spaced --increment apart. The --days value determines the bookends --begin-date and --end-date when only one is set, though the number of elements may differ based on the --increment value.

# NEW BEHAVIOR
> seq date -e 2025-01-01 --days 2 --increment 5hr --output-format "%Y-%m-%d %H:%M:%S"

╭───┬─────────────────────╮
│ 0 │ 2025-01-23 22:25:05 │
│ 1 │ 2025-01-24 03:25:05 │
│ 2 │ 2025-01-24 08:25:05 │
│ 3 │ 2025-01-24 13:25:05 │
│ 4 │ 2025-01-24 18:25:05 │
╰───┴─────────────────────╯

Tests + Formatting

I added several examples for each user-facing change in generators/seq_date.rs and some tests in tests/commands/seq_date.rs.

After Submitting

@pyz4 pyz4 changed the title seq date: generalize to allow any duration for --increment flag seq date: generalize to allow any duration for --increment arg Jan 24, 2025
@pyz4 pyz4 changed the title seq date: generalize to allow any duration for --increment arg seq date: generalize to allow any duration for --increment argument Jan 24, 2025
@fdncred
Copy link
Collaborator

fdncred commented Jan 24, 2025

I haven't looked at the code yet but I'm wondering if --days and --period should be mutually exclusive instead of --days just being silently ignored, as you describe above. I'm not a huge fan of ignoring flags and not telling people what's going on. It tends to lead to problems.

This is a very cool addition. I really like it alot. Thanks for working on it!

@pyz4
Copy link
Contributor Author

pyz4 commented Jan 24, 2025

Maybe the best solution is just to remove --days. Its original purpose was to set the number of elements in the output when only one of --begin-date or --end-date was set, which is what --periods tries to do but with an argument name that applies more generally.

The --days argument does behave slightly differently from --periods when --increment is not 1day. For example,

# OLD BEHAVIOR (using --days)
> seq date -b 2025-01-01 --days 5 --increment 2
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-03 │
│ 2 │ 2025-01-05 │
╰───┴────────────╯

# NEW BEHAVIOR (using --periods)
> seq date -b 2025-01-01 --periods 5 --increment 2day
╭───┬────────────╮
│ 0 │ 2025-01-01 │
│ 1 │ 2025-01-03 │
│ 2 │ 2025-01-05 │
│ 3 │ 2025-01-07 │
│ 4 │ 2025-01-09 │
╰───┴────────────╯

I suspect --days isn't being used much in the wild anyway, given it has an off-by-one error.

@fdncred fdncred added pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes pr:bugfix This PR fixes some bug pr:commands This PR changes our commands in some way labels Jan 24, 2025
@fdncred
Copy link
Collaborator

fdncred commented Jan 24, 2025

I guess I'm ok with removing --days. I really don't like breaking changes but --periods seems a better fit with these changes.

@pyz4
Copy link
Contributor Author

pyz4 commented Jan 24, 2025

Another alternative is to move this logic into a separate command, seq datetime? This may introduce some command-bloat and is not my preference, but it will mitigate breaking changes. Just a thought.

@fdncred
Copy link
Collaborator

fdncred commented Jan 24, 2025

I'd rather just have it all in seq date because it's easier to type. Just my opinion.

@132ikl
Copy link
Contributor

132ikl commented Jan 24, 2025

I honestly think we should be going in the direction of using Value::Date here, I don't think there's any advantage to using strings. If you want a specific output format, you can always tack on a format date to format the dates however you'd like. I don't love that we're duplicating datetime parsing logic here. Especially if we're making breaking changes, I think it would make sense to switch over to using actual dates instead of strings. I don't mean to force this all onto you in this PR, but I do think if we're making breaking changes we should rip off the band-aid here and super-break it once instead of breaking it twice.

@pyz4
Copy link
Contributor Author

pyz4 commented Jan 24, 2025

Agreed. I wanted to switch to SyntaxType::DateTime types for begin and end dates, and DateTime for the outputs, but also wanted to minimize the scope of breaking changes in this PR. Happy to implement it if there's more buy-in. @fdncred, any thoughts here?

@fdncred
Copy link
Collaborator

fdncred commented Jan 24, 2025

I think we save the datetime changes for another PR. I'm not necessarily against it, although it makes getting an answer more difficult because you have to tack on a format command.

@132ikl
Copy link
Contributor

132ikl commented Jan 24, 2025

maybe then we avoid breaking anything with this PR, merge it now/before release, and then do all of the breaking in another PR?

@pyz4
Copy link
Contributor Author

pyz4 commented Jan 24, 2025

I think the only way to have no breaking changes is to overload the --increment argument to accept both an integer and duration. Definitely doable -- whether that's preferred, I'm not sure.

@fdncred
Copy link
Collaborator

fdncred commented Jan 24, 2025

I'm fine with @132ikl's suggestion of having this be a non-breaking change for now.

@132ikl
Copy link
Contributor

132ikl commented Jan 24, 2025

I think the only way to have no breaking changes is to overload the --increment argument to accept both an integer and duration. Definitely doable -- whether that's preferred, I'm not sure.

maybe not ideal, but I think this is a reasonable tradeoff, as long as we do follow up at some point

@pyz4 pyz4 force-pushed the generalize-seq-date branch 3 times, most recently from ffb2fb9 to be6718a Compare January 24, 2025 20:34
@pyz4
Copy link
Contributor Author

pyz4 commented Jan 24, 2025

See updated PR: breaking change has been mitigated, and --increment now accepts either integer or duration. All default values are the same, and all previous commands run as expected.

@pyz4 pyz4 force-pushed the generalize-seq-date branch from be6718a to 0eed82c Compare January 24, 2025 20:47
@fdncred fdncred removed the pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes label Jan 24, 2025
@pyz4 pyz4 force-pushed the generalize-seq-date branch from 0eed82c to 409eeb0 Compare January 25, 2025 14:42
@pyz4 pyz4 force-pushed the generalize-seq-date branch from 409eeb0 to 3c7c7c5 Compare January 25, 2025 16:21
@fdncred fdncred merged commit 926b040 into nushell:main Jan 25, 2025
15 checks passed
@fdncred
Copy link
Collaborator

fdncred commented Jan 25, 2025

Thanks

@github-actions github-actions bot added this to the v0.102.0 milestone Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:bugfix This PR fixes some bug pr:commands This PR changes our commands in some way
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants