為什么要轉換時間
由於業務所需,需要計算客戶連接服務時長,一般情況連接服務時長都是小時的整數,比如1小時、3小時、8小時。但有的客戶返回連接時長有分鍾,比如A客戶返回連接時長2小時30分鍾,B客戶返回連接時長為4小時45分鍾。
做報表與客戶溝通的客服就傻了,因為系統錄入的時候是10進制的,(2小時30分鍾就對應2.5小時, 4小時45分鍾對應4.75小時)遇到小時的二分之一(半個小時)、四分之一(一刻鍾)還會算,但她們遇到小時的非整除數有的人就不會計算了。
有一次一個客服問我,6小時18分鍾對應小數點的小時是多少呢?
好吧,我給她們講,用分鍾除以60就可了。但有的人還是轉不過彎,所以才有了時間轉小數點的工具。
很簡單的邏輯
用2小時25分鍾作為說明數據
小時整數不用轉換,整數為2
因為1小時=60分鍾,所以25/60=0.4166666666666667
加起來就是2+0.417=2.417小時
反過來,同樣的道理
2.32小時等於多少小時多少分鍾呢?
2.32小時=2小時+0.32小時
2為小時整數
因為1小時=60分鍾, 0.32小時*60=19.20分鍾
加起來就是2小時19分鍾
代碼的實現
10進制與60進制的原理明白了,那代碼就是English了
我們用Qt Designer 設計出界面
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>decToTime</class> <widget class="QDialog" name="decToTime"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>178</height> </rect> </property> <property name="minimumSize"> <size> <width>400</width> <height>0</height> </size> </property> <property name="windowTitle"> <string>Dialog</string> </property> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QGroupBox" name="groupBox"> <property name="title"> <string>分鍾小數點轉換</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QComboBox" name="comboBox"> <item> <property name="text"> <string>小數點轉換為分鍾</string> </property> </item> <item> <property name="text"> <string>分鍾轉換為小數點</string> </property> </item> </widget> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> <widget class="QLineEdit" name="input"/> </item> <item> <widget class="QPushButton" name="button"> <property name="text"> <string>轉換</string> </property> </widget> </item> </layout> </item> <item> <widget class="QTextEdit" name="text"/> </item> </layout> </widget> </item> </layout> </widget> <resources> <include location="qrc.qrc"/> </resources> <connections/> </ui>
用UIC轉換為Py文件
python.exe -m PyQt5.uic.pyuic -o decimalToTime.py -x decimalToTime.ui
mian.py :
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets from decimalToTime import Ui_decToTime import sys import icoqrc import re class MainFrom(QtWidgets.QDialog): def __init__(self): super(MainFrom, self).__init__() self.Ui = Ui_decToTime() self.Ui.setupUi(self) self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # 設置總是在最前 self.setWindowIcon(QtGui.QIcon(':/ico/qq.ico')) self.setWindowFlags(QtCore.Qt.Dialog) # 設置類型為Dialog類型 self.Ui.button.clicked.connect(self.toTime) # 點擊轉換按鈕 self.Ui.input.returnPressed.connect(self.toTime) # 回車事件 ''' 判斷是否為數字 ''' def is_number(self, s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False ''' '開始轉換 ''' def toTime(self): # 判斷input是否有值 # self.Ui.input.setText('2') pre = self.Ui.input.text() if self.is_number(pre) == False: OK = QtWidgets.QMessageBox.warning(self, (u'提示'), (u'請輸入正確的數字!'), QtWidgets.QMessageBox.Yes) return False result = self.setText() self.Ui.text.setText(result) ''' 設置時間 ''' def setText(self): cindex = self.Ui.comboBox.currentIndex() # 當前comboBox索引Index值 索引從0開始 cindex = self.Ui.comboBox.currentText() # 當前comboBox文本值 r = re.compile(r'(\d+)\.(\d*)') m = r.match(self.Ui.input.text()) if cindex == '小數點轉換為分鍾': try: minute = str(int(float(str("0." + m.group(2))) * 60)) + "分鍾" res = str(self.Ui.input.text()) + "小時等於<b style='color:red'>" + str( m.group(1)) + "</b>小時<b style='color:red'>" + minute + "</b>" except Exception as e: res = str(self.Ui.input.text()) + "小時等於" + str(self.Ui.input.text()) + "小時" if cindex == '分鍾轉換為小數點': try: point = int((float(m.group(2)) / 60) * 100) t = str(m.group(1)) + "." + str(point) res = str(m.group(1)) + "小時" + str(m.group(2)) + "分鍾等於<b style='color:red'>" + t + "</b>小時" except Exception as e: res = str(self.Ui.input.text()) + "小時等於" + str(self.Ui.input.text()) + "小時" return res if __name__ == '__main__': App = QtWidgets.QApplication(sys.argv) MainApp = MainFrom() MainApp.show() sys.exit(App.exec_())
運行效果