zoukankan      html  css  js  c++  java
  • python3 格式化输出给定时间的下一秒

    # 功能:输入一个时间,格式化输出该时间的下一秒

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Hiuhung Wan
    
    # 功能:输入一个时间,格式化输出该时间的下一秒
    
    def main():
        time_input = input("请输入一个时间格式的字符串")
        if input_check(time_input):
            print(next_sec(time_input))
        else:
            print('输入不符合要求,格式为:xx:xx:xx')
    
    def input_check(time_str:str):
        '''
        对输入作出检查,看是否符合要求
        :param time_str: 输入时间格式的字符串
        :return: 符合要求,就返回True,否则返回False
        '''
        if time_str.count(':') != 2:   #先判断有没有两个冒号
            return False
        elif time_str.replace(":",'').isdigit():
    
            time_temp_list = time_str.split(':')     #分隔成三段
    
            if int(time_temp_list[0]) >= 24:   #大于24小时
                return False
            elif (int(time_temp_list[1]) >= 60) or (int(time_temp_list[2]) >= 60):
                return False
            else:
                return True
        else:
            return False
    
    def next_sec(time_str):
        '''
        格式化输出给定时间的下一秒
        :param time_str: 时间字符串
        :return: 返回给定时间的下一秒
        '''
        time_list = time_str.split(":")
        h = int(time_list[0])   #小时
        m = int(time_list[1])   #分钟
        s = int(time_list[2])   #秒钟
        s += 1
        if s == 60:
            m += 1
            s =0
            if m == 60:
                h += 1
                m = 0
                if h == 24:
                    h = 0
        #转为字符串
        h = str(h)
        m = str(m)
        s = str(s)
        if len(h) == 1:   #没有十位数,补0
            h = '0' + h
        if len(m) == 1:
            m = '0' + m
        if len(s) == 1:
            s = '0' + s
        return (h + ':' + m + ':' + s)
    
    if __name__ == '__main__':
        main()
    

      

    效果:

    C:Python36python.exe D:/Py/1704/day05/下一秒.py
    请输入一个时间格式的字符串23:59:59
    00:00:00
    
    Process finished with exit code 0
    

      

  • 相关阅读:
    Facade
    Adapter
    Bridge
    Factory
    Singleton
    Decorator
    Template Method
    设计模式
    寻找最小的k个数
    java并发编程(4)--线程池的使用
  • 原文地址:https://www.cnblogs.com/hiuhungwan/p/9250548.html
Copyright © 2011-2022 走看看