k3time

Documentation for the Code

Time convertion utils.

>>> parse('2017-01-24T07:51:59.000Z', 'iso')
datetime.datetime(2017, 1, 24, 7, 51, 59)
>>> format_ts(1485216000, 'iso')
'2017-01-24T00:00:00.000Z'
>>> format_ts(1485216000, '%Y-%m-%d')
'2017-01-24'

Functions

k3time.parse_to_ts(time_str, fmt_key)

Similar to parse but returns a timestamp in second instead of a datetime instance.

Returns

timestamp.

Return type

int

k3time.parse(time_str, fmt_key, timezone=None)

parse time string to datetime instance.

Parameters
  • time_str (str) – time in string. Please refer to timeutil.formats, for example: ‘Tue, 24 Jan 2017 07:51:59 UTC’, ‘2017-01-24T07:51:59.000Z’.

  • fmt_key (str) –

    specifies time string format. It can be a named format alias, or format string:

    • ’default’: ‘%a, %d %b %Y %H:%M:%S UTC’,

    • ’iso’: ‘%Y-%m-%dT%H:%M:%S.000Z’,

    • ’utc’: ‘%a, %d %b %Y %H:%M:%S UTC’,

    • ’archive’: ‘%Y%m%d-%H’,

    • ’compact’: ‘%Y%m%d-%H%M%S’,

    • ’daily’: ‘%Y-%m-%d’,

    • ’mysql’: ‘%Y-%m-%d %H:%M:%S’,

    • ’nginxaccesslog’: “%d/%b/%Y:%H:%M:%S”,

    • ’nginxerrorlog’: “%Y/%m/%d %H:%M:%S”,

    Thus parse(tm, “default”) is same as parse(tm, “%a, %d %b %Y %H:%M:%S UTC”).

  • timezone – specifies a timezone to get an aware datetime object. It is a string, such as ‘Asia/Shanghai’.

Returns

datetime.

k3time.format(dt, fmt_key)

convert datetime instance to specified format time string

Parameters
  • dt (datetime) – datetime instance

  • fmt_key (str) – specifies time string format. It can be a named format alias, or format string.

Returns

time string in specified format.

Return type

str

k3time.format_ts(ts, fmt_key, utc=True)

convert timestamp to specified format time string

Parameters
  • ts (int) – timestamp in second

  • fmt_key (str) – specifies time string format. It can be a named format alias, or format string.

  • utc (bool) – set to True to get utc time, set to False to get local time.

Returns

formatted time string.

Return type

str

k3time.utc_datetime_to_ts(dt)

convert datetime instance to timestamp in second

Parameters

dt (datetime) – datetime instance

Returns

timestamp in second.

Return type

int

k3time.datetime_to_ts(dt)

convert naive or aware datetime instance to timestamp in second

Parameters

dt (datetime) – datetime instance

Returns

timestamp in second.

Return type

int

k3time.ts_to_datetime(ts, utc=True)

convert timestamp in second to datetime instance

Parameters

ts (int) – timestamp in second

Returns

datetime instance

Return type

datetime

k3time.ts()

get current timestamp in second

Returns

timestamp in second

Return type

int

k3time.ms()

get current timestamp in millisecond

Returns

timestamp in millisecond

Return type

int

k3time.us()

get current timestamp in microsecond

Returns

timestamp in microsecond

Return type

int

k3time.ns()

get current timestamp in nanosecond

Returns

timestamp in nanosecond

Return type

int

k3time.to_sec(v)

Convert millisecond, microsecond or nanosecond to second.

Parameters

v – timestamp in int, long, float or string. It can be a timestamp in second, millisecond(10e-3), microsecond(10e-6) or nanosecond(10e-9).

Returns

timestamp in second.

Return type

int

Raises

ValueError – If v is not a valid timestamp.

k3time.is_timestamp(ts, unit=None)

It check if ts is a valid timestamp, in string or number.

Parameters
  • ts – is timestamp in string or int.

  • unit

    specifies what the unit ts is in:

    • s: second

    • ms: millisecond 10^-3

    • us: microsecond 10^-6

    • ns: nanosecond 10^-9

    • None: choose automatically.

Returns

bool

Indices and tables