首页 > 解决方案 > 有人可以解释这个 python 代码的作用吗?

问题描述

我需要在 PHP 中创建这段代码,但我不知道 python,也不知道这段代码的作用。

import argparse
import codecs
import math
import os
import re


def leading_zeros(value, digits=2):
    value = "000000" + str(value)
    return value[-digits:]


def convert_time(raw_time):
    if int(raw_time) == 0:
        return "{}:{}:{},{}".format(0, 0, 0, 0)

    ms = '000'
    if len(raw_time) > 4:
        ms = leading_zeros(int(raw_time[:-4]) % 1000, 3)
    time_in_seconds = int(raw_time[:-7]) if len(raw_time) > 7 else 0
    second = leading_zeros(time_in_seconds % 60)
    minute = leading_zeros(int(math.floor(time_in_seconds / 60)) % 60)
    hour = leading_zeros(int(math.floor(time_in_seconds / 3600)))
    return "{}:{}:{},{}".format(hour, minute, second, ms)

例如,它将 926759167 转换为 00:01:32,675,但我不知道如何。

标签: python

解决方案


它将自时间码 00:00:00,000 以来的微秒数转换为小时:分钟:秒,毫秒。毫秒被截断,而不是四舍五入。


推荐阅读