summaryrefslogtreecommitdiffstats
path: root/tests/test_time.py
blob: 62d436161a79a58f39d8cd52f1839dd2ad6cfca2 (plain)
1
2
3
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
29
30
31
32
33
34
import unittest
import sys

from sqlglot.time import format_time, subsecond_precision


class TestTime(unittest.TestCase):
    def test_format_time(self):
        self.assertEqual(format_time("", {}), None)
        self.assertEqual(format_time(" ", {}), " ")
        mapping = {"a": "b", "aa": "c"}
        self.assertEqual(format_time("a", mapping), "b")
        self.assertEqual(format_time("aa", mapping), "c")
        self.assertEqual(format_time("aaada", mapping), "cbdb")
        self.assertEqual(format_time("da", mapping), "db")

    def test_subsecond_precision(self):
        self.assertEqual(6, subsecond_precision("2023-01-01 12:13:14.123456+00:00"))
        self.assertEqual(3, subsecond_precision("2023-01-01 12:13:14.123+00:00"))
        self.assertEqual(0, subsecond_precision("2023-01-01 12:13:14+00:00"))
        self.assertEqual(0, subsecond_precision("2023-01-01 12:13:14"))
        self.assertEqual(0, subsecond_precision("garbage"))

    @unittest.skipUnless(
        sys.version_info >= (3, 11),
        "Python 3.11 relaxed datetime.fromisoformat() parsing with regards to microseconds",
    )
    def test_subsecond_precision_python311(self):
        # ref: https://docs.python.org/3/whatsnew/3.11.html#datetime
        self.assertEqual(6, subsecond_precision("2023-01-01 12:13:14.123456789+00:00"))
        self.assertEqual(6, subsecond_precision("2023-01-01 12:13:14.12345+00:00"))
        self.assertEqual(6, subsecond_precision("2023-01-01 12:13:14.1234+00:00"))
        self.assertEqual(3, subsecond_precision("2023-01-01 12:13:14.12+00:00"))
        self.assertEqual(3, subsecond_precision("2023-01-01 12:13:14.1+00:00"))