【问题标题】:django timezone aware on multiple days and hoursdjango 时区感知多天多小时
【发布时间】:2015-09-25 22:09:20
【问题描述】:

我想创建一个显示锦标赛列表的网站。比赛可能每天重复举行更多次。我举个例子:
锦标赛 1:

  • 星期一 16:00
  • 星期一 18:00
  • 星期二 16:00
  • 星期二 18:00
  • 星期五 16:00
  • 星期五 18:00

我想避免重复,所以我在日期和时间使用m2m:

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')

还有更多参数,但重要的是只有最后两个。我创建时间和日期以添加任意数量的小时和天数。

timezone 有问题。 TimeField 不支持时区。我希望用户选择天数,然后选择小时数。是否有可能以某种方式创建此功能时区感知?

我想我可以将它单独存储在 db 中,然后在 views.py 中创建时区感知日期时间对象对吗?这里最好的方法是什么?

【问题讨论】:

    标签: django datetime timezone


    【解决方案1】:

    如果您需要将一个朴素的日期时间转换为有意识的数据时间,您可以替换它的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>)
    

    仅以'America/Chicago' 为例,您可以使用自己的时区。

    如果您想像在另一个日期时间中一样显示有意识的日期时间:

    >>> _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>)
    

    最后一行将显示_now,来自America/Chicago 时区,如America/Lima 时间。

    【讨论】:

    • 嗨,我知道我可以在 datetime 中替换 tzinfo。但我想从用户(和他的时区)获取工作日作为字符串和时间(以小时和分钟为单位),将其保存到数据库,然后将其打印为带有用户时区的日期时间对象。
    • @Lucas03 你保存用户的时区吗?
    • 我正在使用 django-easy-timezones。我正在考虑以 UTC 存储提交的锦标赛,然后使用当前用户时区打印它们。
    猜你喜欢
    • 2013-07-08
    • 2016-05-07
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2012-03-30
    相关资源
    最近更新 更多