-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTime utility class and enhance data generation capabilities
- Loading branch information
1 parent
ed69f3d
commit 5d206ef
Showing
3 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
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,49 @@ | ||
from datetime import datetime, timedelta | ||
import random | ||
|
||
class DateTime: | ||
""" | ||
DateTime class to generate datetime objects | ||
""" | ||
|
||
@staticmethod | ||
def get_date(between_yr:str="2020-2025", format:str="%d/%m/%Y", era:str=""): | ||
""" | ||
Get date based on the era | ||
""" | ||
if "now"==era: | ||
return datetime.now().strftime(format) | ||
try: | ||
_from = between_yr.split("-")[0] | ||
_to = between_yr.split("-")[1] | ||
_rand_yr = random.randint(int(_from), int(_to)) | ||
_date = (datetime.now() - timedelta(days=random.randint(1,10000))) | ||
_yr = _date.year | ||
return _date.strftime(format).replace(str(_yr), str(_rand_yr)) | ||
|
||
except Exception as e: | ||
return str(e) | ||
|
||
@staticmethod | ||
def get_time(era:str="now"): | ||
""" | ||
Get time based on the era | ||
""" | ||
if era=="past": | ||
return (datetime.now() - timedelta(hours=random.randint(1, 23))).time() | ||
elif era=="future": | ||
return (datetime.now() + timedelta(hours=random.randint(1, 23))).time() | ||
return datetime.now().time() | ||
|
||
@staticmethod | ||
def get_datetime(era:str="past", format:str="%d/%m/%Y %H:%M:%S"): | ||
""" | ||
Get datetime based on the era | ||
""" | ||
if era=="past": | ||
return (datetime.now() - timedelta(days=random.randint(1,1000))).strftime(format) | ||
elif era=="future": | ||
return (datetime.now() + timedelta(days=random.randint(1,1000))).strftime(format) | ||
return datetime.now().strftime(format) | ||
|
||
print(DateTime.get_datetime("")) |
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