zoukankan      html  css  js  c++  java
  • 带小数点时间分钟转换

     

    为什么要转换时间

    由于业务所需,需要计算客户连接服务时长,一般情况连接服务时长都是小时的整数,比如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_())

    运行效果

  • 相关阅读:
    【HTML5 绘图与动画】使用canvas
    【H5新增元素和文档结构】新的全局属性 1. contentEditable 可编辑内容 2. contextmenu 快捷菜单 3. data 自定义属性 4. draggable 可拖动 5. dropzone 拖动数据 6. hidden 隐藏 7. spellcheck 语法检查 8. translate 可翻译
    【H5新增元素和文档结构】完善旧元素 1. a 超链接 2. ol 有序列表 3. dl 定义列表 4. cite 引用文本 5. small 小号字体 6. iframe 浮动框架 7. script 脚本
    【H5新增元素和文档结构】新的语义信息 1. address 2. time 3. figure 跟 figcaption 4. details 和 summary 5. mark 6. progress 7. meter 8. dialog 9.bdi 10. wbr 11. ruby、rt、rp 12. command
    【H5新增元素跟文档结构】新的文档结构 1. article 文章块 2. section 区块 3. nav 导航条 4. aside 辅助栏 5. main 主要区域 6. header 标题栏 7. hgroup 标题组 8. footer 页脚栏
    5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数
    【华为云技术分享】鲲鹏弹性云服务器GCC交叉编译环境搭建指南
    【华为云技术分享】7 分钟全面了解位运算
    【华为云技术分享】Linux内核编程环境 (1)
    【华为云技术分享】华为云MySQL 8.0正式商用,全新增强版开源利器强势来袭
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4608011.html
Copyright © 2011-2022 走看看