---
slug: "django-timezone-freezegun"
title: "How to Use freezegun in Django Tests to Set the Date One Day Ahead with USE_TZ=True"
description: "Use freezegun in Django tests so that \"one day later\" or relative offsets work correctly under `USE_TZ=True`."
url: "https://www.ytyng.com/en/blog/django-timezone-freezegun"
publish_date: "2019-03-25T23:57:50Z"
created: "2019-03-25T23:57:50Z"
updated: "2026-05-11T13:11:12.824Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/59e0426e30124a90b3ef12d9d1d4f660.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# How to Use freezegun in Django Tests to Set the Date One Day Ahead with USE_TZ=True

reezegun is a Python library.

It hooks into the current time retrieval with datetime, allowing you to perform tests that assume "the next day" and similar scenarios.

If you want to perform tests in Django that assume "the next day":

```python
今日のログインボーナス()
with freezegun.freeze_time(datetime.timedelta(days=1)):
    明日のログインボーナス()
```

This might cause issues such as test failures before 9:00 AM when developing in the JST timezone.

```python
from django.utils import timezone

今日のログインボーナス()
with freezegun.freeze_time(timezone.now() + datetime.timedelta(days=1)):
    明日のログインボーナス()
```

The same goes for this case.

If you pass a timezone-aware datetime other than UTC to the freeze_time argument, the timezone may not be handled correctly in a development environment using the JST timezone.

To correctly determine the next day, you should do:

```python
import datetime

今日のログインボーナス()
with freezegun.freeze_time(datetime.datetime.now() + datetime.timedelta(days=1)):
    明日のログインボーナス()
```

or

```python
import datetime

今日のログインボーナス()
with freezegun.freeze_time(datetime.timedelta(days=1), tz_offset=9):
    明日のログインボーナス()
```

```python
import datetime
from django.utils import timezone
tz = timezone.get_current_timezone()

今日のログインボーナス()
with freezegun.freeze_time(datetime.timedelta(days=1), tz_offset=tz._utcoffset):
    明日のログインボーナス()
```

You need to set it up like this.

## To generally freeze_time with an aware datetime

```python
aware_datetime = ...

with freezegun.freeze_time(
    aware_datetime, tz_offset=aware_datetime._utcoffset):
    ...
```
