當前位置: 首頁>>代碼示例>>Python>>正文


Python utils.timezone方法代碼示例

本文整理匯總了Python中django.utils.timezone方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.timezone方法的具體用法?Python utils.timezone怎麽用?Python utils.timezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.utils的用法示例。


在下文中一共展示了utils.timezone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_labelling_counts

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def test_labelling_counts(self):
        d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala"))
        msg = self.create_message(self.unicef, 301, self.ann, "Hi", created_on=d1)
        msg.label(self.aids, self.tea)

        self.assertEqual(DailyCount.get_by_label([self.aids], "I").day_totals(), [(date(2015, 1, 1), 1)])
        self.assertEqual(DailyCount.get_by_label([self.tea], "I").day_totals(), [(date(2015, 1, 1), 1)])

        msg.unlabel(self.aids)

        self.assertEqual(DailyCount.get_by_label([self.aids], "I").day_totals(), [(date(2015, 1, 1), 0)])
        self.assertEqual(DailyCount.get_by_label([self.tea], "I").day_totals(), [(date(2015, 1, 1), 1)])

        msg.clear_labels()

        self.assertEqual(DailyCount.get_by_label([self.aids], "I").day_totals(), [(date(2015, 1, 1), 0)])
        self.assertEqual(DailyCount.get_by_label([self.tea], "I").day_totals(), [(date(2015, 1, 1), 0)]) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:19,代碼來源:tests.py

示例2: ask_auto_now_add_addition

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def ask_auto_now_add_addition(self, field_name, model_name):
        """Adding an auto_now_add field to a model."""
        if not self.dry_run:
            choice = self._choice_input(
                "You are trying to add the field '{}' with 'auto_now_add=True' "
                "to {} without a default; the database needs something to "
                "populate existing rows.\n".format(field_name, model_name),
                [
                    "Provide a one-off default now (will be set on all "
                    "existing rows)",
                    "Quit, and let me add a default in models.py",
                ]
            )
            if choice == 2:
                sys.exit(3)
            else:
                return self._ask_default(default='timezone.now')
        return None 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:20,代碼來源:questioner.py

示例3: __init__

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def __init__(self, *args, **kwargs):
        super(StaffEvent, self).__init__(*args, **kwargs)
        for field in ['beginning', 'end', 'english_beginning', 'english_end']:
            self.fields[field] = date_input(self.fields[field])

        if self.instance:
            if getattr(self.instance, "beginning"):
                beginning = getattr(self.instance, "beginning")
                beginning = beginning.astimezone(timezone('Asia/Tokyo'))
                self.fields["beginning_time"].initial = "%02d:%02d" % (beginning.hour, beginning.minute)
            if getattr(self.instance, "end"):
                end = getattr(self.instance, "end")
                end = end.astimezone(timezone('Asia/Tokyo'))
                self.fields["end_time"].initial = "%02d:%02d" % (end.hour, end.minute)
            if getattr(self.instance, "english_beginning"):
                english_beginning = getattr(self.instance, "english_beginning")
                self.fields["english_beginning_time"].initial = "%02d:%02d" % (english_beginning.hour, english_beginning.minute)
            if getattr(self.instance, "english_end"):
                english_end = getattr(self.instance, "english_end")
                self.fields["english_end_time"].initial = "%02d:%02d" % (english_end.hour, english_end.minute)

        self.fields['image'].required = True 
開發者ID:MagiCircles,項目名稱:SchoolIdolAPI,代碼行數:24,代碼來源:forms.py

示例4: ask_auto_now_add_addition

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def ask_auto_now_add_addition(self, field_name, model_name):
        "Adding an auto_now_add field to a model"
        if not self.dry_run:
            choice = self._choice_input(
                "You are trying to add the field '{}' with 'auto_now_add=True' "
                "to {} without a default; the database needs something to "
                "populate existing rows.\n".format(field_name, model_name),
                [
                    "Provide a one-off default now (will be set on all "
                    "existing rows)",
                    "Quit, and let me add a default in models.py",
                ]
            )
            if choice == 2:
                sys.exit(3)
            else:
                return self._ask_default(default='timezone.now')
        return None 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:20,代碼來源:questioner.py

示例5: new_messages

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def new_messages(self, day, count):
        created = []
        for m in range(count):
            self._incoming_backend_id += 1
            created_on = self.anytime_on_day(day, pytz.timezone("Africa/Kampala"))

            created.append(
                self.create_message(self.unicef, self._incoming_backend_id, self.ann, "Hello", created_on=created_on)
            )
        return created 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:12,代碼來源:tests.py

示例6: new_outgoing

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def new_outgoing(self, user, day, count):
        created = []
        for m in range(count):
            self._outgoing_backend_id += 1
            created_on = self.anytime_on_day(day, pytz.timezone("Africa/Kampala"))

            created.append(
                self.create_outgoing(
                    self.unicef, user, self._outgoing_backend_id, "B", "Hello", self.ann, created_on=created_on
                )
            )
        return created 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:14,代碼來源:tests.py

示例7: test_case_counts_opened

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def test_case_counts_opened(self):
        d1 = self.anytime_on_day(date(2015, 1, 1), pytz.timezone("Africa/Kampala"))
        msg2 = self.create_message(self.unicef, 234, self.ann, "Hello again", [self.aids], created_on=d1)

        with patch.object(timezone, "now", return_value=d1):
            case = Case.get_or_open(self.unicef, self.user1, msg2, "Summary", self.moh)

        self.assertEqual(
            DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_OPENED).day_totals(),
            [(date(2015, 1, 1), 1)],
        )

        self.assertEqual(
            DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_OPENED).day_totals(), [(date(2015, 1, 1), 1)]
        )

        self.assertEqual(DailyCount.get_by_partner([case.assignee], DailyCount.TYPE_CASE_CLOSED).day_totals(), [])
        self.assertEqual(DailyCount.get_by_org([self.unicef], DailyCount.TYPE_CASE_CLOSED).day_totals(), [])

        self.assertEqual(
            DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_OPENED).day_totals(),
            [(date(2015, 1, 1), 1)],
        )
        self.assertEqual(
            DailyCount.get_by_user(self.unicef, [self.user1], DailyCount.TYPE_CASE_CLOSED).day_totals(), []
        ) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:28,代碼來源:tests.py

示例8: test_user_export

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def test_user_export(self):
        url = reverse("statistics.dailycountexport_create")

        tz = pytz.timezone("Africa/Kampala")
        d1 = date(2016, 1, 1)
        d2 = date(2016, 1, 15)

        # Jan 1st
        with patch.object(timezone, "now", return_value=self.anytime_on_day(d1, tz)):
            [msg] = self.new_messages(d1, 1)
            Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)

        # Jan 15th
        with patch.object(timezone, "now", return_value=self.anytime_on_day(d2, tz)):
            [msg] = self.new_messages(d2, 1)
            Case.get_or_open(self.unicef, self.user1, msg, "summary", self.moh)

        self.new_outgoing(self.user1, d1, 1)  # Jan 1st
        self.new_outgoing(self.user3, d2, 1)  # Jan 15th

        self.login(self.admin)

        response = self.url_post_json("unicef", url, {"type": "U", "after": "2016-01-01", "before": "2016-01-31"})
        self.assertEqual(response.status_code, 200)

        export = DailyCountExport.objects.get()
        workbook = self.openWorkbook(export.filename)
        (replies_sheet, cases_opened_sheet, cases_closed_sheet) = workbook.sheets()

        self.assertEqual(replies_sheet.nrows, 32)
        self.assertExcelRow(replies_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(replies_sheet, 1, [d1, 0, 1, 0, 0], tz=tz)
        self.assertExcelRow(replies_sheet, 15, [d2, 1, 0, 0, 0], tz=tz)

        self.assertExcelRow(cases_opened_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(cases_opened_sheet, 1, [d1, 0, 1, 0, 0], tz=tz)
        self.assertExcelRow(cases_opened_sheet, 15, [d2, 0, 1, 0, 0], tz=tz)

        self.assertExcelRow(cases_closed_sheet, 0, ["Date", "Carol", "Evan", "Kidus", "Rick"])
        self.assertExcelRow(cases_closed_sheet, 1, [d1, 0, 0, 0, 0], tz=tz)
        self.assertExcelRow(cases_closed_sheet, 15, [d2, 0, 0, 0, 0], tz=tz) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:43,代碼來源:tests.py

示例9: test_incoming_chart

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def test_incoming_chart(self):
        url = reverse("statistics.incoming_chart")

        self.assertLoginRedirect(self.url_get("unicef", url), url)

        self.new_messages(date(2016, 1, 1), 1)  # Jan 1st
        msgs = self.new_messages(date(2016, 1, 15), 1)  # Jan 15th
        self.new_messages(date(2016, 1, 16), 2)  # Jan 16th

        # add label to message on Jan 15th
        msgs[0].label(self.tea)

        self.login(self.user3)

        # simulate making requests on March 10th
        with patch.object(timezone, "now", return_value=datetime(2016, 3, 10, 9, 0, tzinfo=pytz.UTC)):
            response = self.url_get("unicef", url)

            series = response.json["series"]
            self.assertEqual(len(series), 60)
            self.assertEqual(series[0], [date_to_milliseconds(date(2016, 1, 11)), 0])  # from Jan 11th
            self.assertEqual(series[4], [date_to_milliseconds(date(2016, 1, 15)), 1])
            self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 16)), 2])
            self.assertEqual(series[-1], [date_to_milliseconds(date(2016, 3, 10)), 0])  # to March 10th

            response = self.url_get("unicef", url + "?label=%d" % self.tea.pk)

            series = response.json["series"]
            self.assertEqual(len(series), 60)
            self.assertEqual(series[4], [date_to_milliseconds(date(2016, 1, 15)), 1])
            self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 16)), 0]) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:33,代碼來源:tests.py

示例10: test_most_used_labels_chart

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def test_most_used_labels_chart(self):
        url = reverse("statistics.labels_pie_chart")

        self.assertLoginRedirect(self.url_get("unicef", url), url)

        old_msgs = self.new_messages(date(2016, 2, 1), 2)  # Feb 1st (not included)
        old_msgs[0].label(self.aids)

        cur_msgs = self.new_messages(date(2016, 2, 20), 3)  # Feb 20th (included)
        cur_msgs[0].label(self.aids)
        cur_msgs[1].label(self.tea)
        cur_msgs[2].label(self.tea)

        self.login(self.admin)

        # simulate making requests on March 10th
        with patch.object(timezone, "now", return_value=datetime(2016, 3, 10, 9, 0, tzinfo=pytz.UTC)):
            response = self.url_get("unicef", url)

            series = response.json["series"]
            self.assertEqual(len(series), 2)
            self.assertEqual(series[0], {"y": 2, "name": "Tea"})
            self.assertEqual(series[1], {"y": 1, "name": "AIDS"})

            # check when there are more labels than can be displayed on pie chart
            for l in range(10):
                new_label = self.create_label(self.unicef, "L-20%d" % l, "Label #%d" % l, "Description")
                cur_msgs[0].label(new_label)

            response = self.url_get("unicef", url)

            series = response.json["series"]
            self.assertEqual(len(series), 10)
            self.assertEqual(series[0], {"y": 2, "name": "Tea"})
            self.assertEqual(series[1], {"y": 1, "name": "AIDS"})
            self.assertEqual(series[2], {"y": 1, "name": "Label #0"})  # labels with same count in alphabetical order
            self.assertEqual(series[8], {"y": 1, "name": "Label #6"})
            self.assertEqual(series[9], {"y": 3, "name": "Other"}) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:40,代碼來源:tests.py

示例11: _ask_default

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def _ask_default(self, default=''):
        """
        Prompt for a default value.

        The ``default`` argument allows providing a custom default value (as a
        string) which will be shown to the user and used as the return value
        if the user doesn't provide any other input.
        """
        print("Please enter the default value now, as valid Python")
        if default:
            print(
                "You can accept the default '{}' by pressing 'Enter' or you "
                "can provide another value.".format(default)
            )
        print("The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now")
        print("Type 'exit' to exit this prompt")
        while True:
            if default:
                prompt = "[default: {}] >>> ".format(default)
            else:
                prompt = ">>> "
            code = input(prompt)
            if not code and default:
                code = default
            if not code:
                print("Please enter some code, or 'exit' (with no quotes) to exit.")
            elif code == "exit":
                sys.exit(1)
            else:
                try:
                    return eval(code, {}, {"datetime": datetime_safe, "timezone": timezone})
                except (SyntaxError, NameError) as e:
                    print("Invalid input: %s" % e) 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:35,代碼來源:questioner.py

示例12: _ask_default

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def _ask_default(self, default=''):
        """
        Prompt for a default value.

        The ``default`` argument allows providing a custom default value (as a
        string) which will be shown to the user and used as the return value
        if the user doesn't provide any other input.
        """
        print("Please enter the default value now, as valid Python")
        if default:
            print(
                "You can accept the default '{}' by pressing 'Enter' or you "
                "can provide another value.".format(default)
            )
        print("The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now")
        print("Type 'exit' to exit this prompt")
        while True:
            if default:
                prompt = "[default: {}] >>> ".format(default)
            else:
                prompt = ">>> "
            if six.PY3:
                # Six does not correctly abstract over the fact that
                # py3 input returns a unicode string, while py2 raw_input
                # returns a bytestring.
                code = input(prompt)
            else:
                code = input(prompt).decode(sys.stdin.encoding)
            if not code and default:
                code = default
            if not code:
                print("Please enter some code, or 'exit' (with no quotes) to exit.")
            elif code == "exit":
                sys.exit(1)
            else:
                try:
                    return eval(code, {}, {"datetime": datetime_safe, "timezone": timezone})
                except (SyntaxError, NameError) as e:
                    print("Invalid input: %s" % e) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:41,代碼來源:questioner.py

示例13: get_timezone

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def get_timezone(self):
        tz = self.timezone if self.timezone else self.get_detected_timezone()
        return tz 
開發者ID:hacktoolkit,項目名稱:django-htk,代碼行數:5,代碼來源:models.py

示例14: get_django_timezone

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def get_django_timezone(self):
        tz = self.get_timezone()
        django_timezone = pytz.timezone(tz)
        return django_timezone 
開發者ID:hacktoolkit,項目名稱:django-htk,代碼行數:6,代碼來源:models.py

示例15: get_local_time

# 需要導入模塊: from django import utils [as 別名]
# 或者: from django.utils import timezone [as 別名]
def get_local_time(self, dt=None):
        """Gets the current local time for User
        If `dt` is specified, format `dt` into User's timezone
        """
        tz = self.get_django_timezone()
        if dt is None:
            local_time = utcnow().astimezone(tz)
        else:
            local_time = dt.astimezone(tz)
        return local_time 
開發者ID:hacktoolkit,項目名稱:django-htk,代碼行數:12,代碼來源:models.py


注:本文中的django.utils.timezone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。