pythonタイムモジュールの使用
5295 ワード
前言:
開発ではイベントスタンプの取得、タイムスタンプのフォーマットなど、時間と関わることがよくありますが、pythonの操作時間の方法を簡単に記録します.
pythonでよく見られる処理時間のモジュール: time:タイムスタンプの取得、フォーマット日などの時間を処理するモジュール datetime:dateとtimeの結合体、処理日時 calendar:カレンダー関連モジュール、例えばカレンダー/カレンダー の処理
timeモジュールの紹介
説明:timeモジュールは主に以下の内容を説明します. 1.タイムスタンプ-->タイムタプルフォーマット(time.struct_time)-->日付文字列 2.日付文字列-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ 3.現在時刻の分/秒 を取得 4.1分間/1時間のタイムスタンプ を取得
1.タイムスタンプ-->タイムタプルフォーマット(time.struct_time)-->日付文字列タイムスタンプ-->タイムグループ形式time.localtime(timestamp)#パラメータtimestampは秒タイムスタンプ 例: 時間タプル-->日付文字列time.strftime(format,p_tuple=None):format:フォーマットされた日付スタイル;p_tuple:タイムユニット 例: パッケージ方法:
2.日付文字列-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ日付文字列-->時間メタグループtime.strptime(string,format)#string:日付文字列、format:日付文字列に対応するフォーマット 例: 時間タプル-->タイムスタンプtime.mktime(p_tuple):p_tuple:タイムユニット 例: のパッケージング方法
3.現在時刻の分/秒を取得する現在のタイムスタンプ を取得する.現在時刻の秒 を取得する.現在時刻の分を取得
4.1分間/1時間のタイムスタンプを取得の考え方:まず対応する進数値で割って整頓し、捨てた残数部分の整数を得る、それから対応する進数値 を乗じる.
datetimeモジュールの紹介
Datetimeモジュールでよく見られるクラス: datetime.date:処理日 datetime.time:処理時間 datetime.Datetime:処理日時 datetime.timedelta:処理時間差 説明:datetimeモジュールは主に以下の内容を説明します. 1.タイムスタンプ-->datetimeタイムフォーマット-->日付文字列 2.日付文字列-->datetimeタイムフォーマット-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ 3.時間差の使用は、現在時刻より前のN日の時刻 を取得する.
1.タイムスタンプ-->datetimeタイムフォーマット-->日付文字列タイムスタンプ-->datetimeタイムフォーマットdatetime.datetime.fromtimestamp(timestamp)パラメータtimestamp:タイムスタンプ 例: datetime時間フォーマット-->日付文字列datetime.datetime.strftime(format)format:日付文字列に対応するフォーマット 例:
2.日付文字列-->datetimeタイムフォーマット-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ日付文字列-->datetime時間フォーマットdatetime.datetime.strptime(date_str, format) date_str:文字列日付format:日付文字列対応フォーマット 例: datetimeタイムフォーマット-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプdatetime.datetime.timetuple():datetime変換時間タプル 例:
3.時間差の使用は、現在時刻より前のN日間の時刻datetimeを取得する.timedelta(days,seconds,microseconds,milliseconds,minutes,hours,weeks)パラメータの説明:1.days:ああ.seconds:秒3.microseconds:ミリ秒1秒=10^3ミリ秒4.milliseconds:マイクロ秒1秒=10^6マイクロ秒5.minutes、分6.hours:時間7.weeks:月曜日weeks=7 days例:
calendarモジュールの紹介
説明:
ここでは、month(year,month)メソッドを使用して、ある年のある月の下のカレンダー時間を印刷する方法を紹介します.
例:
いいね!
開発ではイベントスタンプの取得、タイムスタンプのフォーマットなど、時間と関わることがよくありますが、pythonの操作時間の方法を簡単に記録します.
pythonでよく見られる処理時間のモジュール:
timeモジュールの紹介
説明:timeモジュールは主に以下の内容を説明します.
1.タイムスタンプ-->タイムタプルフォーマット(time.struct_time)-->日付文字列
import time
time_tuple = time.localtime(time.time())
print time_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=29, tm_sec=33, tm_wday=2, tm_yday=30, tm_isdst=0)
time_format = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print time_format # 2019-01-30 11:48:07
def timestamp_format(timestamp):
"""
:brief
:param timestamp:
:return:
"""
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
2.日付文字列-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ
import time
time_str_to_tuple = time.strptime("2019-01-30 11:48:07", "%Y-%m-%d %H:%M:%S")
print time_str_to_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=48, tm_sec=7, tm_wday=2, tm_yday=30, tm_isdst=-1)
time_tuple_to_timestamp = int(time.mktime(time_str_to_tuple))
print time_tuple_to_timestamp # :1548820087
def time_str_to_timestamp(date_str, format):
"""
:brief
:param date_str: , :2019-01-30 11:48:07
:param format: , :%Y-%m-%d %H:%M:%S
:return:
"""
return int(time.mktime(time.strptime(date_str, format)))
3.現在時刻の分/秒を取得する
timestamp = int(time.time())
seconds = timestamp % 60
print "seconds:{}".format(seconds)
minute = (timestamp - seconds) % (60 * 60)
print "minute:{}".format(minute / 60)
4.1分間/1時間のタイムスタンプを取得
one_minute = 60 #
one_hour = one_minute * 60 #
whole_minute = int(timestamp / one_minute) * one_minute
whole_hour = int(timestamp / one_hour) * one_hour
datetimeモジュールの紹介
Datetimeモジュールでよく見られるクラス:
1.タイムスタンプ-->datetimeタイムフォーマット-->日付文字列
import time, datetime
datetime_type = datetime.datetime.fromtimestamp(time.time())
print type(datetime_type) #
datetime_format = datetime_type.strftime("%Y/%m/%d %H:%M:%S")
print datetime_format # 2019/01/30 16:44:01
2.日付文字列-->datetimeタイムフォーマット-->タイムタプルフォーマット(time.struct_time)-->タイムスタンプ
datetime_type = datetime.datetime.strptime('2019/01/30 16:44:01', '%Y/%m/%d %H:%M:%S')
print type(datetime_type) #
# print datetime_type.timestamp()
print time.mktime(datetime_type.timetuple())
datetime_type_to_timestamp = int(time.mktime(datetime_type.timetuple()))
print datetime_type_to_timestamp
3.時間差の使用は、現在時刻より前のN日間の時刻datetimeを取得する.timedelta(days,seconds,microseconds,milliseconds,minutes,hours,weeks)パラメータの説明:1.days:ああ.seconds:秒3.microseconds:ミリ秒1秒=10^3ミリ秒4.milliseconds:マイクロ秒1秒=10^6マイクロ秒5.minutes、分6.hours:時間7.weeks:月曜日weeks=7 days
day_timedelta = datetime.timedelta(days=1) # 1
forward_datetime = datetime.datetime.today() - day_timedelta # datetime
print forward_datetime
calendarモジュールの紹介
説明:
ここでは、month(year,month)メソッドを使用して、ある年のある月の下のカレンダー時間を印刷する方法を紹介します.
例:
import calendar
cal = calendar.month(2019, 1) # 2019 1
print cal
いいね!