hmscalc 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hmscalc-0.1.0/LICENSE +21 -0
- hmscalc-0.1.0/PKG-INFO +93 -0
- hmscalc-0.1.0/README.md +75 -0
- hmscalc-0.1.0/hmscalc/__init__.py +1 -0
- hmscalc-0.1.0/hmscalc/exceptions.py +23 -0
- hmscalc-0.1.0/hmscalc/hms_time.py +174 -0
- hmscalc-0.1.0/pyproject.toml +43 -0
hmscalc-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 M0209
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
hmscalc-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: hmscalc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library for performing arithmetic with HH:MM[:SS] formatted time strings.
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: M0209
|
|
7
|
+
Author-email: masanorimurakoshi0209@gmail.com
|
|
8
|
+
Requires-Python: >=3.9,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# hmscalc
|
|
19
|
+
|
|
20
|
+
A lightweight Python library for performing arithmetic on time values formatted as `HH:MM` or `HH:MM:SS`.
|
|
21
|
+
|
|
22
|
+
## ๐ Features
|
|
23
|
+
|
|
24
|
+
- Supports time addition and subtraction
|
|
25
|
+
- Accepts `HH:MM` and `HH:MM:SS` formatted strings
|
|
26
|
+
- Handles negative durations gracefully
|
|
27
|
+
- Converts time to seconds, minutes, hours, and dictionary/tuple formats
|
|
28
|
+
- Fully testable across multiple Python versions via Docker
|
|
29
|
+
|
|
30
|
+
## ๐ณ Quick Start (Docker-based)
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Build the image
|
|
34
|
+
docker build -t hmscalc .
|
|
35
|
+
|
|
36
|
+
# Run tests across multiple Python versions
|
|
37
|
+
docker run --rm hmscalc ./runtests.sh
|
|
38
|
+
|
|
39
|
+
# Run lint
|
|
40
|
+
docker run --rm hmscalc ./lint.sh
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## ๐ฆ Project Structure
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
hmscalc/
|
|
47
|
+
โโโ Dockerfile # Docker setup with pyenv and poetry
|
|
48
|
+
โโโ runtests.sh # Runs tests on multiple Python versions
|
|
49
|
+
โโโ hmscalc/ # Source code
|
|
50
|
+
โ โโโ hms_time.py
|
|
51
|
+
โโโ tests/ # Pytest-based unit tests
|
|
52
|
+
โโโ pyproject.toml # Poetry config
|
|
53
|
+
โโโ README.md # This file
|
|
54
|
+
โโโ LICENSE # MIT license
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## ๐ Usage (inside container)
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from hmscalc import HMSTime
|
|
61
|
+
|
|
62
|
+
a = HMSTime("1:30:15")
|
|
63
|
+
b = HMSTime("2:15:45")
|
|
64
|
+
|
|
65
|
+
print(a + b) # "3:46:00"
|
|
66
|
+
print(a - b) # "-0:45:30"
|
|
67
|
+
print(a.to_seconds()) # 5415
|
|
68
|
+
print(a.to_tuple()) # (1, 30, 15)
|
|
69
|
+
print(a.to_dict()) # {'hh': 1, 'mm': 30, 'ss': 15}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## ๐ Examples
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
HMSTime("2:30") + HMSTime("1:45") # "4:15:00"
|
|
76
|
+
HMSTime("1:00:30") - HMSTime("0:30") # "0:30:30"
|
|
77
|
+
HMSTime("0:00") - HMSTime("0:01") # "-0:01:00"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## ๐งช Running tests locally via Docker
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Build image
|
|
84
|
+
docker build -t hmscalc .
|
|
85
|
+
|
|
86
|
+
# Run matrix tests via pyenv + poetry
|
|
87
|
+
docker run --rm hmscalc ./runtests.sh
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## ๐ License
|
|
91
|
+
|
|
92
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
93
|
+
|
hmscalc-0.1.0/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# hmscalc
|
|
2
|
+
|
|
3
|
+
A lightweight Python library for performing arithmetic on time values formatted as `HH:MM` or `HH:MM:SS`.
|
|
4
|
+
|
|
5
|
+
## ๐ Features
|
|
6
|
+
|
|
7
|
+
- Supports time addition and subtraction
|
|
8
|
+
- Accepts `HH:MM` and `HH:MM:SS` formatted strings
|
|
9
|
+
- Handles negative durations gracefully
|
|
10
|
+
- Converts time to seconds, minutes, hours, and dictionary/tuple formats
|
|
11
|
+
- Fully testable across multiple Python versions via Docker
|
|
12
|
+
|
|
13
|
+
## ๐ณ Quick Start (Docker-based)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Build the image
|
|
17
|
+
docker build -t hmscalc .
|
|
18
|
+
|
|
19
|
+
# Run tests across multiple Python versions
|
|
20
|
+
docker run --rm hmscalc ./runtests.sh
|
|
21
|
+
|
|
22
|
+
# Run lint
|
|
23
|
+
docker run --rm hmscalc ./lint.sh
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## ๐ฆ Project Structure
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
hmscalc/
|
|
30
|
+
โโโ Dockerfile # Docker setup with pyenv and poetry
|
|
31
|
+
โโโ runtests.sh # Runs tests on multiple Python versions
|
|
32
|
+
โโโ hmscalc/ # Source code
|
|
33
|
+
โ โโโ hms_time.py
|
|
34
|
+
โโโ tests/ # Pytest-based unit tests
|
|
35
|
+
โโโ pyproject.toml # Poetry config
|
|
36
|
+
โโโ README.md # This file
|
|
37
|
+
โโโ LICENSE # MIT license
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## ๐ Usage (inside container)
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from hmscalc import HMSTime
|
|
44
|
+
|
|
45
|
+
a = HMSTime("1:30:15")
|
|
46
|
+
b = HMSTime("2:15:45")
|
|
47
|
+
|
|
48
|
+
print(a + b) # "3:46:00"
|
|
49
|
+
print(a - b) # "-0:45:30"
|
|
50
|
+
print(a.to_seconds()) # 5415
|
|
51
|
+
print(a.to_tuple()) # (1, 30, 15)
|
|
52
|
+
print(a.to_dict()) # {'hh': 1, 'mm': 30, 'ss': 15}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## ๐ Examples
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
HMSTime("2:30") + HMSTime("1:45") # "4:15:00"
|
|
59
|
+
HMSTime("1:00:30") - HMSTime("0:30") # "0:30:30"
|
|
60
|
+
HMSTime("0:00") - HMSTime("0:01") # "-0:01:00"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## ๐งช Running tests locally via Docker
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Build image
|
|
67
|
+
docker build -t hmscalc .
|
|
68
|
+
|
|
69
|
+
# Run matrix tests via pyenv + poetry
|
|
70
|
+
docker run --rm hmscalc ./runtests.sh
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## ๐ License
|
|
74
|
+
|
|
75
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""hmscalc: A package for HMS (hours, minutes, seconds) calculations."""
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Custom exception classes for HMS time calculations."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class HMSTimeError(Exception):
|
|
5
|
+
"""Base exception for HMS time errors."""
|
|
6
|
+
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class InvalidTimeFormatError(HMSTimeError):
|
|
11
|
+
"""Exception raised for invalid time format strings."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, time_str: str):
|
|
14
|
+
"""Initialize with the invalid time string."""
|
|
15
|
+
super().__init__(f"Invalid time format: '{time_str}'")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class NotTimeStringError(HMSTimeError):
|
|
19
|
+
"""Exception raised when a non-string is provided as a time input."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, value): # type: ignore[no-untyped-def]
|
|
22
|
+
"""Initialize with the invalid value type."""
|
|
23
|
+
super().__init__(f"Time input must be a string, got: {type(value).__name__}")
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""Module for handling time in hours, minutes, and seconds (HMS) format."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
from .exceptions import InvalidTimeFormatError, NotTimeStringError
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HMSTime:
|
|
9
|
+
"""Class to represent and manipulate time in hours, minutes, and seconds (HMS) format.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
----
|
|
13
|
+
time_str (str): Time string to parse.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, time_str: str):
|
|
18
|
+
"""Initialize HMSTime from a time string in 'HH:MM:SS' or 'HH:MM' format.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
----
|
|
22
|
+
time_str (str): Time string to parse.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
self.total_seconds = self._parse_time_string(time_str)
|
|
26
|
+
|
|
27
|
+
def __add__(self, other: "HMSTime") -> "HMSTime":
|
|
28
|
+
"""Add two HMSTime objects and return a new HMSTime object.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
----
|
|
32
|
+
other (HMSTime): The other HMSTime object to add.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
-------
|
|
36
|
+
HMSTime: The sum of the two times.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
return HMSTime.from_seconds(self.total_seconds + other.total_seconds)
|
|
40
|
+
|
|
41
|
+
def __sub__(self, other: "HMSTime") -> "HMSTime":
|
|
42
|
+
"""Subtract another HMSTime object from this one and return a new HMSTime object.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
----
|
|
46
|
+
other (HMSTime): The other HMSTime object to subtract.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
-------
|
|
50
|
+
HMSTime: The difference of the two times.
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
return HMSTime.from_seconds(self.total_seconds - other.total_seconds)
|
|
54
|
+
|
|
55
|
+
def __str__(self) -> str:
|
|
56
|
+
"""Return the string representation of the HMSTime object in 'HH:MM:SS' format.
|
|
57
|
+
|
|
58
|
+
Returns
|
|
59
|
+
-------
|
|
60
|
+
str: The string representation of the HMSTime object.
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
total = abs(self.total_seconds)
|
|
64
|
+
hh = total // 3600
|
|
65
|
+
mm = (total % 3600) // 60
|
|
66
|
+
ss = total % 60
|
|
67
|
+
sign = "-" if self.total_seconds < 0 else ""
|
|
68
|
+
return f"{sign}{hh}:{mm:02}:{ss:02}"
|
|
69
|
+
|
|
70
|
+
def __repr__(self) -> str:
|
|
71
|
+
"""Return the official string representation of the HMSTime object."""
|
|
72
|
+
return f"HMSTime('{self}')"
|
|
73
|
+
|
|
74
|
+
def __eq__(self, other: object) -> bool:
|
|
75
|
+
"""Check if two HMSTime objects are equal."""
|
|
76
|
+
if not isinstance(other, HMSTime):
|
|
77
|
+
return NotImplemented
|
|
78
|
+
return self.total_seconds == other.total_seconds
|
|
79
|
+
|
|
80
|
+
def __lt__(self, other: "HMSTime") -> bool:
|
|
81
|
+
"""Check if this HMSTime object is less than another."""
|
|
82
|
+
return self.total_seconds < other.total_seconds
|
|
83
|
+
|
|
84
|
+
def __le__(self, other: "HMSTime") -> bool:
|
|
85
|
+
"""Check if this HMSTime object is less than or equal to another."""
|
|
86
|
+
return self.total_seconds <= other.total_seconds
|
|
87
|
+
|
|
88
|
+
def __gt__(self, other: "HMSTime") -> bool:
|
|
89
|
+
"""Check if this HMSTime object is greater than another."""
|
|
90
|
+
return self.total_seconds > other.total_seconds
|
|
91
|
+
|
|
92
|
+
def __ge__(self, other: "HMSTime") -> bool:
|
|
93
|
+
"""Check if this HMSTime object is greater than or equal to another."""
|
|
94
|
+
return self.total_seconds >= other.total_seconds
|
|
95
|
+
|
|
96
|
+
def __ne__(self, other: object) -> bool:
|
|
97
|
+
"""Check if two HMSTime objects are not equal."""
|
|
98
|
+
if not isinstance(other, HMSTime):
|
|
99
|
+
return NotImplemented
|
|
100
|
+
return self.total_seconds != other.total_seconds
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def from_seconds(cls, total_seconds: int) -> "HMSTime":
|
|
104
|
+
"""Create an HMSTime object from a total number of seconds.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
----
|
|
108
|
+
total_seconds (int): The total number of seconds.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
-------
|
|
112
|
+
HMSTime: The corresponding HMSTime object.
|
|
113
|
+
|
|
114
|
+
"""
|
|
115
|
+
instance = cls.__new__(cls)
|
|
116
|
+
instance.total_seconds = total_seconds
|
|
117
|
+
return instance
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def _parse_time_string(time_str: str) -> int:
|
|
121
|
+
"""Parse a time string and return the total number of seconds.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
----
|
|
125
|
+
time_str (str): Time string to parse.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
-------
|
|
129
|
+
int: Total number of seconds represented by the string.
|
|
130
|
+
|
|
131
|
+
Raises:
|
|
132
|
+
------
|
|
133
|
+
NotTimeStringError: If input is not a string.
|
|
134
|
+
InvalidTimeFormatError: If the string format is invalid.
|
|
135
|
+
|
|
136
|
+
"""
|
|
137
|
+
if not isinstance(time_str, str):
|
|
138
|
+
raise NotTimeStringError(time_str)
|
|
139
|
+
|
|
140
|
+
match = re.fullmatch(r"(-)?(\d+):(\d{1,2})(?::(\d{1,2}))?", time_str)
|
|
141
|
+
if not match:
|
|
142
|
+
raise InvalidTimeFormatError(time_str)
|
|
143
|
+
neg, hh, mm, ss = match.groups()
|
|
144
|
+
hh = int(hh)
|
|
145
|
+
mm = int(mm)
|
|
146
|
+
ss = int(ss) if ss is not None else 0
|
|
147
|
+
|
|
148
|
+
total = hh * 3600 + mm * 60 + ss
|
|
149
|
+
return -total if neg else total
|
|
150
|
+
|
|
151
|
+
def to_seconds(self) -> int:
|
|
152
|
+
"""Return the total number of seconds represented by this HMSTime object."""
|
|
153
|
+
return self.total_seconds
|
|
154
|
+
|
|
155
|
+
def to_minutes(self) -> float:
|
|
156
|
+
"""Return the total number of minutes represented by this HMSTime object."""
|
|
157
|
+
return self.total_seconds / 60
|
|
158
|
+
|
|
159
|
+
def to_hours(self) -> float:
|
|
160
|
+
"""Return the total number of hours represented by this HMSTime object."""
|
|
161
|
+
return self.total_seconds / 3600
|
|
162
|
+
|
|
163
|
+
def to_tuple(self) -> tuple[int, int, int]:
|
|
164
|
+
"""Return the time as a tuple of (hours, minutes, seconds)."""
|
|
165
|
+
total = abs(self.total_seconds)
|
|
166
|
+
hh = total // 3600
|
|
167
|
+
mm = (total % 3600) // 60
|
|
168
|
+
ss = total % 60
|
|
169
|
+
return (hh, mm, ss)
|
|
170
|
+
|
|
171
|
+
def to_dict(self) -> dict[str, int]:
|
|
172
|
+
"""Return the time as a dictionary with keys 'hh', 'mm', and 'ss'."""
|
|
173
|
+
hh, mm, ss = self.to_tuple()
|
|
174
|
+
return {"hh": hh, "mm": mm, "ss": ss}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "hmscalc"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Python library for performing arithmetic with HH:MM[:SS] formatted time strings."
|
|
5
|
+
authors = ["M0209 <masanorimurakoshi0209@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
packages = [{ include = "hmscalc" }]
|
|
9
|
+
|
|
10
|
+
[tool.poetry.dependencies]
|
|
11
|
+
python = "^3.9"
|
|
12
|
+
|
|
13
|
+
[tool.poetry.group.dev.dependencies]
|
|
14
|
+
pytest = "^8.0.0"
|
|
15
|
+
black = "^24.0.0"
|
|
16
|
+
isort = "^5.12.0"
|
|
17
|
+
mypy = "^1.9.0"
|
|
18
|
+
ruff = "^0.4.0"
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["poetry-core"]
|
|
22
|
+
build-backend = "poetry.core.masonry.api"
|
|
23
|
+
|
|
24
|
+
[tool.ruff]
|
|
25
|
+
line-length = 100
|
|
26
|
+
target-version = "py311"
|
|
27
|
+
|
|
28
|
+
[tool.ruff.lint]
|
|
29
|
+
select = ["D211", "D212", "E", "F", "I", "N", "B", "D"]
|
|
30
|
+
ignore = ["D203", "D213"]
|
|
31
|
+
|
|
32
|
+
[tool.black]
|
|
33
|
+
line-length = 88
|
|
34
|
+
target-version = ['py311']
|
|
35
|
+
|
|
36
|
+
[tool.isort]
|
|
37
|
+
profile = "black"
|
|
38
|
+
|
|
39
|
+
[tool.mypy]
|
|
40
|
+
python_version = 3.11
|
|
41
|
+
strict = true
|
|
42
|
+
disallow_untyped_defs = true
|
|
43
|
+
ignore_missing_imports = true
|