qemu_command_builder/
rtc.rs

1use bon::Builder;
2use chrono::{DateTime, Utc};
3
4use crate::to_command::{ToArg, ToCommand};
5
6#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
7pub enum RtcBase {
8    Utc,
9    Localtime,
10    Datetime(DateTime<Utc>),
11}
12
13#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
14pub enum RtcClock {
15    Host,
16    Rt,
17    Vm,
18}
19
20impl ToArg for RtcClock {
21    fn to_arg(&self) -> &str {
22        match self {
23            RtcClock::Host => "host",
24            RtcClock::Rt => "rt",
25            RtcClock::Vm => "vm",
26        }
27    }
28}
29#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
30pub enum RtcDriftFix {
31    None,
32    Slew,
33}
34
35impl ToArg for RtcDriftFix {
36    fn to_arg(&self) -> &str {
37        match self {
38            RtcDriftFix::None => "none",
39            RtcDriftFix::Slew => "slew",
40        }
41    }
42}
43#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Default, Builder)]
44pub struct Rtc {
45    base: Option<RtcBase>,
46    clock: Option<RtcClock>,
47    drift_fix: Option<RtcDriftFix>,
48}
49
50impl ToCommand for Rtc {
51    fn to_command(&self) -> Vec<String> {
52        let mut cmd = vec![];
53
54        cmd.push("-rtc".to_string());
55
56        let mut args = vec![];
57
58        if let Some(base) = &self.base {
59            match base {
60                RtcBase::Utc => {
61                    args.push("base=utc".to_string());
62                }
63                RtcBase::Localtime => {
64                    args.push("base=localtime".to_string());
65                }
66                RtcBase::Datetime(dt) => {
67                    // use formats 2006-06-17T16:01:21 or 2006-06-17
68                    // TODO support date based
69                    let formatted = format!("base={}", dt.format("%Y-%m-%dT%H:%M:%S"));
70                    args.push(formatted);
71                }
72            }
73        }
74
75        if let Some(clock) = &self.clock {
76            args.push(format!("clock={}", clock.to_arg()));
77        }
78        if let Some(drift_fix) = &self.drift_fix {
79            args.push(format!("drift={}", drift_fix.to_arg()));
80        }
81
82        cmd.push(args.join(","));
83        cmd
84    }
85}