python UNIX_TIMESTAMP時間処理方法の分析


本論文の実例はpython UNIX_を述べている。TIMESTAMP時間処理方法。皆さんに参考にしてあげます。具体的には以下の通りです。

from datetime import datetime
import time
int(time.mktime(rm.start_time.timetuple()))
 datetime.fromtimestamp(time.time())
rf.start_time + timedelta(0, math.ceil(diff.total_seconds()/(3600*rf.period))*3600*rf.period)

月または時間ごとのオフセット時間を取得します。

def getlastday(datetimeobj):
 year= datetimeobj.year
 month = datetimeobj.month + 1
 if month>12:
  year = year + 1
  month=1
 return datetime(year, month, 1) - timedelta(1)
def getSendTime(period, start_time):
 datenow = datetime.now()
 nextTime = preTime = start_time
 if period > 0 and period < 1000:
  diff = datetime.now() - start_time
  nextTime = start_time + timedelta(0, math.ceil(diff.total_seconds()/(3600*period))*3600*period)
  preTime = start_time + timedelta(0, math.floor(diff.total_seconds()/(3600*period))*3600*period)
 if period == 1001:
  if datenow.day>start_time.day:
   preTime = datetime(datenow.year, datenow.month, start_time.day)
   nextTime = getlastday(getlastday(datenow)+timedelta(1))
   if nextTime.day > start_time.day:
    nextTime = datetime(nextTime.year, nextTime.month, start_time.day)
  else:
   preTime = getlastday(datetime(datenow.year, datenow.month, 1) - timedelta(1))
   if preTime.day > start_time.day:
    preTime = datetime(preTime.year, preTime.month, start_time.day)
   nextTime = getlastday(datenow)
   if nextTime.day > start_time.day:
    nextTime = datetime(nextTime.year, nextTime.month, start_time.day)
 return preTime, nextTime

並べ替え

reportMetaPeriodInfo.sort(lambda p1, p2:cmp(p1['k'], p2['k']))

ipを取得

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  s.connect(('8.8.8.8', 0))
  localIP=s.getsockname()[0]

Python関連の内容についてもっと興味がある方は、本駅のテーマを見てください。「Python日付と時間操作テクニックのまとめ」、「Python関数使用テクニックのまとめ」、「Python文字列操作テクニックのまとめ」、「Python入門と階段の経典教程」、「Pythonファイルとディレクトリ操作の概要
ここで述べたように、皆様のPythonプログラムの設計に役に立ちます。