Skip to main content

Time utilities in Python

Project description

Time Utilities

Time utilities in Python.

PyPI PyPI - Python Version PyPI - License PyPI - Downloads PyPI - Downloads Coverage Status GitHub code size in bytes GitHub last commit DeepSource

TimeUtil class

Methods

Method Description Parameters Returns
date_ms_to_date_str Converts a date in milliseconds to a date string. date_ms (int) The date in milliseconds to convert to a date string.
format_str (str, Default: YMD_HMSF_DATE_FORMAT) The format string to convert the date to.
str The date string.
date_ms_to_date_time Converts a date in milliseconds to a date time object. date_ms (int) The date in milliseconds to convert to a date time object. datetime The date time object.
date_str_to_date_time Converts a date string to a date time object. date_str (str) The date string to convert to a date time object.
format_str (str, Default: YMD_HMSF_DATE_FORMAT) The format string to convert the date to.
datetime The date time object.
date_time_to_date_ms Converts a date time object to a date in milliseconds. dt (datetime) The date time object to convert to a date in milliseconds. int The date in milliseconds.
get_current_date_ms Returns the current date in milliseconds. int The current date in milliseconds.
get_timezone_offset_hours Returns the timezone offset in hours. timezone_str (str) A string representing the timezone int The timezone offset in hours.
is_leap_year Checks if a year is a leap year. year (int) The year to check if it is a leap year. bool True if the year is a leap year, False otherwise.
is_timeout_ms Checks if a timeout in milliseconds has passed. start_time_ms (int) The start time in milliseconds.
timeout_ms (int) The timeout in milliseconds.
bool True if the timeout has passed, False otherwise.
is_date_in_format Checks if a date string is in a specific format. date_str (str) The date string to check.
format_str (str) The format string to check.
bool True if the date string is in the format, False otherwise.

Examples:

  • TimeUtil.date_ms_to_date_str

    Code

    from time_utils import TimeUtil
    
    # Convert a date in milliseconds to a date string
    date_str = TimeUtil.date_ms_to_date_str(1617223200000)
    
    print(date_str)
    

    Output

    2021-03-31 00:00:00.000
    
  • TimeUtil.date_ms_to_date_time

    Code

    from time_utils import TimeUtil
    
    # Convert a date in milliseconds to a date time object
    dt = TimeUtil.date_ms_to_date_time(1617223200000)
    
    print(dt)
    

    Output

    2021-03-31 00:00:00
    
  • TimeUtil.date_str_to_date_time

    Code

    from time_utils import TimeUtil
    
    # Convert a date string to a date time object
    dt = TimeUtil.date_str_to_date_time('2021-03-31 00:00:00.000')
    
    print(dt)
    

    Output

    2021-03-31 00:00:00
    
  • TimeUtil.date_time_to_date_ms

    Code

    from time_utils import TimeUtil
    from datetime import datetime
    
    # Convert a date time object to a date in milliseconds
    date_ms = TimeUtil.date_time_to_date_ms(datetime(2021, 3, 31, 0, 0, 0))
    
    print(date_ms)
    

    Output

    1617223200000
    
  • TimeUtil.get_current_date_ms

    Code

    from time_utils import TimeUtil
    
    # Get the current date in milliseconds
    current_date_ms = TimeUtil.get_current_date_ms()
    
    print(current_date_ms)
    

    Output

    1617223200000
    
  • TimeUtil.get_timezone_offset_hours

    Code

    from time_utils import TimeUtil
    
    # Get the timezone offset in hours
    timezone_offset = TimeUtil.get_timezone_offset_hours('PDT')
    
    print(timezone_offset)
    

    Output

    -7
    
  • TimeUtil.is_leap_year

    Code

    from time_utils import TimeUtil
    
    # Check if a year is a leap year
    is_leap_year = TimeUtil.is_leap_year(2020)
    
    print(is_leap_year)
    

    Output

    True
    
  • TimeUtil.is_timeout_ms

    Code

    from time_utils import TimeUtil
    from time import sleep
    
    current_date_ms = TimeUtil.get_current_date_ms()
    is_timeout = TimeUtil.is_timeout_ms(current_date_ms, 1000)
    print(is_timeout)
    sleep(2)
    is_timeout = TimeUtil.is_timeout_ms(current_date_ms, 1000)
    print(is_timeout)
    

    Output

    False
    True
    
  • TimeUtil.is_date_in_format

    Code

    from time_utils import TimeUtil
    from time_utils import YMD_HMSF_DATE_FORMAT
    
    # Check if a date string is in a specific format
    is_date_in_format = \
        TimeUtil.is_date_in_format('2021-03-31 00:00:00.000', YMD_HMSF_DATE_FORMAT)
    
    print(is_date_in_format)
    

    Output

    True
    

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

nrt_time_utils-1.0.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

nrt_time_utils-1.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file nrt_time_utils-1.0.0.tar.gz.

File metadata

  • Download URL: nrt_time_utils-1.0.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.0

File hashes

Hashes for nrt_time_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f9efa005c1e8df2ce6dc12a9ba34712db0b6d27dcae5bfe73390a183bb3ba89f
MD5 b51aa45bd47fcc4c9f0ab9f5174f059c
BLAKE2b-256 de326ce92023df524b28247ad05569600417ba5e192c7883f52cc852295b4017

See more details on using hashes here.

File details

Details for the file nrt_time_utils-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nrt_time_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 901ced6ed6afeecf7c57def3a0deeeee4ba4f8f0dd02cb5d04997cd4c9237f62
MD5 f9b273ff4582f68d23a4068152c681b9
BLAKE2b-256 964e76b69efe6b3ab298ae13a26c5c4d2fa79c8e8ad6d2eeac7f49a66886e20c

See more details on using hashes here.

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