sqlglot.time API documentation

Edit on GitHub

# sqlglot.time

View Source

     1import typing as t
     2
     3# The generic time format is based on python time.strftime.
     4# https://docs.python.org/3/library/time.html#time.strftime
     5from sqlglot.trie import TrieResult, in_trie, new_trie
     6
     7
     8def format_time(
     9    string: str, mapping: t.Dict[str, str], trie: t.Optional[t.Dict] = None
    10) -> t.Optional[str]:
    11    """
    12    Converts a time string given a mapping.
    13
    14    Examples:
    15        >>> format_time("%Y", {"%Y": "YYYY"})
    16        'YYYY'
    17
    18        Args:
    19            mapping: dictionary of time format to target time format.
    20            trie: optional trie, can be passed in for performance.
    21
    22        Returns:
    23            The converted time string.
    24    """
    25    if not string:
    26        return None
    27
    28    start = 0
    29    end = 1
    30    size = len(string)
    31    trie = trie or new_trie(mapping)
    32    current = trie
    33    chunks = []
    34    sym = None
    35
    36    while end <= size:
    37        chars = string[start:end]
    38        result, current = in_trie(current, chars[-1])
    39
    40        if result == TrieResult.FAILED:
    41            if sym:
    42                end -= 1
    43                chars = sym
    44                sym = None
    45            start += len(chars)
    46            chunks.append(chars)
    47            current = trie
    48        elif result == TrieResult.EXISTS:
    49            sym = chars
    50
    51        end += 1
    52
    53        if result != TrieResult.FAILED and end > size:
    54            chunks.append(chars)
    55
    56    return "".join(mapping.get(chars, chars) for chars in chunks)

def format_time( string: str, mapping: Dict[str, str], trie: Optional[Dict] = None) -> Optional[str]: View Source

     9def format_time(
    10    string: str, mapping: t.Dict[str, str], trie: t.Optional[t.Dict] = None
    11) -> t.Optional[str]:
    12    """
    13    Converts a time string given a mapping.
    14
    15    Examples:
    16        >>> format_time("%Y", {"%Y": "YYYY"})
    17        'YYYY'
    18
    19        Args:
    20            mapping: dictionary of time format to target time format.
    21            trie: optional trie, can be passed in for performance.
    22
    23        Returns:
    24            The converted time string.
    25    """
    26    if not string:
    27        return None
    28
    29    start = 0
    30    end = 1
    31    size = len(string)
    32    trie = trie or new_trie(mapping)
    33    current = trie
    34    chunks = []
    35    sym = None
    36
    37    while end <= size:
    38        chars = string[start:end]
    39        result, current = in_trie(current, chars[-1])
    40
    41        if result == TrieResult.FAILED:
    42            if sym:
    43                end -= 1
    44                chars = sym
    45                sym = None
    46            start += len(chars)
    47            chunks.append(chars)
    48            current = trie
    49        elif result == TrieResult.EXISTS:
    50            sym = chars
    51
    52        end += 1
    53
    54        if result != TrieResult.FAILED and end > size:
    55            chunks.append(chars)
    56
    57    return "".join(mapping.get(chars, chars) for chars in chunks)

Converts a time string given a mapping.

###### Examples:

>     >>> format_time("%Y", {"%Y": "YYYY"})
>     'YYYY'

> Args: mapping: dictionary of time format to target time format. trie: optional trie, can be passed in for performance.

> Returns: The converted time string.