码迷,mamicode.com
首页 > 其他好文 > 详细

字节转换跟时间戳转换

时间:2019-04-19 11:55:37      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:coding   mkt   group   for   函数   常用   its   ftime   字典   

#coding=utf-8
#设计转换函数

import re,os
base=2**10#基数1024
units={0:‘b‘,1:‘kb‘,2:‘mb‘,3:‘gb‘,4:‘tb‘}#常用字节大小单位字典
count=0

#转换时间
import time

#转换为时间戳
def tr_1(date_str):
date = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")#转换成tuple日期
stamp = int(time.mktime(date))
return stamp

#转换为str格式的时间
def tr_2(stamp):
date= time.localtime(stamp)#转换成tuple日期
return time.strftime("%Y-%m-%d %H:%M:%S", date)

def transfer_h(value):
def transfer(value1):
global count
v=value1 // base
if v>0:
count+=1
transfer(v)
else:pass
value1=int(value.strip("bB"))#将单位字符去掉转换成int
transfer(value1)#确认转换单位
msg="{} {}".format(str(float(value1)/pow(base,count)),units[count])#输出转换单位值
return msg

def transfer_b(value):
unit=re.search(r‘\D+‘,value).group(0)#提取字节大小单位
d=int(value.strip(unit))#提取数值
for k,v in units.items():
if unit.lower() in v:
return d * pow(base,k)#转换成单位为b的大小

字节转换跟时间戳转换

标签:coding   mkt   group   for   函数   常用   its   ftime   字典   

原文地址:https://blog.51cto.com/12119115/2381194

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!