Skip to content

Commit 7e4c7d9

Browse files
LarryOstermanchlowellCopilot
authored
Standardize on time::Duration and time::OffsetDateTime for time elements (#2713)
* standardize on time::Duration and time::OffsetDateTime for time elements --------- Co-authored-by: Charles Lowell <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 847e89a commit 7e4c7d9

File tree

79 files changed

+572
-594
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+572
-594
lines changed

Cargo.lock

Lines changed: 8 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/core/azure_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ the asynchronous runtime used by the Azure SDK.
1010
### Breaking Changes
1111

1212
- Moved `process::Executor` to `azure_identity`.
13+
- Renamed `azure_core::date` to `azure_core::time` and added `azure_core::time::Duration` as the standard "duration" type for the SDK.
1314

1415
### Bugs Fixed
1516

sdk/core/azure_core/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ sha2 = { workspace = true, optional = true }
2727
tokio = { workspace = true, optional = true }
2828
tracing.workspace = true
2929
typespec = { workspace = true, features = ["http", "json"] }
30-
typespec_client_core = { workspace = true, features = ["derive", "http", "json"] }
30+
typespec_client_core = { workspace = true, features = [
31+
"derive",
32+
"http",
33+
"json",
34+
] }
3135

3236
[build-dependencies]
3337
rustc_version.workspace = true
@@ -38,7 +42,6 @@ azure_identity.workspace = true
3842
azure_security_keyvault_secrets.path = "../../keyvault/azure_security_keyvault_secrets"
3943
criterion.workspace = true
4044
thiserror.workspace = true
41-
time.workspace = true
4245
tokio.workspace = true
4346
tracing-subscriber.workspace = true
4447

sdk/core/azure_core/README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ you can find the [package on crates.io][Package (crates.io)].
2020

2121
The main shared concepts of `azure_core` - and Azure SDK libraries using `azure_core` - include:
2222

23-
- Configuring service clients, e.g. configuring retries, logging (`ClientOptions`).
24-
- Accessing HTTP response details (`Response<T>`).
25-
- Paging and asynchronous streams (`Pager<T>`).
26-
- Errors from service requests in a consistent fashion. (`azure_core::Error`).
27-
- Customizing requests (`ClientOptions`).
28-
- Abstractions for representing Azure SDK credentials. (`TokenCredentials`).
23+
- Configuring service clients, e.g. configuring retries, logging (`ClientOptions`).
24+
- Accessing HTTP response details (`Response<T>`).
25+
- Paging and asynchronous streams (`Pager<T>`).
26+
- Errors from service requests in a consistent fashion. (`azure_core::Error`).
27+
- Customizing requests (`ClientOptions`).
28+
- Abstractions for representing Azure SDK credentials. (`TokenCredentials`).
2929

3030
### Thread safety
3131

@@ -44,15 +44,15 @@ We guarantee that all client instance methods are thread-safe and independent of
4444

4545
## Features
4646

47-
- `debug`: enables extra information for developers e.g., emitting all fields in `std::fmt::Debug` implementation.
48-
- `hmac_openssl`: configures HMAC using `openssl`.
49-
- `hmac_rust`: configures HMAC using pure Rust.
50-
- `reqwest` (default): enables and sets `reqwest` as the default `HttpClient`. Enables `reqwest`'s `native-tls` feature.
51-
- `reqwest_deflate` (default): enables deflate compression for `reqwest`.
52-
- `reqwest_gzip` (default): enables gzip compression for `reqwest`.
53-
- `reqwest_rustls`: enables `reqwest`'s `rustls-tls-native-roots-no-provider` feature,
54-
- `tokio`: enables and sets `tokio` as the default async runtime.
55-
- `xml`: enables XML support.
47+
- `debug`: enables extra information for developers e.g., emitting all fields in `std::fmt::Debug` implementation.
48+
- `hmac_openssl`: configures HMAC using `openssl`.
49+
- `hmac_rust`: configures HMAC using pure Rust.
50+
- `reqwest` (default): enables and sets `reqwest` as the default `HttpClient`. Enables `reqwest`'s `native-tls` feature.
51+
- `reqwest_deflate` (default): enables deflate compression for `reqwest`.
52+
- `reqwest_gzip` (default): enables gzip compression for `reqwest`.
53+
- `reqwest_rustls`: enables `reqwest`'s `rustls-tls-native-roots-no-provider` feature,
54+
- `tokio`: enables and sets `tokio` as the default async runtime.
55+
- `xml`: enables XML support.
5656

5757
## Examples
5858

@@ -255,8 +255,9 @@ If your application uses a different asynchronous runtime, you can replace the a
255255
You provide the implementation by calling the `set_async_runtime()` API:
256256

257257
```rust no_run
258-
use azure_core::async_runtime::{
259-
set_async_runtime, AsyncRuntime, TaskFuture, SpawnedTask};
258+
use azure_core::{async_runtime::{
259+
set_async_runtime, AsyncRuntime, TaskFuture, SpawnedTask},
260+
time::Duration};
260261
use std::sync::Arc;
261262
use futures::FutureExt;
262263

@@ -266,7 +267,7 @@ impl AsyncRuntime for CustomRuntime {
266267
fn spawn(&self, f: TaskFuture) -> SpawnedTask {
267268
unimplemented!("Custom spawn not implemented");
268269
}
269-
fn sleep(&self, duration: std::time::Duration) -> TaskFuture {
270+
fn sleep(&self, duration: Duration) -> TaskFuture {
270271
unimplemented!("Custom sleep not implemented");
271272
}
272273
}

sdk/core/azure_core/src/credentials.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use serde::{Deserialize, Serialize};
77
use std::{borrow::Cow, fmt::Debug};
8-
use typespec_client_core::{date::OffsetDateTime, fmt::SafeDebug};
8+
use typespec_client_core::{fmt::SafeDebug, time::OffsetDateTime};
99

1010
/// Default Azure authorization scope.
1111
pub static DEFAULT_SCOPE_SUFFIX: &str = "/.default";

sdk/core/azure_core/src/http/policies/bearer_token_policy.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use crate::{
1212
use async_lock::RwLock;
1313
use async_trait::async_trait;
1414
use std::sync::Arc;
15-
use std::time::Duration;
16-
use typespec_client_core::date::OffsetDateTime;
1715
use typespec_client_core::http::{Context, Request};
16+
use typespec_client_core::time::{Duration, OffsetDateTime};
1817

1918
/// Authentication policy for a bearer token.
2019
#[derive(Debug, Clone)]
@@ -114,7 +113,7 @@ impl Policy for BearerTokenCredentialPolicy {
114113
}
115114

116115
fn should_refresh(expires_on: &OffsetDateTime) -> bool {
117-
*expires_on <= OffsetDateTime::now_utc() + Duration::from_secs(300)
116+
*expires_on <= OffsetDateTime::now_utc() + Duration::minutes(5)
118117
}
119118

120119
#[cfg(test)]
@@ -127,6 +126,7 @@ mod tests {
127126
policies::Policy,
128127
Request, StatusCode,
129128
},
129+
time::OffsetDateTime,
130130
Bytes, Result,
131131
};
132132
use async_trait::async_trait;
@@ -136,10 +136,9 @@ mod tests {
136136
atomic::{AtomicUsize, Ordering},
137137
Arc,
138138
};
139-
use std::time::Duration;
140-
use time::OffsetDateTime;
141-
use typespec_client_core::http::{
142-
policies::TransportPolicy, Method, RawResponse, TransportOptions,
139+
use typespec_client_core::{
140+
http::{policies::TransportPolicy, Method, RawResponse, TransportOptions},
141+
time::Duration,
143142
};
144143

145144
#[derive(Debug, Clone)]
@@ -245,7 +244,7 @@ mod tests {
245244
}
246245

247246
for handle in handles {
248-
tokio::time::timeout(Duration::from_secs(2), handle)
247+
tokio::time::timeout(Duration::seconds(2).try_into().unwrap(), handle)
249248
.await
250249
.expect("task timed out after 2 seconds")
251250
.expect("completed task");
@@ -256,7 +255,7 @@ mod tests {
256255
async fn caches_token() {
257256
run_test(&[AccessToken {
258257
token: Secret::new("fake".to_string()),
259-
expires_on: OffsetDateTime::now_utc() + Duration::from_secs(3600),
258+
expires_on: OffsetDateTime::now_utc() + Duration::seconds(3600),
260259
}])
261260
.await;
262261
}
@@ -266,11 +265,11 @@ mod tests {
266265
run_test(&[
267266
AccessToken {
268267
token: Secret::new("1".to_string()),
269-
expires_on: OffsetDateTime::now_utc() - Duration::from_secs(1),
268+
expires_on: OffsetDateTime::now_utc() - Duration::seconds(1),
270269
},
271270
AccessToken {
272271
token: Secret::new("2".to_string()),
273-
expires_on: OffsetDateTime::now_utc() + Duration::from_secs(3600),
272+
expires_on: OffsetDateTime::now_utc() + Duration::seconds(3600),
274273
},
275274
])
276275
.await;

sdk/core/azure_core/src/http/poller.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
//! Types and methods for Long-Running Operations (LROs).
55
66
use crate::http::headers::Headers;
7-
use std::time::Duration;
8-
use typespec_client_core::date::OffsetDateTime;
7+
use typespec_client_core::time::{Duration, OffsetDateTime};
98

109
/// Default retry time for long running operations if no retry-after header is present
1110
///
1211
/// This value is the same as the default used in the Azure SDK for Python.
1312
/// Ref: <https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-mgmt-core/azure/mgmt/core/polling/arm_polling.py#L191>
14-
const DEFAULT_RETRY_TIME: Duration = Duration::from_secs(30);
13+
const DEFAULT_RETRY_TIME: Duration = Duration::seconds(30);
1514

1615
/// Long-Running Operation (LRO) status.
1716
#[derive(Debug)]

sdk/core/azure_core/src/http/request/options/if_source_modified_since_condition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Licensed under the MIT License.
33

44
use crate::{
5-
date,
65
http::headers::{self, Header, HeaderName},
6+
time,
77
};
8-
use typespec_client_core::date::OffsetDateTime;
8+
use typespec_client_core::time::OffsetDateTime;
99

1010
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1111
pub enum IfSourceModifiedSinceCondition {
@@ -24,7 +24,7 @@ impl Header for IfSourceModifiedSinceCondition {
2424
fn value(&self) -> headers::HeaderValue {
2525
match self {
2626
IfSourceModifiedSinceCondition::Modified(date)
27-
| IfSourceModifiedSinceCondition::Unmodified(date) => date::to_rfc7231(date).into(),
27+
| IfSourceModifiedSinceCondition::Unmodified(date) => time::to_rfc7231(date).into(),
2828
}
2929
}
3030
}

sdk/core/azure_core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ pub use constants::*;
2222

2323
// Re-export modules in typespec_client_core such that azure_core-based crates don't need to reference it directly.
2424
pub use typespec_client_core::{
25-
async_runtime, base64, create_enum, create_extensible_enum, date,
25+
async_runtime, base64, create_enum, create_extensible_enum,
2626
error::{self, Error, Result},
27-
fmt, json, sleep, stream, Bytes, Uuid,
27+
fmt, json, sleep, stream, time, Bytes, Uuid,
2828
};
2929

3030
#[cfg(feature = "xml")]

sdk/core/azure_core_amqp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Breaking Changes
88

9+
- Converted all `time::Duration` references to be `azure_core::time::Duration`.
10+
911
### Bugs Fixed
1012

1113
### Other Changes

0 commit comments

Comments
 (0)