# Path: dju_common/tools.py
# def datetime_to_dtstr(dt=None):
#     """
#     Comvert datetime to short text.
#     If datetime has timezone then it will be convert to UTC0.
#     """
#     if dt is None:
#         dt = datetime.datetime.utcnow()
#     elif timezone.is_aware(dt):
#         dt = dt.astimezone(tz=pytz.UTC)
#     return int2base36(int(time.mktime(dt.timetuple()) * 1e3 + dt.microsecond / 1e3))
# 
# def dtstr_to_datetime(dtstr, to_tz=None, fail_silently=True):
#     """
#     Convert result from datetime_to_dtstr to datetime in timezone UTC0.
#     """
#     try:
#         dt = datetime.datetime.utcfromtimestamp(int(dtstr, 36) / 1e3)
#         if to_tz:
#             dt = timezone.make_aware(dt, timezone=pytz.UTC)
#             if to_tz != pytz.UTC:
#                 dt = dt.astimezone(to_tz)
#         return dt
#     except ValueError, e:
#         if not fail_silently:
#             raise e
#         return None
# 
# def int2base36(n):
#     """
#     Convert int base10 to base36.
#     Back convert: int('<base36>', 36)
#     """
#     assert isinstance(n, (int, long))
#     c = '0123456789abcdefghijklmnopqrstuvwxyz'
#     if n < 0:
#         return '-' + int2base36(-n)
#     elif n < 36:
#         return c[n]
#     b36 = ''
#     while n != 0:
#         n, i = divmod(n, 36)
#         b36 = c[i] + b36
#     return b36


import datetime
import pytz
from django.test import TestCase
from django.utils import timezone
from dju_common.tools import datetime_to_dtstr, dtstr_to_datetime, int2base36
        self.assertEqual(dtstr, 'igsiclph')
        self.assertEqual(dtstr_to_datetime(dtstr), dt)

        dt = datetime.datetime(2015, 7, 9, 22, 15, 45, 125000)
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'ibwr6tph')
        self.assertEqual(dtstr_to_datetime(dtstr), dt)

    def test_datetime_to_dtstr_to_datetime_for_aware(self):
        dt1 = timezone.make_aware(datetime.datetime(2015, 11, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt1)
        self.assertEqual(dtstr, 'igse2a5h')
        dt2 = timezone.make_aware(dtstr_to_datetime(dtstr), pytz.UTC).astimezone(pytz.timezone('Europe/Kiev'))
        self.assertEqual(dt2, dt1)

        dt1 = timezone.make_aware(datetime.datetime(2015, 7, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt1)
        self.assertEqual(dtstr, 'ibwkrcdh')
        dt2 = timezone.make_aware(dtstr_to_datetime(dtstr), pytz.UTC).astimezone(pytz.timezone('Europe/Kiev'))
        self.assertEqual(dt2, dt1)

    def test_datetime_to_dtstr_to_datetime_with_tz(self):
        dt = timezone.make_aware(datetime.datetime(2015, 11, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'igse2a5h')
        self.assertEqual(dtstr_to_datetime(dtstr, to_tz=pytz.timezone('Europe/Kiev')), dt)

        dt = timezone.make_aware(datetime.datetime(2015, 7, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'ibwkrcdh')

# Path: dju_common/tools.py
# def datetime_to_dtstr(dt=None):
#     """
#     Comvert datetime to short text.
#     If datetime has timezone then it will be convert to UTC0.
#     """
#     if dt is None:
#         dt = datetime.datetime.utcnow()
#     elif timezone.is_aware(dt):
#         dt = dt.astimezone(tz=pytz.UTC)
#     return int2base36(int(time.mktime(dt.timetuple()) * 1e3 + dt.microsecond / 1e3))
# 
# def dtstr_to_datetime(dtstr, to_tz=None, fail_silently=True):
#     """
#     Convert result from datetime_to_dtstr to datetime in timezone UTC0.
#     """
#     try:
#         dt = datetime.datetime.utcfromtimestamp(int(dtstr, 36) / 1e3)
#         if to_tz:
#             dt = timezone.make_aware(dt, timezone=pytz.UTC)
#             if to_tz != pytz.UTC:
#                 dt = dt.astimezone(to_tz)
#         return dt
#     except ValueError, e:
#         if not fail_silently:
#             raise e
#         return None
# 
# def int2base36(n):
#     """
#     Convert int base10 to base36.
#     Back convert: int('<base36>', 36)
#     """
#     assert isinstance(n, (int, long))
#     c = '0123456789abcdefghijklmnopqrstuvwxyz'
#     if n < 0:
#         return '-' + int2base36(-n)
#     elif n < 36:
#         return c[n]
#     b36 = ''
#     while n != 0:
#         n, i = divmod(n, 36)
#         b36 = c[i] + b36
#     return b36


# Path: tests/tests/test_tools.py
import datetime
import pytz
from django.test import TestCase
from django.utils import timezone
from dju_common.tools import datetime_to_dtstr, dtstr_to_datetime, int2base36

        self.assertEqual(dtstr, 'igsiclph')
        self.assertEqual(dtstr_to_datetime(dtstr), dt)

        dt = datetime.datetime(2015, 7, 9, 22, 15, 45, 125000)
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'ibwr6tph')
        self.assertEqual(dtstr_to_datetime(dtstr), dt)

    def test_datetime_to_dtstr_to_datetime_for_aware(self):
        dt1 = timezone.make_aware(datetime.datetime(2015, 11, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt1)
        self.assertEqual(dtstr, 'igse2a5h')
        dt2 = timezone.make_aware(dtstr_to_datetime(dtstr), pytz.UTC).astimezone(pytz.timezone('Europe/Kiev'))
        self.assertEqual(dt2, dt1)

        dt1 = timezone.make_aware(datetime.datetime(2015, 7, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt1)
        self.assertEqual(dtstr, 'ibwkrcdh')
        dt2 = timezone.make_aware(dtstr_to_datetime(dtstr), pytz.UTC).astimezone(pytz.timezone('Europe/Kiev'))
        self.assertEqual(dt2, dt1)

    def test_datetime_to_dtstr_to_datetime_with_tz(self):
        dt = timezone.make_aware(datetime.datetime(2015, 11, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'igse2a5h')
        self.assertEqual(dtstr_to_datetime(dtstr, to_tz=pytz.timezone('Europe/Kiev')), dt)

        dt = timezone.make_aware(datetime.datetime(2015, 7, 9, 22, 15, 45, 125000), pytz.timezone('Europe/Kiev'))
        dtstr = datetime_to_dtstr(dt)
        self.assertEqual(dtstr, 'ibwkrcdh')

        self.assertEqual(dtstr_to_datetime(dtstr, to_tz=pytz.timezone('Europe/Kiev')), dt)