Repository: spyder-ide/spyder

Issue:
Spyder 4 is not preserving files creation time
Hello,Spyder developer，when i use Spyder 4.0.1 to edit my python file it change my file creation time. Spyder change my python file creation time to newest time but creation time is very important for me. Creation time is a important record for me so could your solve this issue？I am looking forward your reply . How can i recover my python file creation time? Creation time is important for me! my email：lin649665076@gmail.com.


Discussion:
Thanks for reporting. We haven't noticed this before.

> How can i recover my python file creation time?

There's no way to recover that date, sorry. So I'm afraid you'll have to use another editor until we fix this in Spyder.
@andfoy, please take a look at this one. This probably happens because we're not preserving the creation date after doing an atomic write.
> Thanks for reporting. We haven't noticed this before.
> 
> > How can i recover my python file creation time?
> 
> There's no way to recover that date, sorry. So I'm afraid you'll have to use another editor until we fix this in Spyder.

It's too bad. Creation time is a important record for me, because it record beginning to program.

Resolving patch:
diff --git a/spyder/utils/encoding.py b/spyder/utils/encoding.py
--- a/spyder/utils/encoding.py
+++ b/spyder/utils/encoding.py
@@ -17,6 +17,7 @@
 import re
 import os
 import sys
+import time
 import errno
 
 # Third-party imports
@@ -240,12 +241,15 @@ def write(text, filename, encoding='utf-8', mode='wb'):
         # Needed to fix file permissions overwritting.
         # See spyder-ide/spyder#9381.
         try:
-            original_mode = os.stat(filename).st_mode
+            file_stat = os.stat(filename)
+            original_mode = file_stat.st_mode
+            creation = file_stat.st_atime
         except OSError:  # Change to FileNotFoundError for PY3
             # Creating a new file, emulate what os.open() does
             umask = os.umask(0)
             os.umask(umask)
             original_mode = 0o777 & ~umask
+            creation = time.time()
         try:
             with atomic_write(filename, overwrite=True,
                               mode=mode) as textfile:
@@ -257,6 +261,9 @@ def write(text, filename, encoding='utf-8', mode='wb'):
                 with open(filename, mode) as textfile:
                     textfile.write(text)
         os.chmod(filename, original_mode)
+        file_stat = os.stat(filename)
+        # Preserve creation timestamps
+        os.utime(filename, (creation, file_stat.st_mtime))
     return encoding
 
 


Test patch:
diff --git a/spyder/utils/tests/test_encoding.py b/spyder/utils/tests/test_encoding.py
--- a/spyder/utils/tests/test_encoding.py
+++ b/spyder/utils/tests/test_encoding.py
@@ -36,6 +36,22 @@ def test_permissions(tmpdir):
     assert old_mode == new_mode
 
 
+def test_timestamp(tmpdir):
+    """Check that the modification timestamp is preserved."""
+    tmp_file = tmpdir.mkdir("timestamp").join('test_file.txt')
+    tmp_file = to_text_string(tmp_file)
+
+    # Write a file
+    write("Test text", tmp_file)
+    st = os.stat(tmp_file)
+    actual_creation_time = st.st_atime
+
+    # Write the file and check that creation time is preserved.
+    write('New text', tmp_file)
+    creation_time = os.stat(tmp_file).st_atime
+    assert actual_creation_time == creation_time
+
+
 def test_is_text_file(tmpdir):
     p = tmpdir.mkdir("sub").join("random_text.txt")
     p.write("Some random text")