martinthenext/daytobase

Implement time module

Opened this issue · 2 comments

Currently, daytobase shows record time stamps in UTC. Instead, we have to query and record user's timezone and use it to:

  1. Convert UTC time stamps to user's timezone when outputting information
  2. Interpret time tags (#t 12:04 pm) and output UTC time stamps

Looks like PyMongo suggests we use pytz module. We need to:

  1. Figure out how to serialize/deserialize timezone objects to store them in MongoDB. Let us assume timezone is going to be defined by a string.
  2. Write functions that convert from a string representation to pytz timezone object and back
  3. Write a function get_datetime_repr(dt, user) where dt is a UTC datetime and user in an object that has a timezone field with a timezone string representation in it, that outputs a string representation of dt in the right timezone.
  4. Write a function get_time_tag(message, user) where message is a text of a message, i.e. "ate a banana #t one hour ago", and outputs a datetime object in UTC.

For the latter, use cases we definitely need to handle:

  1. "#t 2016.08.23 18:32 flight to London" - we already handle that
  2. #t one hour ago
  3. #t 12:44

After that is done it would be nice to have at least some minor unittests for these functions.

Just did the first 3 items:

  1. pytz.all_timzones lists the string representations of all timezones
  2. london_tz = pytz.timezone("Europe/London") converts from a string representation to a timezone object. london_tz.zone converts back to the string representation
  3. Wrote the get_datetime_repr() function in timezone_conversions.py along with a very basic test (this is also just to get some tests set up which makes it easier to add more!)

Merged this module into the dev branch. I also renamed it timezone.py (like Django's module), as time.py clashed with the builtin time module.