-
Notifications
You must be signed in to change notification settings - Fork 4
Add --begin/--end and --timezone to start #36
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
Open
parthitce
wants to merge
1
commit into
kimai:master
Choose a base branch
from
linumiz:dev/add-begin-end-tz
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| use Swagger\Client\ApiException; | ||
| use Swagger\Client\Model\TimesheetEditForm; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
@@ -25,12 +26,15 @@ protected function configure(): void | |
| $this | ||
| ->setName('start') | ||
| ->setDescription('Starts a new timesheet') | ||
| ->setHelp('This command lets you start a new timesheet') | ||
| ->setHelp('This command lets you start a new timesheet and optionally end it immediately.') | ||
| ->addOption('customer', 'c', InputOption::VALUE_OPTIONAL, 'The customer to filter the project list, can be an ID or a search term or empty (you will be prompted for a customer).') | ||
| ->addOption('project', 'p', InputOption::VALUE_OPTIONAL, 'The project to use, can be an ID or a search term or empty. You will be prompted for the project.') | ||
| ->addOption('activity', 'a', InputOption::VALUE_OPTIONAL, 'The activity ID to use') | ||
| ->addOption('tags', 't', InputOption::VALUE_OPTIONAL, 'Comma separated list of tag names') | ||
| ->addOption('description', 'd', InputOption::VALUE_OPTIONAL, 'The timesheet description') | ||
| ->addOption('begin', 'b', InputOption::VALUE_OPTIONAL, 'The begin (date and) time in a format supported by PHP') | ||
| ->addOption('end', 'e', InputOption::VALUE_OPTIONAL, 'The end (date and) time in a format supported by PHP') | ||
| ->addOption('timezone', 'z', InputOption::VALUE_OPTIONAL, 'IANA timezone (e.g. Asia/Kolkata) for interpreting --begin/--end. Falls back to the PHP default timezone if omitted.') | ||
| ; | ||
| } | ||
|
|
||
|
|
@@ -81,6 +85,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| $form->setDescription($description); | ||
| } | ||
|
|
||
| $timezone = $input->getOption('timezone'); | ||
|
|
||
| try { | ||
| if (null !== ($begin = $input->getOption('begin'))) { | ||
| $begin = $this->parseAndRefineDateTime((string) $begin, 'begin', $timezone); | ||
| $form->setBegin($begin); | ||
| } | ||
|
|
||
| if (null !== ($end = $input->getOption('end'))) { | ||
| $end = $this->parseAndRefineDateTime((string) $end, 'end', $timezone); | ||
| $form->setEnd($end); | ||
| } | ||
|
Comment on lines
+96
to
+99
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two issues:
|
||
| } catch (\InvalidArgumentException $ex) { | ||
| $io->error($ex->getMessage()); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| try { | ||
| $timesheet = $api->postPostTimesheet($form); | ||
| } catch (ApiException $ex) { | ||
|
|
@@ -92,6 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| $fields = [ | ||
| 'ID' => $timesheet->getId(), | ||
| 'Begin' => $timesheet->getBegin()->format(\DateTime::ISO8601), | ||
| 'End' => $timesheet->getEnd()?->format(\DateTime::ISO8601), | ||
| 'Description' => $timesheet->getDescription(), | ||
| 'Tags' => implode(PHP_EOL, $timesheet->getTags()), | ||
| 'Customer' => $customer->getName(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -321,4 +321,26 @@ private function askForActivity(SymfonyStyle $io, array $activities): ?Activity | |
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function parseAndRefineDateTime(string $value, string $valueName, ?string $timezone = null): \DateTime | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| $zone = $this->resolveTimezone($timezone); | ||
|
|
||
| try { | ||
| return new \DateTime($value, $zone); | ||
| } catch (\Exception $e) { | ||
| throw new \InvalidArgumentException(\sprintf('Value "%s" for field "%s" is not a valid DateTime.', $value, $valueName)); | ||
| } | ||
| } | ||
|
|
||
| private function resolveTimezone(?string $timezone): \DateTimeZone | ||
| { | ||
| $name = $timezone ?? date_default_timezone_get(); | ||
|
|
||
| try { | ||
| return new \DateTimeZone($name); | ||
| } catch (\Exception $e) { | ||
| throw new \InvalidArgumentException(\sprintf('Invalid timezone "%s".', $name)); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timezone: is only validated if either begin or end are passed. always execute the resolveTimezone() - otherwise the user won't recognize some broken value (e.g.
--timezone=foowould be silently ignored currently).Add a try & catch and show an error + Command::FAILURE if parsing fails