nfs_rs/shared.rs
1// Copyright 2025 NetApp Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17use std::time::{Duration, SystemTime, UNIX_EPOCH};
18
19/// Struct describing an NFS timestamp.
20#[derive(Clone, Copy, Debug, Default, PartialEq)]
21pub struct Time {
22 pub seconds: u32,
23 pub nseconds: u32,
24}
25
26impl Time {
27 // Convert to std::time::SystemTime
28 pub fn to_system_time(&self) -> SystemTime {
29 UNIX_EPOCH + Duration::new(self.seconds as u64, self.nseconds)
30 }
31
32 // Create Time from SystemTime
33 pub fn from_system_time(system_time: SystemTime) -> Self {
34 match system_time.duration_since(UNIX_EPOCH) {
35 Ok(duration) => Time {
36 seconds: duration.as_secs() as u32,
37 nseconds: duration.subsec_nanos(),
38 },
39 Err(e) => {
40 // Handle time earlier than UNIX_EPOCH
41 let duration = e.duration();
42 Time {
43 seconds: duration.as_secs() as u32,
44 nseconds: duration.subsec_nanos(),
45 }
46 }
47 }
48 }
49}