-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add time module with basic functionality
- Loading branch information
1 parent
0a7b921
commit 8515c71
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
import datetime | ||
|
||
def get_current_time(): | ||
""" | ||
Returns the current time as a datetime object. | ||
""" | ||
return datetime.datetime.now() | ||
|
||
def format_time(time=None, format_string="%Y-%m-%d %H:%M:%S"): | ||
""" | ||
Formats a given time or the current time if none is provided. | ||
Args: | ||
time (datetime, optional): The time to format. Defaults to current time. | ||
format_string (str, optional): The format string. Defaults to "%Y-%m-%d %H:%M:%S". | ||
Returns: | ||
str: The formatted time string. | ||
""" | ||
if time is None: | ||
time = get_current_time() | ||
return time.strftime(format_string) | ||
|
||
def time_difference(time1, time2): | ||
""" | ||
Calculates the difference between two times. | ||
Args: | ||
time1 (datetime): The first time. | ||
time2 (datetime): The second time. | ||
Returns: | ||
timedelta: The time difference. | ||
""" | ||
return abs(time2 - time1) |