Skip to content

time

get_timestamp(only_int=True)

Returns a timestamp as a string.

Parameters:

Name Type Description Default
only_int bool

if True, the timestamp is returned as an integer.

True

Returns:

Name Type Description
str str

the timestamp as a string.

Examples:

>>> from spotpython.utils.time import get_timestamp
>>> get_timestamp()
'2021-06-28 14:51:54.500000'
>>> get_timestamp(only_int=True)
'20210628145154500000'
Source code in spotpython/utils/time.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def get_timestamp(only_int=True) -> str:
    """
    Returns a timestamp as a string.

    Args:
        only_int (bool): if True, the timestamp is returned as an integer.

    Returns:
        str: the timestamp as a string.

    Examples:
        >>> from spotpython.utils.time import get_timestamp
        >>> get_timestamp()
        '2021-06-28 14:51:54.500000'
        >>> get_timestamp(only_int=True)
        '20210628145154500000'
    """
    dt = datetime.datetime.now().isoformat(sep=" ", timespec="microseconds")
    if only_int:
        # remove - . : and space
        dt = dt.replace("-", "")
        dt = dt.replace(".", "")
        dt = dt.replace(":", "")
        dt = dt.replace(" ", "")
    return dt