Aprendizaje de Python (3) hora y fecha y hora MD5

# coding=utf-8
import time
if __name__ == '__main__':
#time
    time.clock()# 返回自进程开始或之后的CPU时间
    time.ctime()# 依据参数secs秒数,返回自1970开始后计算的日期,参数为空时返回当前日期时间
    print("time.ctime "+time.ctime(time.time())) #Tue Jun 25 15:01:37 2019
    time.gmtime()# 和ctime()区别在于返回值的格式不同,返回值的格式为struct_time  可以继续获取年 月
    print(time.gmtime(time.time()))# time.struct_time(tm_year=2019, tm_mon=6, tm_mday=25, tm_hour=7, tm_min=5, tm_sec=2, tm_wday=1, tm_yday=176, tm_isdst=0)
    print(time.gmtime(time.time()).tm_year)# 2019
    time.localtime()# 和gmtime()方法功能基本相同,但是gmtime()是转换为标准的UTC时间
    print(time.localtime())# time.struct_time(tm_year=2019, tm_mon=6, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=46, tm_wday=1, tm_yday=176, tm_isdst=0)

    # time.mktime()# 将当地时间的时间元组转换为自纪元以来的秒数,也就是时间戳的值
    # time.sleep()# 延迟指定的secs秒数时间,就是让代码在此处休眠指定的秒数时间,然后再执行后续代码
    time.time() # 时间戳
    #  返回指定t的格式format的字符串类型时间,例如:time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) = '2019-06-25 15:12:48'
    item_str=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    print(item_str)
    # 将字符串类型的时间转换成 struct_time类型的时间对象
    time_lsit=time.strptime('20190825', "%Y%m%d")
    print(time_lsit.tm_year)# 2019
    print(time_lsit)# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=237, tm_isdst=-1)
    # 还有很多 方法 可以继续查看源码
    
#datetime  更友好 更方便
    # tzinfo 类  时区

# date 类 日期类 天
    temp = datetime.date.fromtimestamp(time.time())  # 根据跟定的时间戳,返回一个date对象
    print("fromtimestamp %s" % temp)  # fromtimestamp 2019-06-25
    datetime.date.today()# 返回一个表示当前日期的date对象
    print(datetime.date.today()) # 2019-06-25
    #测试
    a = datetime.date(2017, 3, 22)
    b = a.toordinal()
    datetime.date.fromordinal(b) # 将Gregorian日历时间转换为date对象

    # datetime.date.year
    # datetime.date.month
    # datetime.date.day
	# date 对象 年 月 日 是必填参数
    date=datetime.date(2019,6,25)
    date.ctime()# 返回当前date对象的ctime()样式字符串,例如今天是2018-07-21 星期六,则返回'Sat Jul 21 00:00:00 2018'
    print(date.ctime())# Sun Jun 23 00:00:00 2019
    date.strftime('%Y-%m-%d') # 根据字符串的类型 将时间对象改写,返回字符串
    print(date.strftime('%Y-%m-%d'))# 2019-06-23
    date.isoformat()
    date.timetuple()
    date.toordinal()

    datetime.date.isoformat(datetime.date.today())# 返回符合ISO 标准的日期字符串
    print(datetime.date.isoformat(datetime.date.today()))# 2019-06-25
    datetime.date.timetuple(datetime.date.today())# 返回struct_time 类型的数据数组
    print(datetime.date.timetuple(datetime.date.today()))# time.struct_time(tm_year=2019, tm_mon=6, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=176, tm_isdst=-1)
    datetime.date.toordinal(datetime.date.today())# 返回公元公历开始到现在的天数。公元1年1月1日为1
    print(datetime.date.toordinal(datetime.date.today()))# 737235

    yestoday=date.replace(2019,6,24)# 是新创建的日期对象 原来的不受影响
    yestoday.isoweekday() #返回指定日期所在的星期数 周一是1 周二是2...
    yestoday.weekday()# 与isoweekday(...)相似的还有一个weekday(...)方法,只不过是weekday(...)方法返回的周一为 0, 周日为 6
    print(yestoday.isoweekday())# 1
    print(yestoday.weekday())# 0
    yestoday.isocalendar()# 返回多少年 第几周,星期几
    print(yestoday.isocalendar())# (2019, 26, 1)
# time 类 时间秒
    # time_ins=datetime.time(16,23,30,0)
    # time_ins.replace()
    # time_ins.isoformat()
    # time_ins.strftime()
    # time_ins.dst()
    #time_ins.replace() 和datetime.date.replace()是一样的

# timedelta(SupportsAbs[timedelta])类 时间差  时间之间的加减运算
    """
    @property
    def days(self) -> int: ...
    @property
    def seconds(self) -> int: ...
    @property
    def microseconds(self) -> int: ...

    def total_seconds(self) -> float: ...
    def __add__(self, other: timedelta) -> timedelta: ...
    def __radd__(self, other: timedelta) -> timedelta: ...
    def __sub__(self, other: timedelta) -> timedelta: ...
    def __rsub__(self, other: timedelta) -> timedelta: ...
    def __neg__(self) -> timedelta: ...
    def __pos__(self) -> timedelta: ...
    def __abs__(self) -> timedelta: ...
    def __mul__(self, other: float) -> timedelta: ...
    def __rmul__(self, other: float) -> timedelta: ...
    @overload
    def __floordiv__(self, other: timedelta) -> int: ...
    @overload
    def __floordiv__(self, other: int) -> timedelta: ...
    if sys.version_info >= (3,):
        @overload
        def __truediv__(self, other: timedelta) -> float: ...
        @overload
        def __truediv__(self, other: float) -> timedelta: ...
        def __mod__(self, other: timedelta) -> timedelta: ...
        def __divmod__(self, other: timedelta) -> Tuple[int, timedelta]: ...
    else:
        @overload
        def __div__(self, other: timedelta) -> float: ...
        @overload
        def __div__(self, other: float) -> timedelta: ...
    def __le__(self, other: timedelta) -> bool: ...
    def __lt__(self, other: timedelta) -> bool: ...
    def __ge__(self, other: timedelta) -> bool: ...
    def __gt__(self, other: timedelta) -> bool: ...
    def __hash__(self) -> int: ...

    """
# datetime 类 日期和时间
    datetime.datetime.now()# 当前日期和时间
    print(datetime.datetime.now())# 2019-06-25 16:39:27.923400

    datetime.datetime.combine(date,time_lsit)# 返回当前系统UTC时间戳对应的日期时间
    '''
    class datetime(date):
    min: ClassVar[datetime]
    max: ClassVar[datetime]
    resolution: ClassVar[timedelta]

    if sys.version_info >= (3, 6):
        def __init__(self, year: int, month: int, day: int, hour: int = ...,
                     minute: int = ..., second: int = ..., microsecond: int = ...,
                     tzinfo: Optional[_tzinfo] = ..., *, fold: int = ...) -> None: ...
    else:
        def __init__(self, year: int, month: int, day: int, hour: int = ...,
                     minute: int = ..., second: int = ..., microsecond: int = ...,
                     tzinfo: Optional[_tzinfo] = ...) -> None: ...

    @property
    def year(self) -> int: ...
    @property
    def month(self) -> int: ...
    @property
    def day(self) -> int: ...
    @property
    def hour(self) -> int: ...
    @property
    def minute(self) -> int: ...
    @property
    def second(self) -> int: ...
    @property
    def microsecond(self) -> int: ...
    @property
    def tzinfo(self) -> Optional[_tzinfo]: ...
    if sys.version_info >= (3, 6):
        @property
        def fold(self) -> int: ...

    @classmethod
    def fromtimestamp(cls, t: float, tz: Optional[_tzinfo] = ...) -> datetime: ...
    @classmethod
    def utcfromtimestamp(cls, t: float) -> datetime: ...
    @classmethod
    def today(cls) -> datetime: ...
    @classmethod
    def fromordinal(cls, n: int) -> datetime: ...
    @classmethod
    def now(cls, tz: Optional[_tzinfo] = ...) -> datetime: ...
    @classmethod
    def utcnow(cls) -> datetime: ...
    if sys.version_info >= (3, 6):
        @classmethod
        def combine(cls, date: date, time: time, tzinfo: Optional[_tzinfo] = ...) -> datetime: ...
    else:
        @classmethod
        def combine(cls, date: date, time: time) -> datetime: ...
    if sys.version_info >= (3, 7):
        @classmethod
        def fromisoformat(cls, date_string: str) -> datetime: ...
    def strftime(self, fmt: _Text) -> str: ...
    if sys.version_info >= (3,):
        def __format__(self, fmt: str) -> str: ...
    else:
        def __format__(self, fmt: AnyStr) -> AnyStr: ...
    def toordinal(self) -> int: ...
    def timetuple(self) -> struct_time: ...
    if sys.version_info >= (3, 3):
        def timestamp(self) -> float: ...
    def utctimetuple(self) -> struct_time: ...
    def date(self) -> _date: ...
    def time(self) -> _time: ...
    def timetz(self) -> _time: ...
    if sys.version_info >= (3, 6):
        def replace(self, year: int = ..., month: int = ..., day: int = ..., hour: int = ...,
                    minute: int = ..., second: int = ..., microsecond: int = ..., tzinfo:
                    Optional[_tzinfo] = ..., *, fold: int = ...) -> datetime: ...
    else:
        def replace(self, year: int = ..., month: int = ..., day: int = ..., hour: int = ...,
                    minute: int = ..., second: int = ..., microsecond: int = ..., tzinfo:
                    Optional[_tzinfo] = ...) -> datetime: ...
    if sys.version_info >= (3, 3):
        def astimezone(self, tz: Optional[_tzinfo] = ...) -> datetime: ...
    else:
        def astimezone(self, tz: _tzinfo) -> datetime: ...
    def ctime(self) -> str: ...
    if sys.version_info >= (3, 6):
        def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...
    else:
        def isoformat(self, sep: str = ...) -> str: ...
    @classmethod
    def strptime(cls, date_string: _Text, format: _Text) -> datetime: ...
    def utcoffset(self) -> Optional[timedelta]: ...
    def tzname(self) -> Optional[str]: ...
    def dst(self) -> Optional[int]: ...
    def __le__(self, other: datetime) -> bool: ...  # type: ignore
    def __lt__(self, other: datetime) -> bool: ...  # type: ignore
    def __ge__(self, other: datetime) -> bool: ...  # type: ignore
    def __gt__(self, other: datetime) -> bool: ...  # type: ignore
    def __add__(self, other: timedelta) -> datetime: ...
    @overload  # type: ignore
    def __sub__(self, other: datetime) -> timedelta: ...  # type: ignore
    @overload  # type: ignore
    def __sub__(self, other: timedelta) -> datetime: ...  # type: ignore
    def __hash__(self) -> int: ...
    def weekday(self) -> int: ...
    def isoweekday(self) -> int: ...
    def isocalendar(self) -> Tuple[int, int, int]: ...

    '''
    # 很多都是重复方法 就不写了 贴出源码

Cómo escribir la hora formateada
inserte la descripción de la imagen aquí

2
cadenas MD5 y valores MD5 de archivos

import hashlib
import os

md5 = hashlib.md5()
md5.update("sss".encode('utf-8'))
print(md5.hexdigest())
# 文件
def GetFileMd5(filename):
    if not os.path.isfile(filename):
        return
    myhash = hashlib.md5()
    f = open(filename,'rb')
    while True:
        b = f.read(8096)
        if not b :
            break
        myhash.update(b)
    f.close()
    return myhash.hexdigest()

Supongo que te gusta

Origin blog.csdn.net/m_cainiaokuaifei/article/details/93621607
Recomendado
Clasificación