Skip to content

Commit

Permalink
dt scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
adasium committed Feb 28, 2024
1 parent 2c718ad commit 428ff3e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
43 changes: 43 additions & 0 deletions bin/chom_dt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
from __future__ import annotations

import sys
import argparse

from datetime import datetime
from datetime import timedelta
from datetime import timezone
from typing import Sequence


LOGZIO_FORMAT = '%b %d, %Y @ %H:%M:%S.%f'
GCF_FORMAT = '%Y-%m-%dT%H:%M:%S'


def _columns(string: str) -> list[str] | None:
chunks = string.split(',')
return [chunk.strip() for chunk in chunks]


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('-gcf', '--google-cloud-functions', required=False, action='store_true')
args = parser.parse_args()

timestamp_str = sys.stdin.read().strip()

local_timestamp = datetime.strptime(timestamp_str, LOGZIO_FORMAT)
utc_timestamp = local_timestamp.astimezone(timezone.utc)

if args.google_cloud_functions:
timestamp_before = utc_timestamp - timedelta(minutes=1)
timestamp_after = utc_timestamp + timedelta(minutes=1)
print(f'timestamp >= "{timestamp_before:{GCF_FORMAT}}" AND timestamp <= "{timestamp_after:{GCF_FORMAT}}"')
return 0

print(utc_timestamp)
return 0


if __name__ == '__main__':
raise SystemExit(main())
16 changes: 11 additions & 5 deletions bin/chom_json2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ def _inner():


def try_parse_datetime(dt_str: str) -> datetime:
try:
return datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S.%f %Z")
except ValueError:
print("Error: Unable to parse datetime string.")
raise
formats = [
"%Y-%m-%d %H:%M:%S.%f %Z",
"%m/%d/%Y",
]
for format in formats:
try:
return datetime.strptime(dt_str, format)
except ValueError:
pass
raise ValueError(f"Error: Unable to parse datetime string. tried: {formats}")



def format_output_item(
Expand Down

0 comments on commit 428ff3e

Please sign in to comment.