Skip to main content

Time package supporting chain call

Project description

py-itime is a time package supporting chain call.

install and use:

pip install py-itime

Example

from itime import iTime

# get datetime str of previous day with specified hours
>>> iTime(f'{iTime.now().delta(days=-1).date_str()} 10:00:00').datetime_str()
'2021-04-04 10:00:00'

# get unix timestamp of previous day
>>> iTime.now().delta(days=-1).uts(is_ms=True)
1627211818635

# get corresponding unix timestamp with specified datetime str
>>> iTime('2021-04-04 18:23:12').uts()
1617531792

# get datetime str of UTC time which is converted and down sampled by local time
>>> iTime.now().delta(hours=8).ds(minutes=5).datetime_str()
'2021-04-04 08:05:00'

Initialize

There are 5 ways to initialize an iTime object.

# 1. init with time str, format '%Y{}%m{}%d', '%Y{}%m{}%d %H{}%M{}%S' date_sep could be '','-','/', time_sep could be '',':'.
>>> iTime('20210404')
>>> iTime('2021-04-04 18:23:12')
>>> iTime('2021/04/04 18:23:12')

# 2. init with unix timestamp, support second and milliseconds, default as second
>>> iTime(1617531792)
>>> iTime(1617531792000, is_ms=True)    # if is milliseconds, is_ms=True
>>> iTime(1617531792.123)               # float is also supported

# 3. init with custom datetime str, use iTime.strp(time: str, fmt: str)
>>> iTime.strp('2021-04-04 18:23', fmt='%Y-%m-%d %H:%M')

# 4. init datetime.datetime object
>>> dt = datetime.datetime.now()
>>> iTime(dt)

# 5. init with timetuple
>>> iTime((2021, 4, 4, 18, 23, 12))
>>> iTime(['2021', '04', '04', '18', '23', '12'])

class iTime

  • iTime.now() -> iTime

    get current local time.

  • iTime.today() -> iTime

    get current date, hour minute second is 00:00:00.

  • iTime.strp(time: str, fmt: str) -> iTime

    init iTime from custom time str format.

  • iTime.uts(is_ms=False) -> int

    get unix timestamp, if is_ms=True, get milliseconds.

  • iTime.date_str(date_sep=’-’) -> str

    get date str, sep include ‘’, ‘-’, ‘/’.

  • time_str(time_sep=’:’) -> str

    get time str, ‘time_sep’ is sep include ‘’, ‘:’.

  • datetime_str(date_sep=’-’, time_sep=’:’) -> str

    get datetime str, date_sep and time_sep same to above.

  • join(datetime_str: str, fmt: str) -> iTime

    join iTime self with given time str. Notice: There is no date or time range checking, be careful

::
>>> iTime('2021-04-04 18:23:12').join('23:59:59').__str__()
'2021-04-04 23:59:59'
>>> iTime('2021-04-04 23:59:59').join('10 235959', fmt='%d %H%M%S').__str__()
'2021-04-10 23:59:59'
>>> iTime('2021-04-04 18:23:12').join('10', fmt='%d').__str__()
'2021-04-10 18:23:12'
  • strf(fmt) -> str

    get custom time str with given fmt.

  • pop() -> datetime.datetime

    get datetime.datetime object from iTime instance

  • delta(days=0, seconds=0, minutes=0, hours=0) -> iTime

    get offset time.

  • ds(hours=None, minutes=None, seconds=None) -> iTime

    down sample time, example as follows.

::
>>> iTime('2021-04-04 18:23:12').ds(hours=5).__str__()
'2021-04-04 15:23:12'
>>> iTime('2021-04-04 18:23:12').ds(minutes=5).__str__()
'2021-04-04 18:20:12'
>>> iTime('2021-04-04 18:23:12').ds(seconds=5).__str__()
'2021-04-04 18:23:10'
>>> iTime('2021-04-04 18:23:12').ds(minutes=5, seconds=0).__str__()
'2021-04-04 18:20:00'
>>> iTime('2021-04-04 18:23:12').ds(hours=0, minutes=0, seconds=0).__str__()
'2021-04-04 00:00:00'
>>> iTime('2021-04-04 18:23:12').ds(hours=17, minutes=5, seconds=5).__str__()
'2021-04-04 17:20:10'

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

py-itime-0.0.4.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

py_itime-0.0.4-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file py-itime-0.0.4.tar.gz.

File metadata

  • Download URL: py-itime-0.0.4.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for py-itime-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e245843d3b483fd21e44dfde0a94485b0f053c75bad88368a76a4e72345704a3
MD5 afa010b036cbbec5e40dbdd2a3ddf12e
BLAKE2b-256 c1c4417b9cfad9a43395794a4072392e85416d13b3bfcb7b9f76823c0feeece1

See more details on using hashes here.

File details

Details for the file py_itime-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: py_itime-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for py_itime-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 05d3b8aabbf30d24c1b78d686103a25e06d2aac8dc2e8d1e1ac56fac4136dc21
MD5 4cabbe7161e18041383a671d9bb6ed24
BLAKE2b-256 69578af903022b969bb6577a0632a9cc212ec09a087a64ab3fde51202d6dd1c8

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