文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python时间处理

python时间处理

时间:2010-06-02  来源:hkebao

#-*-coding:utf-8-*-
"""
#================统计WIN服务器性能脚本=======================
作者:[email protected]
时间:2010-04-27
#================统计WIN服务器性能脚本=======================
"""
from time import strftime, localtime
from datetime import timedelta, date
import calendar
  
year = strftime("%Y",localtime())
mon = strftime("%m",localtime())
day = strftime("%d",localtime())
hour = strftime("%H",localtime())
min = strftime("%M",localtime())
sec = strftime("%S",localtime())

def today():
     '''
     get today,date format="YYYY-MM-DD"
     '''
     return date.today()
  
def todaystr():
     '''
     get date string
     date format="YYYYMMDD"
     '''
     return year+mon+day
  
def datetime():
     '''
     get datetime,format="YYYY-MM-DD HH:MM:SS"
     '''
     return strftime("%Y-%m-%d %H:%M:%S",localtime())
  
def datetimestr():
     '''
     get datetime string
     date format="YYYYMMDDHHMMSS"
     '''
     return year+mon+day+hour+min+sec
  
def getdayofday(n=0):
     '''
     if n>=0,date is larger than today
     if n<0,date is less than today
     date format = "YYYY-MM-DD"
     '''
     if(n<0):
         n = abs(n)
         return date.today()-timedelta(days=n)
     else:
         return date.today()+timedelta(days=n)
  
def getdaysofmonth(year,mon):
     '''
     get days of month
     '''
     return calendar.monthrange(year, mon)[1]
  
def getfirstdayofmonth(year,mon):
     '''
     get the first day of month
     date format = "YYYY-MM-DD"
     '''
     days="01"
     if(int(mon)<10):
         mon = "0"+str(int(mon))
     arr = (year,mon,days)
     return "-".join("%s" %i for i in arr)
  
def getlastdayofmonth(year,mon):
     '''
     get the last day of month
     date format = "YYYY-MM-DD"
     '''
     days=calendar.monthrange(year, mon)[1]
     mon = addzero(mon)
     arr = (year,mon,days)
     return "-".join("%s" %i for i in arr)
  
def get_firstday_month(n=0):
     '''
     get the first day of month from today
     n is how many months
     '''
     (y,m,d) = getyearandmonth(n)
     d = "01"
     arr = (y,m,d)
     return "-".join("%s" %i for i in arr)
  
def get_lastday_month(n=0):
     '''
     get the last day of month from today
     n is how many months
     '''
     return "-".join("%s" %i for i in getyearandmonth(n))
   
def get_today_month(n=0):
     '''
     get last or next month's today
     n is how many months
     date format = "YYYY-MM-DD"
     '''
     (y,m,d) = getyearandmonth(n)
     arr=(y,m,d)
     if(int(day)<int(d)):
         arr = (y,m,day)
     return "-".join("%s" %i for i in arr)
  
def getyearandmonth(n=0):
     '''
     get the year,month,days from today
     befor or after n months
     '''
     thisyear = int(year)
     thismon = int(mon)
     totalmon = thismon+n
     if(n>=0):
         if(totalmon<=12):
             days = str(getdaysofmonth(thisyear,totalmon))
             totalmon = addzero(totalmon)
             return (year,totalmon,days)
         else:
             i = totalmon/12
             j = totalmon%12
             if(j==0):
                 i-=1
                 j=12
             thisyear += i
             days = str(getdaysofmonth(thisyear,j))
             j = addzero(j)
             return (str(thisyear),str(j),days)
     else:
         if((totalmon>0) and (totalmon<12)):
             days = str(getdaysofmonth(thisyear,totalmon))
             totalmon = addzero(totalmon)
             return (year,totalmon,days)
         else:
             i = totalmon/12
             j = totalmon%12
             if(j==0):
                 i-=1
                 j=12
             thisyear +=i
             days = str(getdaysofmonth(thisyear,j))
             j = addzero(j)
             return (str(thisyear),str(j),days)
  
def addzero(n):
     '''
     add 0 before 0-9
     return 01-09
     ''


相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载