Python package to work with NTP servers, time zones, and localized names of months and days of the week.
Project description
Datetime Toolkit
Python package to work with NTP servers, time zones, and localized names of months and days of the week.
Report Bug
·
Request Feature
Table of Contents
🏁 Getting Started
Prerequisites
- Python 3
Installation
Using pip
pip install datetime_toolkit
✨ Usage
Default instantation
Default parameters are from 🇨🇱 Chile:
- NTP Server: ntp.shoa.cl
- Time zone: America / Santiago
- Locale: Your system default locale
To use custom locale, the language must be installed on your system. On Linux, check your /etc/locale.gen
file.
For example, I have English, Spanish and German installed.
grep -v "#" /etc/locale.gen
en_US.UTF-8 UTF-8
es_CL.UTF-8 UTF-8
de_DE.UTF-8 UTF-8
datetime_toolkit = DatetimeToolkit(locale = 'es_CL.UTF-8')
print(datetime_toolkit.months)
print(datetime_toolkit.days)
# Printed values
['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
['Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado', 'Domingo']
Custom instantation
To use custom parameters, use the country ISO 3166 alpha-2 code to find your country information.
For example, the country code for 🇩🇪 Germany is "DE", so you can use:
info = DatetimeToolkit.country_information(alpha_2_code = 'de')
# Static method country_information(code) will return a dictionary like this
info = {'country': 'Germany', 'time_zones': ['Europe/Berlin', 'Europe/Busingen']}
I will use PTB NTP server (ptbtime1.ptb.de
) and the timezone from Berlin.
datetime_toolkit = DatetimeToolkit(ntp = 'ptbtime1.ptb.de', tz = 'Europe/Berlin', locale = 'de_DE.UTF-8')
print(datetime_toolkit.months)
print(datetime_toolkit.days)
# Printed values
['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']
['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']
Class DatetimeToolkit properties and methods
Property | Returns |
---|---|
days | List with the names of the days of the week as strings. Language used is determined by locale parameter or system default. First letter is uppercase and the rest lowercase. |
months | List with the names of the months as strings. Language used is determined by locale parameter or system default. First letter is uppercase and the rest lowercase. |
Method | Returns |
---|---|
@staticmethod country_information(alpha_2_code: str) |
Dictionary with country information. Keys are the strings "country" and "time_zones". |
locale() | Locale used as string. None if no locale was provided. |
server_url() | NTP server URL/IP as string. |
tz() | Object datetime.tzinfo for the given time zone. |
local_time() | Float timestamp of the current local time. Wrapper of time.time() |
server_time() | Integer timestamp of the server time. |
datetime(is_request_forced = False, is_utc = False) | Datetime object using given time zone. |
string(is_request_forced = False, is_utc = False, string_format = '%Y-%m-%d %H:%M:%S') | String representing datetime using string_format. |
float_to_datetime(timestamp: float, is_utc = False) | Datetime from timestamp using given time zone. |
float_to_string(timestamp: float, is_utc = False, string_format = '%Y-%m-%d %H:%M:%S') | String from timestamp using given time zone and string_format. |
name_month(custom_datetime: datetime = None, is_request_forced = False, is_utc = False) | String of the localized month name. If custom_datetime is None, it will use datetime() method. |
name_day(custom_datetime: datetime = None, is_request_forced = False, is_utc = False) | String of the localized day name. If custom_datetime is None, it will use datetime() method. |
🤝🏼 Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
📜️️ License
Distributed under the MIT License. See LICENSE.txt
for more information.
📬 Contact
👨🏻💻 Sebastián Valdivia Loyola [ ✉️ E-Mail - 📂 GitHub - 📦 Project ]
🙏🏼 Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for datetime_toolkit-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95d97905ed075aebf7222a0958a9aaeb8b905cafb9a6849f3b077f2b43a4d15b |
|
MD5 | a3884b9d9ec9f424412852434dff2101 |
|
BLAKE2b-256 | 7c6bfaf3f50c37bc66081444a3adc2c34415e270e5f6b06b627dc4ffb122bb4d |