コンテンツにスキップ

DcSysTime

DcSysTime は, EtherCAT 分散クロック (Distributed Clock, DC) 上の時刻を表す型. TransitionMode::SysTimeGpioOut::SysTimeEq などで用いる.

EtherCAT では, 全スレーブで同期したクロック (分散クロック) があり, 各デバイスはこれに同期して動作する. この時刻は 64 bitの ns 単位のカウンタで, 2000年1月1日 00:00:00 (UTC) から数える.

DcSysTime は現在時刻, UTC 日時, または生の ns から構築する.

let epoch = DcSysTime::ZERO;
let now = DcSysTime::now();
let at = DcSysTime::from_utc(Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap()).unwrap();
let raw = DcSysTime::from_nanos(1_000_000_000);
let ns: u64 = now.sys_time();
let utc = now.to_utc();
操作 説明
ZERO エポック (2000-01-01 00:00:00 UTC)
now() ホストの現在時刻
from_nanos(ns) エポックからの経過 ns から構築
from_utc(utc) UTC 日時から構築
sys_time() エポックからの経過 ns
to_utc() UTC 日時へ変換

DcSysTime には時間差を加減算する演算が定義されている.

let future = DcSysTime::now() + Duration::from_millis(100);
let past = future - Duration::from_millis(50);
let elapsed: Duration = future - past;
let is_after: bool = future > past; // true
演算 結果 説明
DcSysTime + Duration DcSysTime 指定時間の加算
DcSysTime - Duration DcSysTime 指定時間の減算
DcSysTime - DcSysTime Duration 2 時刻の差 (Rust のみ)
比較 (< / > / == など) bool 比較 (Rust / C# のみ)

Rust の Durationcore::time::Duration, C# は TimeSpan, Python は autd3.Duration を用いる. Python は加減算 (+ / - による時間差の適用) のみを提供し, 時刻差の算出や比較は持たない.