关于python:我如何比较timezone.now和item.deadline

how can i compare timezone.now and item.deadline

嗨,我要服务old book sale service for university student

我创建了itempost模型,当用户发布他们的书籍时,itempost的截止日期保存在deadline中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.db import models
from django.conf import settings
from django.utils import timezone


def localtime():
    return timezone.localtime(timezone.now())


class ItemPost(models.Model):


    title = models.TextField(
        )

    created_at = models.DateTimeField(
        default=localtime
        )

    is_deleted = models.BooleanField(
        default=False,
        verbose_name="??? ???",
        )

    # ????? ??? ??
    def deadline_def(self):
        year_of_item_created = self.created_at.year

        if self.created_at.month <= 6:
            return timezone.datetime(year_of_item_created, 6, 30)
        else:
            return timezone.datetime(year_of_item_created, 12, 31)

    deadline = property(deadline_def)

    # ??? ??? ?? ????? ??
    def is_ended_semester_def(self):
        now = timezone.now()
        if now > self.deadline:
            return True
        return False

    is_ended_semester = property(is_ended_semester_def)

    def __str__(self):
       return self.title

我要比较项目的截止日期、时区。现在())

并返回真或假

但如果我命令item.is_ended_semester我就不能

埃多克斯1〔3〕

我怎么解决这个问题?

1
2
3
4
5
item.deadline
> datetime.datetime(2017, 6, 30, 0, 0)

timezone.now()
> datetime.datetime(2017, 7, 14, 8, 50, 57, 91304, tzinfo=<UTC>)


我用timezone.utc解决了我的问题。

1
2
3
4
5
6
7
8
# ????? ??? ??
def deadline_def(self):
    year_of_item_created = self.created_at.year

    if self.created_at.month <= 6:
        return timezone.datetime(year_of_item_created, 6, 30, tzinfo=timezone.utc)
    else:
        return timezone.datetime(year_of_item_created, 12, 31, tzinfo=timezone.utc)