day14

day14

类方法与静态方法

  • 静态方法和显示名称,对于处理一类本地数据更好的解决方案

  • 类方法更适合处理层级之间每一类的不同数据

"""
class Student:
    count=0
    def __init__(self,name,age):
        self.name=name
        self.age=age
        Student.count+=1#计数总的实例化次数
        # self.count+=1
stu1=Student("Alex",18)
stu2=Student("Jerry",18)
print(Student.count)
"""
"""
class Student():
    count=0
    def __init__(self):
        Student.count+=1

    @staticmethod #只能使用类名调用,不能实例化调用,能被继承和重写
    def print_count():
        print(Student.count)

A=Student()
Student.print_count()
B=Student()
Student.print_count()

class Sub(Student):
    def print_count():
        print(Sub.count,Student.count)
        Student.print_count()

a=Sub()
b=Sub()
Sub.print_count()

"""
"""
class Student:
    count=0
    def __init__(self):
        Student.count+=1

    @classmethod#在类方法中参数为cls,可被继承、重写
    def print_count(cls):
        print(cls.count)

a=Student()
b=Student()
c=Student()
Student.print_count()

class sub(Student):
    def print_count(cls):
        Student.print_count()

s=sub()
s.print_count()
"""

time模块

import time
a = time.time()
print(a, type(a))  # 1597296865.299882 <class 'float'>
# 格式化时间字符串
print(time.strftime("%Y-%m-%d %X")) 
# 2020-08-13 13:38:15
# 格式化时间->年月日时分秒,一年中的第几周,一年中的第几天,夏令时

# 获取本地时间
print(time.localtime())
# time.struct_time(tm_year=2020, tm_mon=8, tm_mday=13, tm_hour=13,
#                  tm_min=41, tm_sec=32, tm_wday=3, tm_yday=226, tm_isdst=0)
# UTC时间
print(time.gmtime())
# time.struct_time(tm_year=2020, tm_mon=8, tm_mday=13,
# tm_hour=5, tm_min=42, tm_sec=51, tm_wday=3, tm_yday=226, tm_isdst=0)
print(time.strftime("%c-%I-%p"))

格式化时间字符串

符号描述
%YYear with century as a decimal number.
%mMonth as a decimal number [01,12].
%dDay of the month as a decimal number [01,31].
%HHour (24-hour clock) as a decimal number [00,23].
%MMinute as a decimal number [00,59].
%SSecond as a decimal number [00,61].
%zTime zone offset from UTC.
%aLocale’s abbreviated weekday name.
%ALocale’s full weekday name.
%bLocale’s abbreviated month name.
%BLocale’s full month name.
%cLocale’s appropriate date and time representation.
%IHour (12-hour clock) as a decimal number [01,12].
%pLocale’s equivalent of either AM or PM.

datetime模块

import time,datetime

#时间
print(datetime.datetime.now())
#2020-08-13 13:56:02.922100
#日期
print(datetime.date.fromtimestamp(time.time()))
#2020-08-13
#当前时间加三天,负数为减三天
print(datetime.datetime.now()+datetime.timedelta(3))
#2020-08-16 13:59:44.243535
#当前时间加三小时
print(datetime.datetime.now()+datetime.timedelta(hours=3))
#2020-08-13 17:01:14.458462
#时间替换
c_time=datetime.datetime.now()
print(c_time.replace(minute=30,day=29))

# 2020-08-29 14:30:57.088441
  • datetime与time相似,datetime功能更强,datetime中主要类:date,time,datetime,timedelta,tzinfo

1.date类

"""
datetime.date(year,month,day)
静态方法及字段
date对象所能表达的最大值
date.max
date对象所能表达的最小值
date.min
date对象表示日期的最小单位
date.resolution
返回一个表示本地日期的date对象
date.today()
根据给定的时间戳,返回一个date对象
date.fromtimestamp(timestamp)
"""

实例

from datetime import *
import time
print(date.max,date.min)
# 9999-12-31 0001-01-01
print(date.today())
# 2020-08-13
print(date.fromtimestamp(time.time()))
# 2020-08-13
"""
#方法和属性
from datetime import *
#创建date对象
d1=date(2020,10,10)
a=d1.year
b=d1.day
#生成一个新的日期对象
#d1.replace(year,month,day)
#返回日期对应点的time.struct_time对象
d1.timetuple()
#返回weekday,周一返回0,周日返回6
d1.weekday()
#返回weekday,周一返回1,周日返回7
d1.isoweekday()
#返回元组格式(year,month,day)
print(d1.isocalendar())
# (2020, 41, 6)
#返回字符串
print(d1.isoformat())
#2020-10-10
#与time模块的format方法相似
print(d1.strftime("%Y-%m-%d %X"))
#2020-10-10 00:00:00
"""

2.time类

#datetime.time(hour,minute,second)
#time类的最大最小时间
# time.min
# time.max
#时间的最小单位,1微秒
# datetime.time.replace()
"""
t2=datetime.time(15,0,0,0)
print(t2.hour)
print(t2.tzinfo)#时区
#创建时间对象
t1=t2.replace(hour=22)
print(t1)#22:00:00"""
"""#datetime类
#datetime(year,month,day,...........)
print(datetime.datetime.max,datetime.datetime.min)
# 9999-12-31 23:59:59.999999 0001-01-01 00:00:00
print(datetime.datetime.resolution)
print(datetime.datetime.today())
print(datetime.datetime.now())
print(datetime.datetime.fromtimestamp(time.time()))
# 0:00:00.000001
# 2020-08-13 15:19:22.942746
# 2020-08-13 15:19:22.942745
# 2020-08-13 15:19:22.942745
"""

3.timedelta类

"""
import datetime as dt

dt1=dt.datetime.now()
dt2=dt1+dt.timedelta(days=1)
print(dt2)
print(dt2.hour)
#2020-08-14 15:27:25.228791"""

4.tzinfo类

from datetime import *

tz=tzinfo
"""
tzinfo 是关于时区信息的类,是一个抽象类,不能直接进行实例化
"""
class UTC(tzinfo):
    def __init__(self,offset=0):
        self.offset=offset

    def utcoffset(self, dt):
        return timedelta(hours=self.offset)

    def tzname(self, dt):
        return 'UTC:%s'%self.offset

    def dst(self, dt):
        return timedelta(hours=self.offset)


bj=datetime(2020,8,13,0,1,0,tzinfo=UTC(8))

print(bj)
rb=datetime(2020,8,13,0,1,0,tzinfo=UTC(9))
print(rb)
#bj时间转为rb时间
print(bj.astimezone(UTC(9)))
# 2020-08-13 01:01:00+09:00

self.offset

def dst(self, dt):
    return timedelta(hours=self.offset)

bj=datetime(2020,8,13,0,1,0,tzinfo=UTC(8))

print(bj)
rb=datetime(2020,8,13,0,1,0,tzinfo=UTC(9))
print(rb)
#bj时间转为rb时间
print(bj.astimezone(UTC(9)))

2020-08-13 01:01:00+09:00


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值