Added a new formatTime function to common, and replaced similar functions in other classes

This commit is contained in:
Veronica K. B. Olsen
2020-10-23 20:26:29 +02:00
parent 78a0a595b9
commit 170c2dc19c
4 changed files with 43 additions and 33 deletions
+10
View File
@@ -169,6 +169,16 @@ def formatTimeStamp(theTime, fileSafe=False):
else: else:
return datetime.fromtimestamp(theTime).strftime(nwConst.tStampFmt) return datetime.fromtimestamp(theTime).strftime(nwConst.tStampFmt)
def formatTime(tS):
"""Format the time spent in 00:00:00 format.
"""
if isinstance(tS, int):
if tS >= 86400:
return f"{tS//86400:d}-{tS//3600%24:02d}:{tS%3600//60:02d}:{tS%60:02d}"
else:
return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
return "ERROR"
def splitVersionNumber(vString): def splitVersionNumber(vString):
""" Splits a version string on the form aa.bb.cc into major, minor """ Splits a version string on the form aa.bb.cc into major, minor
and patch, and computes an integer value aabbcc. and patch, and computes an integer value aabbcc.
+2 -4
View File
@@ -35,6 +35,7 @@ from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import qApp, QStatusBar, QLabel, QAbstractButton from PyQt5.QtWidgets import qApp, QStatusBar, QLabel, QAbstractButton
from nw.core import NWSpellCheck from nw.core import NWSpellCheck
from nw.common import formatTime
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -206,10 +207,7 @@ class GuiMainStatus(QStatusBar):
if self.refTime is None: if self.refTime is None:
self.timeText.setText("00:00:00") self.timeText.setText("00:00:00")
else: else:
tS = int(time() - self.refTime) self.timeText.setText(formatTime(round(time() - self.refTime)))
self.timeText.setText(
f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
)
return return
# END Class GuiMainStatus # END Class GuiMainStatus
+11 -27
View File
@@ -39,6 +39,7 @@ from PyQt5.QtWidgets import (
QLabel, QGroupBox, QMenu, QAction, QFileDialog, QSpinBox, QHBoxLayout QLabel, QGroupBox, QMenu, QAction, QFileDialog, QSpinBox, QHBoxLayout
) )
from nw.common import formatTime
from nw.constants import nwConst, nwFiles, nwAlert from nw.constants import nwConst, nwFiles, nwAlert
from nw.gui.custom import QSwitch from nw.gui.custom import QSwitch
@@ -123,11 +124,11 @@ class GuiWritingStats(QDialog):
self.infoForm = QGridLayout(self) self.infoForm = QGridLayout(self)
self.infoBox.setLayout(self.infoForm) self.infoBox.setLayout(self.infoForm)
self.labelTotal = QLabel(self._formatTime(0)) self.labelTotal = QLabel(formatTime(0))
self.labelTotal.setFont(self.theTheme.guiFontFixed) self.labelTotal.setFont(self.theTheme.guiFontFixed)
self.labelTotal.setAlignment(Qt.AlignVCenter | Qt.AlignRight) self.labelTotal.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
self.labelFilter = QLabel(self._formatTime(0)) self.labelFilter = QLabel(formatTime(0))
self.labelFilter.setFont(self.theTheme.guiFontFixed) self.labelFilter.setFont(self.theTheme.guiFontFixed)
self.labelFilter.setAlignment(Qt.AlignVCenter | Qt.AlignRight) self.labelFilter.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
@@ -367,34 +368,26 @@ class GuiWritingStats(QDialog):
elif dataFmt == self.FMT_CSV: elif dataFmt == self.FMT_CSV:
outFile.write( outFile.write(
"\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n" % ( '"Date","Length (sec)","Words Changed","Novel Words","Note Words"\n'
"Date", "Length (sec)", "Words Changed", "Novel Words", "Note Words"
)
) )
for _, sD, tT, wD, wA, wB in self.filterData: for _, sD, tT, wD, wA, wB in self.filterData:
outFile.write( outFile.write(f'"{sD}",{tT:.0f},{wD},{wA},{wB}\n')
"\"%s\",%d,%d,%d,%d\n" % (sD, tT, wD, wA, wB)
)
wSuccess = True wSuccess = True
else: else:
errMsg = "Unknown format" errMsg = "Unknown format"
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e).replace("\n", "<br>")
# Report to user # Report to user
if wSuccess: if wSuccess:
self.theParent.makeAlert( self.theParent.makeAlert(
"%s file successfully written to:<br> %s" % ( f"{textFmt} file successfully written to:<br>{savePath}", nwAlert.INFO
textFmt, savePath
), nwAlert.INFO
) )
else: else:
self.theParent.makeAlert( self.theParent.makeAlert(
"Failed to write %s file. %s" % ( f"Failed to write {textFmt} file.<br>{errMsg}", nwAlert.ERROR
textFmt, errMsg
), nwAlert.ERROR
) )
return True return True
@@ -455,7 +448,7 @@ class GuiWritingStats(QDialog):
return False return False
ttWords = ttNovel + ttNotes ttWords = ttNovel + ttNotes
self.labelTotal.setText(self._formatTime(ttTime)) self.labelTotal.setText(formatTime(round(ttTime)))
self.novelWords.setText(f"{ttNovel:n}") self.novelWords.setText(f"{ttNovel:n}")
self.notesWords.setText(f"{ttNotes:n}") self.notesWords.setText(f"{ttNotes:n}")
self.totalWords.setText(f"{ttWords:n}") self.totalWords.setText(f"{ttWords:n}")
@@ -544,7 +537,7 @@ class GuiWritingStats(QDialog):
newItem = QTreeWidgetItem() newItem = QTreeWidgetItem()
newItem.setText(self.C_TIME, sStart) newItem.setText(self.C_TIME, sStart)
newItem.setText(self.C_LENGTH, self._formatTime(sDiff)) newItem.setText(self.C_LENGTH, formatTime(round(sDiff)))
newItem.setText(self.C_COUNT, f"{nWords:n}") newItem.setText(self.C_COUNT, f"{nWords:n}")
if nWords > 0 and listMax > 0: if nWords > 0 and listMax > 0:
@@ -567,17 +560,8 @@ class GuiWritingStats(QDialog):
self.listBox.addTopLevelItem(newItem) self.listBox.addTopLevelItem(newItem)
self.timeFilter += sDiff self.timeFilter += sDiff
self.labelFilter.setText(self._formatTime(self.timeFilter)) self.labelFilter.setText(formatTime(round(self.timeFilter)))
return True return True
def _formatTime(self, tS):
"""Format the time spent in 00:00:00 format.
"""
tM = int(tS/60)
tH = int(tM/60)
tM = tM - tH*60
tS = tS - tM*60 - tH*3600
return "%02d:%02d:%02d" % (tH, tM, tS)
# END Class GuiWritingStats # END Class GuiWritingStats
+20 -2
View File
@@ -7,7 +7,7 @@ import pytest
from nw.common import ( from nw.common import (
checkString, checkBool, checkInt, colRange, formatInt, transferCase, checkString, checkBool, checkInt, colRange, formatInt, transferCase,
fuzzyTime, checkHandle, formatTimeStamp fuzzyTime, checkHandle, formatTimeStamp, formatTime
) )
from nwtools import cmpList from nwtools import cmpList
@@ -78,11 +78,29 @@ def testColRange():
) )
@pytest.mark.core @pytest.mark.core
def testFormatTime(): def testFormatTimeStamp():
tTime = time.mktime(time.gmtime(0)) tTime = time.mktime(time.gmtime(0))
assert formatTimeStamp(tTime, False) == "1970-01-01 00:00:00" assert formatTimeStamp(tTime, False) == "1970-01-01 00:00:00"
assert formatTimeStamp(tTime, True) == "1970-01-01 00.00.00" assert formatTimeStamp(tTime, True) == "1970-01-01 00.00.00"
@pytest.mark.core
def testFormatTime():
assert formatTime("1") == "ERROR"
assert formatTime(1.0) == "ERROR"
assert formatTime(1) == "00:00:01"
assert formatTime(59) == "00:00:59"
assert formatTime(60) == "00:01:00"
assert formatTime(180) == "00:03:00"
assert formatTime(194) == "00:03:14"
assert formatTime(3540) == "00:59:00"
assert formatTime(3599) == "00:59:59"
assert formatTime(3600) == "01:00:00"
assert formatTime(11640) == "03:14:00"
assert formatTime(11655) == "03:14:15"
assert formatTime(86399) == "23:59:59"
assert formatTime(86400) == "1-00:00:00"
assert formatTime(360000) == "4-04:00:00"
@pytest.mark.core @pytest.mark.core
def testFormatInt(): def testFormatInt():
assert formatInt(1000) == "1000" assert formatInt(1000) == "1000"