0

I would like to create a site that would show list of tournaments. Tournament may take place more times per day repeatedly. I'll give an example:
Tournament 1:

  • Monday 16:00
  • Monday 18:00
  • Tuesday 16:00
  • Tuesday 18:00
  • Friday 16:00
  • Friday 18:00

I would like to avoid duplicity, so I use m2m on days and times:

class Day(models.Model):
    name = models.CharField(max_length=10)
    weekday = models.IntegerField()

    def __unicode__(self):
        return self.name

class Time(models.Model):
    time = models.TimeField()

class Tournament(models.Model):
    name = models.CharField("Tournament name", max_length=200)
    currency = models.CharField(max_length=5, choices=CURRENCIES, default='USD')
    prize = models.DecimalField(max_digits=20, decimal_places=2)
    entry = models.DecimalField(max_digits=20, decimal_places=2, default=0)
    fee = models.DecimalField(max_digits=20, decimal_places=2, default=0)
    password = models.CharField("password", max_length=200, null=True, blank=True)
    tournament_id = models.CharField("Tournament ID", max_length=50, null=True, blank=True)
    number_of_players = models.DecimalField(max_digits=20, decimal_places=0, default='0', null=True, blank=True)
    type = models.ManyToManyField('room.Type')
    room = models.ManyToManyField('room.Room')
    requirements_difficulty = models.IntegerField('Tournament Difficulty',validators=[MinValueValidator(1), MaxValueValidator(30)],null=True, blank=True)
    requirements_text = models.CharField("Requirements Description", default='no requirements', max_length=1000,null=True, blank=True)
    days = models.ManyToManyField(Day, related_name='days')
    times = models.ManyToManyField(Time, related_name='times')

There are more parameters, but important are only last two. I create times and days to add as many hours and days as I wish.

Problem is with timezone. TimeField is not timezone aware. I wanted user to select days and then hours. Is it somehow possible to create this functionality timezone aware?

I guess I could store it in db separately and then create timezone aware datetime objects in views.py Correct? What is the best way here?

Community
  • 1
  • 1
Lucas03
  • 2,267
  • 2
  • 32
  • 60

1 Answers1

0

If you need to convert a naive datetime to aware datatime, you can replace its tzinfo

from datetime import datetime
import pytz


>>> _now = datetime.now()  # naive datatime
>>> _now
datetime.datetime(2015, 9, 25, 17, 18, 56, 596488)
>>> _now = _now.replace(tzinfo=pytz.timezone('America/Chicago'))  # aware datetime
>>> _now
datetime.datetime(2015, 9, 25, 17, 16, 36, 991419, tzinfo=<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD>)

Using 'America/Chicago' only as example, you can use your own timezone.

And if you want to show an aware datetime as in another datetime:

>>> _now
datetime.datetime(2015, 9, 25, 17, 16, 36, 991419, tzinfo=<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD>)
>>> _now.astimezone(pytz.timezone('America/Lima'))
datetime.datetime(2015, 9, 25, 18, 7, 36, 991419, tzinfo=<DstTzInfo 'America/Lima' PET-1 day, 19:00:00 STD>)

The last line will show _now, from America/Chicago timezone as in America/Lima time.

Gocht
  • 9,924
  • 3
  • 42
  • 81
  • Hi, I know I can replace tzinfo in datetime. But I would like to get weekday as string and time in hours and minutes from user (and his timezone), save it to db and then print it as datetime object with user's timezone as well. – Lucas03 Sep 26 '15 at 10:00
  • @Lucas03 Do you save user's timezone? – Gocht Sep 26 '15 at 14:37
  • I am using django-easy-timezones. I am thinking storing submitted tournaments in UTC and then print them with current user timezone. – Lucas03 Sep 27 '15 at 10:22