Skip to main content

appium_client/commands/
time.rs

1//! Device time
2use async_trait::async_trait;
3use fantoccini::error::CmdError;
4use http::Method;
5use serde_json::json;
6use crate::{AndroidClient, AppiumClientTrait, IOSClient};
7use crate::commands::AppiumCommand;
8
9/// Get device time
10#[async_trait]
11pub trait HasDeviceTime : AppiumClientTrait {
12
13    /// Gets device date and time for both iOS (host time is returned for simulators) and Android devices.
14    /// The default format since Appium 1.8.2 is `YYYY-MM-DDTHH:mm:ssZ`, which complies to ISO-8601.
15    async fn device_time(&self) -> Result<String, CmdError> {
16        let value = self.issue_cmd(AppiumCommand::Custom(
17            Method::GET,
18            "appium/device/system_time".to_string(),
19            None
20        )).await?;
21
22        Ok(serde_json::from_value(value)?)
23    }
24
25    /// Gets device date and time for both iOS and Android devices with given format.
26    ///
27    /// See <https://momentjs.com/docs/> to get the full list of supported datetime format specifiers.
28    /// The default format is `YYYY-MM-DDTHH:mm:ssZ`, which complies to ISO-8601.
29    async fn device_time_with_format(&self, format: &str) -> Result<String, CmdError> {
30        let value = self.execute("mobile: getDeviceTime", vec![
31            json!({"format": format})
32        ]).await?;
33
34        Ok(serde_json::from_value(value)?)
35    }
36}
37
38#[async_trait]
39impl HasDeviceTime for AndroidClient {}
40
41#[async_trait]
42impl HasDeviceTime for IOSClient {}