大漠駝鈴

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

          python的time和date處理

          內置模塊time包含很多與時間相關函數。我們可通過它獲得當前的時間和格式化時間輸出。

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


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

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

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

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

          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 十時制表示的分鐘數 Minute number
            %S 十進制的秒數 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 時區名稱,如果不能得到時區名稱則返回空字符。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 草原上的駱駝 閱讀(3366) 評論(0)  編輯  收藏 所屬分類: Python

          主站蜘蛛池模板: 富民县| 八宿县| 屏东县| 土默特右旗| 武威市| 什邡市| 行唐县| 兴业县| 辽中县| 宣城市| 始兴县| 南雄市| 会泽县| 若尔盖县| 突泉县| 临漳县| 资溪县| 汝阳县| 五河县| 宜兴市| 客服| 西盟| 长寿区| 湖南省| 牡丹江市| 共和县| 凤凰县| 兴安盟| 英吉沙县| 富民县| 乌拉特后旗| 青浦区| 临沧市| 巴彦淖尔市| 宽甸| 三明市| 汝南县| 同德县| 吐鲁番市| 万载县| 曲阜市|