<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大漠駝鈴

    置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
    Java,Php,Shell,Python,服務(wù)器運維,大數(shù)據(jù),SEO, 網(wǎng)站開發(fā)、運維,云服務(wù)技術(shù)支持,IM服務(wù)供應(yīng)商, FreeSwitch搭建,技術(shù)支持等. 技術(shù)討論QQ群:428622099
    隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
    數(shù)據(jù)加載中……

    python的time和date處理

    內(nèi)置模塊time包含很多與時間相關(guān)函數(shù)。我們可通過它獲得當前的時間和格式化時間輸出。

    time(),以浮點形式返回自Linux新世紀以來經(jīng)過的秒數(shù)。在linux中,00:00:00 UTC, January 1, 1970是新**49**的開始。


    strftime可以用來獲得當前時間,可以將時間格式化為字符串等等,還挺方便的。但是需要注意的是獲得的時間是服務(wù)器的時間,注意時區(qū)問題,比如gae撒謊那個的時間就是格林尼治時間的0時區(qū),需要自己轉(zhuǎn)換。

    strftime()函數(shù)將時間格式化
    我們可以使用strftime()函數(shù)將時間格式化為我們想要的格式。它的原型如下:

    size_t strftime(
    char *strDest,
    size_t maxsize,
    const char *format,
    const struct tm *timeptr
    );

    我們可以根據(jù)format指向字符串中格式命令把timeptr中保存的時間信息放在strDest指向的字符串中,最多向strDest中存放maxsize個字符。該函數(shù)返回向strDest指向的字符串中放置的字符數(shù)。

    strftime使時間格式化。python的strftime格式是C庫支持的時間格式的真子集。

      %a 星期幾的簡寫 Weekday name, abbr.
      %A 星期幾的全稱 Weekday name, full
      %b 月分的簡寫 Month name, abbr.
      %B 月份的全稱 Month name, full
      %c 標準的日期的時間串 Complete date and time representation
      %d 十進制表示的每月的第幾天 Day of the month
      %H 24小時制的小時 Hour (24-hour clock)
      %I 12小時制的小時 Hour (12-hour clock)
      %j 十進制表示的每年的第幾天 Day of the year
      %m 十進制表示的月份 Month number
      %M 十時制表示的分鐘數(shù) Minute number
      %S 十進制的秒數(shù) Second number
      %U 第年的第幾周,把星期日做為第一天(值從0到53)Week number (Sunday first weekday)
      %w 十進制表示的星期幾(值從0到6,星期天為0)weekday number
      %W 每年的第幾周,把星期一做為第一天(值從0到53) Week number (Monday first weekday)
      %x 標準的日期串 Complete date representation (e.g. 13/01/08)
      %X 標準的時間串 Complete time representation (e.g. 17:02:10)
      %y 不帶世紀的十進制年份(值從0到99)Year number within century
      %Y 帶世紀部分的十制年份 Year number
      %z,%Z 時區(qū)名稱,如果不能得到時區(qū)名稱則返回空字符。Name of time zone
      %% 百分號

    1. # handling date/time data
       2. # Python23 tested vegaseat 3/6/2005
       3.
       4. import time
       5.
       6. print "List the functions within module time:"
       7. for funk in dir(time):
       8. print funk
       9.
      10. print time.time(), "seconds since 1/1/1970 00:00:00"
      11. print time.time()/(60*60*24), "days since 1/1/1970"
      12.
      13. # time.clock() gives wallclock seconds, accuracy better than 1 ms
      14. # time.clock() is for windows, time.time() is more portable
      15. print "Using time.clock() = ", time.clock(), "seconds since first call to clock()"
      16. print "\nTiming a 1 million loop 'for loop' ..."
      17. start = time.clock()
      18. for x in range(1000000):
      19. y = x # do something
      20. end = time.clock()
      21. print "Time elapsed = ", end - start, "seconds"
      22.
      23. # create a tuple of local time data
      24. timeHere = time.localtime()
      25. print "\nA tuple of local date/time data using time.localtime():"
      26. print "(year,month,day,hour,min,sec,weekday(Monday=0),yearday,dls-flag)"
      27. print timeHere
      28.
      29. # extract a more readable date/time from the tuple
      30. # eg. Sat Mar 05 22:51:55 2005
      31. print "\nUsing time.asctime(time.localtime()):", time.asctime(time.localtime())
      32. # the same results
      33. print "\nUsing time.ctime(time.time()):", time.ctime(time.time())
      34. print "\nOr using time.ctime():", time.ctime()
      35.
      36. print "\nUsing strftime():"
      37. print "Day and Date:", time.strftime("%a %m/%d/%y", time.localtime())
      38. print "Day, Date :", time.strftime("%A, %B %d, %Y", time.localtime())
      39. print "Time (12hr) :", time.strftime("%I:%M:%S %p", time.localtime())
      40. print "Time (24hr) :", time.strftime("%H:%M:%S", time.localtime())
      41. print "DayMonthYear:",time.strftime("%d%b%Y", time.localtime())
      42.
      43. print
      44.
      45. print "Start a line with this date-time stamp and it will sort:",\
      46. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())
      47.
      48. print
      49.
      50. def getDayOfWeek(dateString):
      51. # day of week (Monday = 0) of a given month/day/year
      52. t1 = time.strptime(dateString,"%m/%d/%Y")
      53. # year in time_struct t1 can not go below 1970 (start of epoch)!
      54. t2 = time.mktime(t1)
      55. return(time.localtime(t2)[6])
      56.
      57. Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
      58. 'Friday', 'Saturday', 'Sunday']
      59.
      60. # sorry about the limitations, stay above 01/01/1970
      61. # more exactly 01/01/1970 at 0 UT (midnight Greenwich, England)
      62. print "11/12/1970 was a", Weekday[getDayOfWeek("11/12/1970")]
      63.
      64. print
      65.
      66. print "Calculate difference between two times (12 hour format) of a day:"
      67. time1 = raw_input("Enter first time (format 11:25:00AM or 03:15:30PM): ")
      68. # pick some plausible date
      69. timeString1 = "03/06/05 " + time1
      70. # create a time tuple from this time string format eg. 03/06/05 11:22:00AM
      71. timeTuple1 = time.strptime(timeString1, "%m/%d/%y %I:%M:%S%p")
      72.
      73. #print timeTuple1 # test eg. (2005, 3, 6, 11, 22, 0, 5, 91, -1)
      74.
      75. time2 = raw_input("Enter second time (format 11:25:00AM or 03:15:30PM): ")
      76. # use same date to stay in same day
      77. timeString2 = "03/06/05 " + time2
      78. timeTuple2 = time.strptime(timeString2, "%m/%d/%y %I:%M:%S%p")
      79.
      80. # mktime() gives seconds since epoch 1/1/1970 00:00:00
      81. time_difference = time.mktime(timeTuple2) - time.mktime(timeTuple1)
      82. #print type(time_difference) # test <type 'float'>
      83. print "Time difference = %d seconds" % int(time_difference)
      84. print "Time difference = %0.1f minutes" % (time_difference/60.0)
      85. print "Time difference = %0.2f hours" % (time_difference/(60.0*60))
      86.
      87. print
      88.
      89. print "Wait one and a half seconds!"
      90. time.sleep(1.5)
      91. print "The end!"

    posted on 2011-03-04 16:23 草原上的駱駝 閱讀(3367) 評論(0)  編輯  收藏 所屬分類: Python

    主站蜘蛛池模板: 亚洲国产精品乱码一区二区| 亚洲国产系列一区二区三区| 色屁屁在线观看视频免费| 鲁大师在线影院免费观看| 亚洲精品久久久www| 在线观看亚洲AV日韩AV| 久久精品免费观看国产| 亚洲人成国产精品无码| 亚洲精品成a人在线观看夫| 在线观看免费av网站| 亚洲色婷婷六月亚洲婷婷6月| 亚洲成av人片在线天堂无| 蜜桃AV无码免费看永久| 亚洲成A人片777777| 免费人成网上在线观看| 永久免费bbbbbb视频| 亚洲综合久久一本伊伊区| 特级无码毛片免费视频尤物| 久久亚洲色一区二区三区| 国产偷国产偷亚洲高清人| 女人毛片a级大学毛片免费| 亚洲性色成人av天堂| 日韩av无码久久精品免费| 亚洲女初尝黑人巨高清| 一级毛片a女人刺激视频免费| 日韩黄色免费观看| 久久精品亚洲AV久久久无码 | h片在线免费观看| 亚洲精品无码久久久影院相关影片| 色视频在线观看免费| 国产又大又粗又硬又长免费| 亚洲中文字幕久久精品无码A| ww4545四虎永久免费地址| 亚洲五月激情综合图片区| a级片在线免费看| 亚洲情XO亚洲色XO无码| 两个人www免费高清视频| 久久精品国产亚洲7777| xxxxx做受大片在线观看免费| 亚洲国产精品一区二区第一页免 | 免费人成在线观看网站品爱网 |