Skip to main content

UnixSeconds

Struct UnixSeconds 

Source
#[repr(transparent)]
pub struct UnixSeconds(pub i64);
Available on crate feature wifi and target_os=none only.
Expand description

Units-safe wrapper for Unix timestamps (seconds since 1970-01-01 00:00:00 UTC).

Tuple Fields§

§0: i64

Implementations§

Source§

impl UnixSeconds

Source

pub const fn as_i64(self) -> i64

Get the underlying i64 value.

Examples found in repository?
examples/wifi_auto.rs (line 151)
35async fn inner_main(spawner: Spawner) -> Result<Infallible> {
36    info!("Starting wifi_auto example");
37    let p = embassy_rp::init(Default::default());
38
39    // Initialize LED4 display
40    let cells = OutputArray::new([
41        gpio::Output::new(p.PIN_1, Level::High),
42        gpio::Output::new(p.PIN_2, Level::High),
43        gpio::Output::new(p.PIN_3, Level::High),
44        gpio::Output::new(p.PIN_4, Level::High),
45    ]);
46
47    let segments = OutputArray::new([
48        gpio::Output::new(p.PIN_5, Level::Low),
49        gpio::Output::new(p.PIN_6, Level::Low),
50        gpio::Output::new(p.PIN_7, Level::Low),
51        gpio::Output::new(p.PIN_8, Level::Low),
52        gpio::Output::new(p.PIN_9, Level::Low),
53        gpio::Output::new(p.PIN_10, Level::Low),
54        gpio::Output::new(p.PIN_11, Level::Low),
55        gpio::Output::new(p.PIN_12, Level::Low),
56    ]);
57
58    static LED4_STATIC: Led4Static = Led4::new_static();
59    let led4 = Led4::new(&LED4_STATIC, cells, segments, spawner)?;
60
61    let [
62        wifi_credentials_flash_block,
63        timezone_flash_block,
64        device_name_flash_block,
65        location_flash_block,
66    ] = FlashArray::<4>::new(p.FLASH)?;
67
68    static TIMEZONE_FIELD_STATIC: TimezoneFieldStatic = TimezoneField::new_static();
69    let timezone_field = TimezoneField::new(&TIMEZONE_FIELD_STATIC, timezone_flash_block);
70
71    static DEVICE_NAME_FIELD_STATIC: TextFieldStatic<32> = TextField::new_static();
72    let device_name_field = TextField::new(
73        &DEVICE_NAME_FIELD_STATIC,
74        device_name_flash_block,
75        "device_name",
76        "Device Name",
77        "www.picoclock.net",
78    );
79
80    static LOCATION_FIELD_STATIC: TextFieldStatic<64> = TextField::new_static();
81    let location_field = TextField::new(
82        &LOCATION_FIELD_STATIC,
83        location_flash_block,
84        "location",
85        "Location",
86        "Living Room",
87    );
88
89    let wifi_auto = WifiAuto::new(
90        p.PIN_23,                     // CYW43 power
91        p.PIN_24,                     // CYW43 clock
92        p.PIN_25,                     // CYW43 chip select
93        p.PIN_29,                     // CYW43 data pin
94        p.PIO0,                       // CYW43 PIO interface
95        p.DMA_CH0,                    // CYW43 DMA channel
96        wifi_credentials_flash_block, // Flash block storing Wi-Fi creds
97        p.PIN_13,                     // Reset button pin
98        PressedTo::Ground,            // Button wiring
99        "Pico",                       // Captive portal SSID to display
100        [timezone_field, device_name_field, location_field],
101        spawner,
102    )?;
103
104    let led4_ref = &led4;
105    let (stack, mut button) = wifi_auto
106        .connect(|event| async move {
107            match event {
108                WifiAutoEvent::CaptivePortalReady => {
109                    led4_ref.write_text(['C', 'O', 'N', 'N'], BlinkState::BlinkingAndOn);
110                }
111
112                WifiAutoEvent::Connecting { try_index, .. } => {
113                    led4_ref.animate_text(circular_outline_animation((try_index & 1) == 0));
114                }
115
116                WifiAutoEvent::ConnectionFailed => {
117                    led4_ref.write_text(['F', 'A', 'I', 'L'], BlinkState::BlinkingButOff);
118                }
119            }
120            Ok(())
121        })
122        .await?;
123
124    led4.write_text(['D', 'O', 'N', 'E'], BlinkState::Solid);
125
126    let timezone_offset_minutes = timezone_field
127        .offset_minutes()?
128        .ok_or(Error::MissingCustomWifiAutoField)?;
129    let device_name = device_name_field.text()?.unwrap_or_else(|| {
130        let mut name = String::new();
131        name.push_str("").expect("default name exceeds capacity");
132        name
133    });
134    let location = location_field.text()?.unwrap_or_else(|| {
135        let mut name = String::new();
136        name.push_str("Living Room")
137            .expect("default location exceeds capacity");
138        name
139    });
140    info!(
141        "Device '{}' in '{}' configured with timezone offset {} minutes",
142        device_name, location, timezone_offset_minutes
143    );
144
145    // At this point, `stack` can be used for internet access (HTTP, MQTT, etc.)
146    // and `button` can be used for user interactions (e.g., triggering actions).
147    info!("WiFi setup complete - press button to fetch NTP time");
148    loop {
149        button.wait_for_press().await;
150        match fetch_ntp_time(stack).await {
151            Ok(unix_seconds) => info!("Current time: {}", unix_seconds.as_i64()),
152            Err(err) => warn!("Failed to fetch time: {}", err),
153        }
154    }
155}
Source

pub const fn from_ntp_seconds(ntp: u32) -> Option<Self>

Convert NTP seconds (since 1900-01-01) to Unix seconds (since 1970-01-01).

Examples found in repository?
examples/wifi_auto.rs (line 220)
157async fn fetch_ntp_time(stack: &'static Stack<'static>) -> Result<UnixSeconds, &'static str> {
158    use udp::UdpSocket;
159
160    const NTP_SERVER: &str = "pool.ntp.org";
161    const NTP_PORT: u16 = 123;
162
163    info!("Resolving {}...", NTP_SERVER);
164    let dns_result = stack
165        .dns_query(NTP_SERVER, DnsQueryType::A)
166        .await
167        .map_err(|e| {
168            warn!("DNS lookup failed: {:?}", e);
169            "DNS lookup failed"
170        })?;
171    let server_addr = dns_result.first().ok_or("No DNS results")?;
172
173    let mut rx_meta = [udp::PacketMetadata::EMPTY; 1];
174    let mut rx_buffer = [0; 128];
175    let mut tx_meta = [udp::PacketMetadata::EMPTY; 1];
176    let mut tx_buffer = [0; 128];
177    let mut socket = UdpSocket::new(
178        *stack,
179        &mut rx_meta,
180        &mut rx_buffer,
181        &mut tx_meta,
182        &mut tx_buffer,
183    );
184
185    socket.bind(0).map_err(|e| {
186        warn!("Socket bind failed: {:?}", e);
187        "Socket bind failed"
188    })?;
189
190    let mut ntp_request = [0u8; 48];
191    ntp_request[0] = 0x1B;
192    info!("Sending NTP request...");
193    socket
194        .send_to(&ntp_request, (*server_addr, NTP_PORT))
195        .await
196        .map_err(|e| {
197            warn!("NTP send failed: {:?}", e);
198            "NTP send failed"
199        })?;
200
201    let mut response = [0u8; 48];
202    let (n, _) =
203        embassy_time::with_timeout(Duration::from_secs(5), socket.recv_from(&mut response))
204            .await
205            .map_err(|_| {
206                warn!("NTP receive timeout");
207                "NTP receive timeout"
208            })?
209            .map_err(|e| {
210                warn!("NTP receive failed: {:?}", e);
211                "NTP receive failed"
212            })?;
213
214    if n < 48 {
215        warn!("NTP response too short: {} bytes", n);
216        return Err("NTP response too short");
217    }
218
219    let ntp_seconds = u32::from_be_bytes([response[40], response[41], response[42], response[43]]);
220    UnixSeconds::from_ntp_seconds(ntp_seconds).ok_or("Invalid NTP timestamp")
221}
Source

pub fn to_offset_datetime(self, offset: UtcOffset) -> Option<OffsetDateTime>

Convert to OffsetDateTime with the given timezone offset.

Trait Implementations§

Source§

impl Clone for UnixSeconds

Source§

fn clone(&self) -> UnixSeconds

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnixSeconds

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Format for UnixSeconds

Source§

fn format(&self, f: Formatter<'_>)

Writes the defmt representation of self to fmt.
Source§

impl Ord for UnixSeconds

Source§

fn cmp(&self, other: &UnixSeconds) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for UnixSeconds

Source§

fn eq(&self, other: &UnixSeconds) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for UnixSeconds

Source§

fn partial_cmp(&self, other: &UnixSeconds) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for UnixSeconds

Source§

impl Eq for UnixSeconds

Source§

impl StructuralPartialEq for UnixSeconds

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.