0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 04:12:23 +01:00
posthog/ee/clickhouse/models/util.py
Eric Duong 85bc250dac
Prune unused ee code (#4410)
* prune model functions

* prune more misc
2021-05-20 10:15:03 -04:00

38 lines
800 B
Python

import json
from typing import Optional, Union
import pytz
from dateutil.parser import isoparse
from django.utils import timezone
from posthog.models.property import Property
def is_json(val):
if isinstance(val, int):
return False
try:
int(val)
return False
except:
pass
try:
json.loads(val)
except (ValueError, TypeError):
return False
return True
def cast_timestamp_or_now(timestamp: Optional[Union[timezone.datetime, str]]) -> str:
if not timestamp:
timestamp = timezone.now()
# clickhouse specific formatting
if isinstance(timestamp, str):
timestamp = isoparse(timestamp)
else:
timestamp = timestamp.astimezone(pytz.utc)
return timestamp.strftime("%Y-%m-%d %H:%M:%S.%f")