zulip/zerver/lib/timestamp.py
umkay d260a22637 Add a new statistics/analytics framework.
This is a first pass at building a framework for collecting various
stats about realms, users, streams, etc. Includes:
* New analytics tables for storing counts data
* Raw SQL queries for pulling data from zerver/models.py tables
* Aggregation functions for aggregating hourly stats into daily stats, and
  aggregating user/stream level stats into realm level stats
* A management command for pulling the data

Note that counts.py was added to the linter exclude list due to errors
around %%s.
2016-10-04 17:18:54 -07:00

24 lines
757 B
Python

from __future__ import absolute_import
import datetime
import calendar
from django.utils.timezone import utc
def is_timezone_aware(datetime_object):
# type: (datetime.datetime) -> bool
return datetime_object.tzinfo is not None
def timestamp_to_datetime(timestamp):
# type: (float) -> datetime.datetime
return datetime.datetime.utcfromtimestamp(float(timestamp)).replace(tzinfo=utc)
def datetime_to_timestamp(datetime_object):
# type: (datetime.datetime) -> int
return calendar.timegm(datetime_object.timetuple())
def datetime_to_string(datetime_object):
# type: (datetime.datetime) -> str
assert is_timezone_aware(datetime_object)
date_string = datetime_object.strftime('%Y-%m-%d %H:%M:%S%z')
return date_string