Selaa lähdekoodia

Time and date problems about entry class

Zhilong Li 5 vuotta sitten
vanhempi
commit
0df15c30b4
4 muutettua tiedostoa jossa 87 lisäystä ja 14 poistoa
  1. 49 13
      Jt.py
  2. 1 1
      test_coord.py
  3. 28 0
      test_entry.py
  4. 9 0
      test_interface.py

+ 49 - 13
Jt.py

@@ -1,15 +1,17 @@
-from hashlib import md5
-from os import listdir
+import datetime
 import pickle
-import sys
 import random
+import sys
 import time
+from hashlib import md5
+from os import listdir
 
 
 class Base:
     def __init__(self):
-        self.create_time = time.localtime()
-        self.str_time = time.strftime("%Y%m%d%H%M%S", self.create_time)
+        self.create_time = time.time()
+        self.create_date : datetime.datetime = datetime.datetime.utcfromtimestamp(self.create_time)
+        self.str_time = time.strftime("%Y%m%d%H%M%S", time.gmtime(self.create_time))
         self.__gene_id()
 
     def __gene_id(self):
@@ -28,8 +30,9 @@ class Entry(Base):
 
         self.content: str = "在这里输入事项内容"
         self.set_priority()
-        self.set_deadline()
 
+        self.set_start_date()
+        self.set_deadline(self.create_date + datetime.timedelta(days = 7))
         self.sub_entries_dict: dict = {}
 
     def set_priority(self, priority: float = 0):
@@ -41,16 +44,47 @@ class Entry(Base):
             priority = 10.0
         self.priority: float = priority
 
-    def set_deadline(self, deadline_date: time.struct_time = time.localtime(time.time()+time.mktime(time.strptime("7", "%d")))):
-        if deadline_date < time.localtime():
+    def set_deadline(self, deadline_date: datetime.datetime):
+        """This function will set the entry's deadline date
+
+        Args:
+            deadline_date (float): The deadline date must be datetime structure.
+
+        Returns:
+            bool : If successfully set the deadline will return 0, else 1.
+        """
+        if deadline_date < self.start_date:
             print("\033[1;31mERROR: Let it RIP if it's dead already\033[0m")
             return 1
-        self.deadline = deadline_date
-        return 0
+        else:
+            self.deadline = deadline_date
+            return 0
+
+    def set_start_date(self, start_date : datetime.datetime = datetime.datetime(1970,1,1,0,0,0)):
+        if start_date == datetime.datetime(1970,1,1,0,0,0):
+            self.start_date = self.create_date
+        else:
+            self.start_date = start_date
+
 
     def set_content(self, content: str):
         self.content = content
 
+    def timestamp_converter(self, time_in):
+        """This function can convert timestamp to date string and do reversely.
+
+        Args:
+            time_in (float/ str): Timestamp as float or date string like "YYYY/mm/dd HH:MM".
+
+        Returns:
+            timestamp or date string.
+        """
+        try:
+            _time_in = float(time_in)
+            return datetime.datetime.utcfromtimestamp(_time_in)
+        except ValueError:
+            return time.mktime(time.strptime(time_in, "%Y/%m/%d %H:%M"))
+
 
 class Coordinate(Base):
     def __init__(self):
@@ -64,12 +98,12 @@ class Coordinate(Base):
         print(self.entries_dict)
 
     def add_entry(self, entry_in: Entry):
-        self.entries_dict[entry_in.entry_id] = entry_in
+        self.entries_dict[entry_in.id] = entry_in
         self.entries_num += 1
 
     def del_entry(self, entry_in: Entry):
         try:
-            del self.entries_dict[entry_in.entry_id]
+            del self.entries_dict[entry_in.id]
             self.entries_num -= 1
         except KeyError:
             print("\033[1;31mERROR: Entry doesn't exist\033[0m")
@@ -156,7 +190,9 @@ class Interface:
 
     def __new_entry(self, arg: list):
         e = Entry(arg[0])
-        e.content = sys.stdin.readlines()
+        print("Input the content of this entry, use \"Ctrl+D\" to end.\n")
+        e.content = "\n".join(sys.stdin.readlines())
+        print(e.content)
 
     def __new_coord(self, arg: list):
         c = Coordinate()

+ 1 - 1
test_coord.py

@@ -15,6 +15,6 @@ def test_save_and_load():
     with open("coord/" + c.name + ".crd", "rb") as f:
         cc = pickle.load(f)
 
-    os.remove("coord/testCoord.crd")
+    # os.remove("coord/testCoord.crd")
     assert cc.name == "testCoord"
 

+ 28 - 0
test_entry.py

@@ -0,0 +1,28 @@
+import time
+import datetime
+from Jt import Entry
+
+
+def test_priority():
+    e = Entry()
+    p_list = [1, 2, 3, -19, 3.3, 88, 120, 63452, -7876.87, 897.822]
+    for p in p_list:
+        e.set_priority(p)
+        if p > 10:
+            p = 10
+        elif p < -10:
+            p = -10
+        assert e.priority == p
+
+
+def test_time_conv():
+    e = Entry()
+    t = time.time()
+    d = e.timestamp_converter(t)
+    assert d == datetime.datetime.utcfromtimestamp(t)
+    date_list = ["2020/07/8 21:8", "1970/1/01 0:0", "2032/12/08 23:00"]
+    for date in date_list:
+        ts = e.timestamp_converter(date)
+        assert isinstance(ts, float)
+        groud_truth = time.mktime(time.strptime(date, "%Y/%m/%d %H:%M"))
+        assert ts == groud_truth

+ 9 - 0
test_interface.py

@@ -0,0 +1,9 @@
+from Jt import Interface
+import pytest
+
+@pytest.mark.xfail
+def test_private():
+    i = Interface()
+    i.__open_coord("coord/testCoord.crd")
+    assert hasattr(i,"now_coord")
+