qemu_command_builder/
rtc.rs

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