How to make the date of its last launch when the program starts? You can determine the date using the time module and save it in Notepad, and when you start it, extract the string with the date. Maybe there are other ways?

  • Registry, for example. - Vladimir Martyanov
  • does python have a module for working with the registry? Or is he not needed? - František
  • There is a module, yes - Vladimir Martyanov

2 answers 2

For portability and for easier debugging, you can save time in a human-readable format:

 import datetime as DT print(DT.datetime.now(DT.timezone.utc)) # -> 2018-02-07 07:48:55.388757+00:00 

So that the result does not depend on the current working directory (so that the program can be started from any directory), you should specify the full path to the file with time.

To write user data to a common place, you can appdirs module :

 #!/usr/bin/env python3 import datetime as DT from pathlib import Path import appdirs # $ pip install appdirs path = Path(appdirs.user_data_dir("Название программы", "Автор")) / 'last_run_time.txt' # read time of the last program run if available if path.exists(): last_run_time = DT.datetime.strptime(path.read_text(), '%Y-%m-%d %H:%M:%S.%f+00:00').replace(tzinfo=DT.timezone.utc) print(last_run_time.astimezone()) # display local time else: # first run path.parent.mkdir(parents=True, exist_ok=True) # update time now = DT.datetime.now(DT.timezone.utc) path.write_text(now.isoformat(' ')) 
  • Displays (and records) the time lagging for 3 hours. Why is this happening (I live in Belarus, our time coincides fully with Moscow time)? I removed the DT.timezone.utc parameter in the last but one line. As I understand it, this is an optional tzinfo parameter. The time is recorded correctly, but at the end some numbers are added; and display through print does not work. Gives an error: ValueError: unconverted data: .569772 - Frantisek
  • one
    @ Frantisek: local time can jump back and forth. Store should be time in UTC. If you show this time somewhere (and you want to show local time), then just print(last_run_time.astimezone()) call (except in extreme cases, local time will show). Removing tzinfo should not be from last_run_time . Of course, you can show in any format (it is not tied to the storage format). - jfs
  • I wonder why it "jumps" for several hours ?. You can not make it to the exact time? - František
  • @ Frantisek: the exact time already. Under the word "jumps" I meant the transition from winter time to summer time / flights by plane (when you physically get into another time zone) / launched on another computer (which may be elsewhere). What are your problems with the print(last_run_time.astimezone()) code? - jfs
  • shows time minus three hours. And if you use the class today is normal. now = DT.datetime.today().strftime("%Y-%m-%d %H:%M:%S.%f+00:00") path.write_text(now) It will show the correct user time from another time zone? - František

I think it is more convenient to write the value to the file. Working with the registry will be rather cumbersome, and it may be necessary to run as administrator.

Here is the option to write to the file.

 import os, pickle, time tmp_file = "test.pic" if os.path.isfile(tmp_file): t = pickle.load(open(tmp_file, "rb")) print(t) else: print("Time not found") pickle.dump(str(time.time()), open(tmp_file, "wb"))