Skip to content

Datetime

Module for datetime utility functions.

OqDateTime

This class provides methods to convert timestamps to strings and vice versa.

Source code in onequant/util/datetime.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class OqDateTime:
    """This class provides methods to convert timestamps to strings and vice versa."""

    @staticmethod
    def timestamp_to_string(timestamp):
        """Converts a timestamp to a string in the format 'YYYY-MM-DD HH:MM:SS'.

        Args:
            timestamp (int): The timestamp to convert.

        Returns:
            str: The formatted date and time string.
        """
        return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')

    @staticmethod
    def string_to_timestamp(date_string):
        """Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp.

        Args:
            date_string (str): The date and time string to convert.

        Returns:
            int: The timestamp.
        """
        if len(date_string) == 10:
            date_string += ' 00:00:00'
        elif len(date_string) == 8:
            date_string = date_string[:4] + '-' + date_string[4:6] + '-' + date_string[6:] + ' 00:00:00'
        return int(datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').timestamp())

    @staticmethod
    def string_to_ms_timestamp(date_string):
        """Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp in milliseconds.

        Args:
            date_string (str): The date and time string to convert.

        Returns:
            int: The timestamp in milliseconds.
        """
        if len(date_string) == 10:
            date_string += ' 00:00:00'
        elif len(date_string) == 8:
            date_string = date_string[:4] + '-' + date_string[4:6] + '-' + date_string[6:] + ' 00:00:00'
        return int(datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)

string_to_ms_timestamp(date_string) staticmethod

Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp in milliseconds.

Parameters:

Name Type Description Default
date_string str

The date and time string to convert.

required

Returns:

Name Type Description
int

The timestamp in milliseconds.

Source code in onequant/util/datetime.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@staticmethod
def string_to_ms_timestamp(date_string):
    """Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp in milliseconds.

    Args:
        date_string (str): The date and time string to convert.

    Returns:
        int: The timestamp in milliseconds.
    """
    if len(date_string) == 10:
        date_string += ' 00:00:00'
    elif len(date_string) == 8:
        date_string = date_string[:4] + '-' + date_string[4:6] + '-' + date_string[6:] + ' 00:00:00'
    return int(datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)

string_to_timestamp(date_string) staticmethod

Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp.

Parameters:

Name Type Description Default
date_string str

The date and time string to convert.

required

Returns:

Name Type Description
int

The timestamp.

Source code in onequant/util/datetime.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@staticmethod
def string_to_timestamp(date_string):
    """Converts a date and time string in the format 'YYYY-MM-DD HH:MM:SS' to a timestamp.

    Args:
        date_string (str): The date and time string to convert.

    Returns:
        int: The timestamp.
    """
    if len(date_string) == 10:
        date_string += ' 00:00:00'
    elif len(date_string) == 8:
        date_string = date_string[:4] + '-' + date_string[4:6] + '-' + date_string[6:] + ' 00:00:00'
    return int(datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').timestamp())

timestamp_to_string(timestamp) staticmethod

Converts a timestamp to a string in the format 'YYYY-MM-DD HH:MM:SS'.

Parameters:

Name Type Description Default
timestamp int

The timestamp to convert.

required

Returns:

Name Type Description
str

The formatted date and time string.

Source code in onequant/util/datetime.py
 9
10
11
12
13
14
15
16
17
18
19
@staticmethod
def timestamp_to_string(timestamp):
    """Converts a timestamp to a string in the format 'YYYY-MM-DD HH:MM:SS'.

    Args:
        timestamp (int): The timestamp to convert.

    Returns:
        str: The formatted date and time string.
    """
    return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')