commonutil.ticktock

時間相關輔助函式 / Time related support routines

CYCLE_PRECISION_MINUTE(dt)[source]

(Cycle Precision Function) 週期精確度為分的時間點調整函式 / Date time value adjustment function for run_timeseries_loop

CYCLE_PRECISION_SECOND(dt)[source]

(Cycle Precision Function) 週期精確度為秒的時間點調整函式 / Date time value adjustment function for run_timeseries_loop

get_tz_offset_info(use_cached_info=True)[source]

取出時區偏移資訊,當呼叫端允許時使用快取,快取資料會留存 1800 秒 (30 分鐘) 以處理日光節約時間轉換的議題

Get time-zone offset information. Will use cached data if acceptable. The cache will keep 1800 seconds (half hour) for DST transition.

Parameters:use_cached_info=True – 是否要使用快取的結果,預設為 (True) 使用 / use cache or not
Returns:型式為 (偏移秒數, 時區名稱) 的 tuple

Tuple in (offset-seconds, timezone-name,) form

run_timeseries_loop(cycling_callable, housekeeping_callable=None, tstamp_storage_path=None, invoke_period=1800, cycle_period=3600, housekeeping_period=7200, max_delay=120, cycle_precision=<function CYCLE_PRECISION_MINUTE>)[source]

執行掃描時間序列的迴圈,指定的函數物件會在指定時間後被呼叫,兩個分別代表時間區段起點與終點之時間點會被傳入指定函數。

Run a loop to call given function (or call-able) periodically. Two date-time objects will be passed into given function. The objects indicates segments in continues time flow.

  • 注意: 時間序列精度 (cycle_precision) 的值應小於掃描頻率 (invoke_period) 的值,由於精度值為程式指定,因此呼叫此函式的程式碼應該自行檢查
  • Caution: the cycle precision adjust by precision function should smaller than invoke period.
Parameters:
  • cycling_callable – 迴圈函數,函數原型為 (bool) cycling_callable(range_s, range_e) 當回傳值為 False 時將結束迴圈 / Call-able to be invoke in given cycle. The function prototype is (bool) cycling_callable(range_s, range_e). The loop will terminate when return value of given call-able is False.
  • housekeeping_callable=None – 定期自動檢點函數 / House-keeping call-able
  • tstamp_storage_path=None – 儲存 time-stamp 值的檔案的路徑 / File path to keep progress.
  • invoke_period=1800 – 掃描週期 / Scan period to check if next invoke should take place
  • cycle_period=3600 – 每次掃描要延展的長度,作為呼叫 cycling_callable 的參數時間的長度 / Time span of the arguments for invoking cycle call-able
  • housekeeping_period=7200 – 執行檢點作業的週期 / Period to perform house keeping
  • max_delay=120 – 啓動時可接受的最大延誤時間,啟動時距離上次結束如果已經超過給定時間,則直接使用目前時間減去此延誤時間作為時間區段的起始時間 / Max acceptable delay between invoking
  • cycle_precision=CYCLE_PRECISION_MINUTE – 時間序列精度 / Time value adjustment function
timedelta_to_seconds(d)[source]

將給定的 datetime.timedelta 物件內含秒數計算出來傳回

Calculate seconds in given datetime.timedelta object.

Parameters:d – 要取秒數值的 datetime.timedelta 物件
Returns:給定的 datetime.timedelta 內含之秒數值

Total seconds in given datetime.timedelta object.