-
Notifications
You must be signed in to change notification settings - Fork 2
Add datetime and azure utilities #78
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
243698a
Add timezone utilities
pateash 87263b7
Update CONTRIBUTING.md with Python setup details and refine Makefile'…
pateash e69097e
Merge branch 'main' into codex/close-github-issue-#68
pateash f534a78
Refactor CONTRIBUTING guide, split publishing and docs into separate …
pateash f79cc92
Add zoneinfo backport dependency
pateash d303771
Merge branch 'codex/close-github-issue-#68' of https://github.com/hck…
pateash 38b461c
Update CONTRIBUTING.md with Python setup details and refine Makefile'…
pateash 0c22dc9
Add timezone helpers and improve dt command
pateash 4e7ce5c
Update CONTRIBUTING.md with Python setup details and refine Makefile'…
pateash fcb8862
Azure: added
pateash abfa4c9
Refactor datetime format and add comprehensive documentation
pateash ab27231
Azure: added
pateash eb09a5f
Azure: added
pateash eb1fc4c
Azure: added
pateash 59f06a2
Azure: added
pateash 992761c
Azure: added
pateash 3066d1c
Azure: added
pateash 1a83ed6
Azure: added
pateash 6e122ba
Azure: added
pateash e905462
Azure: added
pateash b7e1d11
Azure: added
pateash 933bf75
Azure: added
pateash 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
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .. hckr documentation master file, created by | ||
| sphinx-quickstart on Wed Jun 12 20:06:39 2024. | ||
| You can adapt this file completely to your liking, but it should at least | ||
| contain the root `toctree` directive. | ||
|
|
||
| .. click:: hckr.cli.dt:dt | ||
| :prog: hckr dt | ||
| :nested: full | ||
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .. hckr documentation master file, created by | ||
| sphinx-quickstart on Wed Jun 12 20:06:39 2024. | ||
| You can adapt this file completely to your liking, but it should at least | ||
| contain the root `toctree` directive. | ||
|
|
||
|
|
||
| Datetime Commands | ||
| ================= | ||
| ``hckr`` provides utilities to work with different timezones. | ||
|
|
||
| .. tip:: | ||
| More commands will be added in future updates. Stay tuned! | ||
|
|
||
| commands | ||
| --------------- | ||
| .. toctree:: | ||
| dt |
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 |
|---|---|---|
|
|
@@ -77,3 +77,11 @@ hckr | |
| :hidden: | ||
|
|
||
| info/index | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 2 | ||
| :caption: Datetime | ||
| :hidden: | ||
|
|
||
| dt/index | ||
|
|
||
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
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import click | ||
|
|
||
| from hckr.utils.MessageUtils import success, error | ||
| from hckr.utils.TimeUtils import current_time, convert | ||
|
|
||
|
|
||
| @click.group( | ||
| help="datetime utilities", | ||
| context_settings={"help_option_names": ["-h", "--help"]}, | ||
| ) | ||
| def dt(): | ||
| pass | ||
|
|
||
|
|
||
| @dt.command(help="show current time in the given timezone") | ||
| @click.option( | ||
| "-t", | ||
| "--timezone", | ||
| default="UTC", | ||
| show_default=True, | ||
| help="Timezone in which to display the time", | ||
| ) | ||
| def now(timezone): | ||
| try: | ||
| dt_now = current_time(timezone) | ||
| success(f"Current time in {timezone}: {dt_now.strftime('%Y-%m-%d %H:%M:%S')}") | ||
| except Exception: | ||
| error("Failed to get current time") | ||
|
|
||
|
|
||
| @dt.command(help="convert datetime between timezones") | ||
| @click.option("--datetime", "dt_str", required=True, help="Datetime in ISO format") | ||
| @click.option("--from-tz", required=True, help="Source timezone") | ||
| @click.option("--to-tz", required=True, help="Destination timezone") | ||
| def convert_time(dt_str, from_tz, to_tz): | ||
| try: | ||
| result = convert(dt_str, from_tz, to_tz) | ||
| success( | ||
| f"{dt_str} [{from_tz}] -> {result.strftime('%Y-%m-%d %H:%M:%S')} [{to_tz}]" | ||
| ) | ||
| except Exception: | ||
| error("Failed to convert time") |
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from datetime import datetime | ||
| from zoneinfo import ZoneInfo | ||
|
|
||
| from .MessageUtils import error, success | ||
|
|
||
|
|
||
| def current_time(timezone: str = "UTC") -> datetime: | ||
| """Return current time in the given timezone.""" | ||
| try: | ||
| tz = ZoneInfo(timezone) | ||
| except Exception as e: # pragma: no cover - error should be very rare | ||
| error(f"Invalid timezone: {timezone}\n{e}") | ||
| raise | ||
| return datetime.now(tz) | ||
|
|
||
|
|
||
| def convert(dt_str: str, from_tz: str, to_tz: str) -> datetime: | ||
| """Convert ``dt_str`` from ``from_tz`` timezone to ``to_tz`` timezone.""" | ||
| try: | ||
| from_zone = ZoneInfo(from_tz) | ||
| to_zone = ZoneInfo(to_tz) | ||
| except Exception as e: # pragma: no cover - user input error | ||
| error(f"Invalid timezone provided\n{e}") | ||
| raise | ||
| dt = datetime.fromisoformat(dt_str) | ||
| if dt.tzinfo is None: | ||
| dt = dt.replace(tzinfo=from_zone) | ||
| else: | ||
| dt = dt.astimezone(from_zone) | ||
| return dt.astimezone(to_zone) |
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
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from click.testing import CliRunner | ||
|
|
||
| from hckr.cli.dt import now, convert_time | ||
|
|
||
|
|
||
| def test_dt_now(): | ||
| runner = CliRunner() | ||
| result = runner.invoke(now, ["--timezone", "UTC"]) | ||
| assert result.exit_code == 0 | ||
| assert "Current time in UTC" in result.output | ||
|
|
||
|
|
||
| def test_dt_convert(): | ||
| runner = CliRunner() | ||
| result = runner.invoke( | ||
| convert_time, | ||
| ["--datetime", "2024-01-01 10:00:00", "--from-tz", "UTC", "--to-tz", "US/Pacific"], | ||
| ) | ||
| assert result.exit_code == 0 | ||
| assert "US/Pacific" in result.output |
Oops, something went wrong.
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.
🛠️ Refactor suggestion
Add a title for the
hckr dtcommand.Include a top-level heading to introduce this command (e.g.,
hckr dtunderlined with=) above the click directive for better readability.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents