diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-09-15 16:46:17 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-09-15 16:46:17 +0000 |
commit | 28cc22419e32a65fea2d1678400265b8cabc3aff (patch) | |
tree | ff9ac1991fd48490b21ef6aa9015a347a165e2d9 /sqlglot/time.py | |
parent | Initial commit. (diff) | |
download | sqlglot-28cc22419e32a65fea2d1678400265b8cabc3aff.tar.xz sqlglot-28cc22419e32a65fea2d1678400265b8cabc3aff.zip |
Adding upstream version 6.0.4.upstream/6.0.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/time.py')
-rw-r--r-- | sqlglot/time.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/sqlglot/time.py b/sqlglot/time.py new file mode 100644 index 0000000..16314c5 --- /dev/null +++ b/sqlglot/time.py @@ -0,0 +1,45 @@ +# the generic time format is based on python time.strftime +# https://docs.python.org/3/library/time.html#time.strftime +from sqlglot.trie import in_trie, new_trie + + +def format_time(string, mapping, trie=None): + """ + Converts a time string given a mapping. + + Examples: + >>> format_time("%Y", {"%Y": "YYYY"}) + 'YYYY' + + mapping: Dictionary of time format to target time format + trie: Optional trie, can be passed in for performance + """ + start = 0 + end = 1 + size = len(string) + trie = trie or new_trie(mapping) + current = trie + chunks = [] + sym = None + + while end <= size: + chars = string[start:end] + result, current = in_trie(current, chars[-1]) + + if result == 0: + if sym: + end -= 1 + chars = sym + sym = None + start += len(chars) + chunks.append(chars) + current = trie + elif result == 2: + sym = chars + + end += 1 + + if result and end > size: + chunks.append(chars) + + return "".join(mapping.get(chars, chars) for chars in chunks) |