Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] document_exporter document dates are off by one day
**Describe the bug**
When running the document_exporter script with the "-f" flag, all documents (and thumbnails) are off by one day.

**To Reproduce**
Steps to reproduce the behavior:
1. Running paperless-ngx on a raspi4 within a docker-container (The OS is set to the timezone "Europe/Berlin" and also within docker-compose PAPERLESS_TIME_ZONE is also set to Europe/Berlin). PAPERLESS_FILENAME_FORMAT is set to `{correspondent}/{document_type}/{created_year}/{created_year}{created_month}{created_day}_{title}_{tag_list}`
2. Consume a document as set the date to e.g. 11.03.2022
3. Check that the document is correctly stored with 20220311_xxx.pdf as filename
4. Run the document_exporter script the document (and the thumbnail) is saved as 20220310_xxx.pdf

**Expected behavior**
After running the document_exporter script the document is correctly saved as 20220311_xxx.pdf

**Relevant information**
 - RaspberryPiOS 64bit
 - Chrome
 - 1.6.0 (but was already with 1.5.0
 - docker
[BUG] document_exporter document dates are off by one day
**Describe the bug**
When running the document_exporter script with the "-f" flag, all documents (and thumbnails) are off by one day.

**To Reproduce**
Steps to reproduce the behavior:
1. Running paperless-ngx on a raspi4 within a docker-container (The OS is set to the timezone "Europe/Berlin" and also within docker-compose PAPERLESS_TIME_ZONE is also set to Europe/Berlin). PAPERLESS_FILENAME_FORMAT is set to `{correspondent}/{document_type}/{created_year}/{created_year}{created_month}{created_day}_{title}_{tag_list}`
2. Consume a document as set the date to e.g. 11.03.2022
3. Check that the document is correctly stored with 20220311_xxx.pdf as filename
4. Run the document_exporter script the document (and the thumbnail) is saved as 20220310_xxx.pdf

**Expected behavior**
After running the document_exporter script the document is correctly saved as 20220311_xxx.pdf

**Relevant information**
 - RaspberryPiOS 64bit
 - Chrome
 - 1.6.0 (but was already with 1.5.0
 - docker


Discussion:
Thanks for reporting. Possibly related: #224 
I ran into that yesterday as well. Its a Synology NAS, `TZ` is set to `Europe/Berlin` (if important). One hint regarding #224: It is unrelated to whether the date was auto-detected or not. I can simply set a date myself, the Filename will always be off by one day.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This should still be fixed I think. More soon then later, it is very annoying for users I fear 
The same problem is also present in the `document_renamer`. I used the default filename format previously (e.g. 000000123.pdf) and changed it to something else. After that I ran the document_renamer script and it populated all placeholders correctly, except for the {created_day} being off one day on all my ~800 documents.
Just updated to 1.7.0, didn't fix it. So it seems to be a different issue as #224 which was fixed in this release.
When editing the document using the web ui, the filename has the correct date, however after running the `document_renamer` it's off by one day again.
Thanks for reporting. Possibly related: #224 
I ran into that yesterday as well. Its a Synology NAS, `TZ` is set to `Europe/Berlin` (if important). One hint regarding #224: It is unrelated to whether the date was auto-detected or not. I can simply set a date myself, the Filename will always be off by one day.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This should still be fixed I think. More soon then later, it is very annoying for users I fear 
The same problem is also present in the `document_renamer`. I used the default filename format previously (e.g. 000000123.pdf) and changed it to something else. After that I ran the document_renamer script and it populated all placeholders correctly, except for the {created_day} being off one day on all my ~800 documents.
Just updated to 1.7.0, didn't fix it. So it seems to be a different issue as #224 which was fixed in this release.
When editing the document using the web ui, the filename has the correct date, however after running the `document_renamer` it's off by one day again.

Resolving patch:
diff --git a/src/documents/models.py b/src/documents/models.py
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -11,7 +11,6 @@
 from django.contrib.auth.models import User
 from django.db import models
 from django.utils import timezone
-from django.utils.timezone import is_aware
 from django.utils.translation import gettext_lazy as _
 from documents.parsers import get_default_file_extension
 
@@ -210,10 +209,8 @@ class Meta:
         verbose_name_plural = _("documents")
 
     def __str__(self):
-        if is_aware(self.created):
-            created = timezone.localdate(self.created).isoformat()
-        else:
-            created = datetime.date.isoformat(self.created)
+        created = datetime.date.isoformat(self.created)
+
         if self.correspondent and self.title:
             return f"{created} {self.correspondent} {self.title}"
         else:


Test patch:
diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py
--- a/src/documents/tests/test_document_model.py
+++ b/src/documents/tests/test_document_model.py
@@ -3,6 +3,11 @@
 from pathlib import Path
 from unittest import mock
 
+try:
+    import zoneinfo
+except ImportError:
+    import backports.zoneinfo as zoneinfo
+
 from django.test import override_settings
 from django.test import TestCase
 from django.utils import timezone
@@ -55,6 +60,24 @@ def test_file_name(self):
         )
         self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
 
+    def test_file_name_with_timezone(self):
+
+        doc = Document(
+            mime_type="application/pdf",
+            title="test",
+            created=timezone.datetime(
+                2020,
+                12,
+                25,
+                0,
+                0,
+                0,
+                0,
+                zoneinfo.ZoneInfo("Europe/Berlin"),
+            ),
+        )
+        self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
+
     def test_file_name_jpg(self):
 
         doc = Document(