Skip to main content

Python package to work with NTP servers, time zones, and localized names of months and days of the week.

Project description

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

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
  1. 🏁 Getting Started
  2. ✨ Usage
  3. 🤝🏼 Contributing
  4. 📜️️ License
  5. 📬 Contact
  6. 🙏🏼 Acknowledgments

🏁 Getting Started

Prerequisites

  • Python 3

Installation

Using pip

pip install datetime_toolkit

(back to top)

✨ 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() Int 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.

(back to top)

🤝🏼 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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

📜️️ License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

📬 Contact

👨🏻‍💻 Sebastián Valdivia Loyola [ ✉️ E-Mail - 📂 GitHub - 📦 Project ]

(back to top)

🙏🏼 Acknowledgments

(back to top)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datetime_toolkit-1.0.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

datetime_toolkit-1.0.0-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page