我正在处理一些持续数天的 Rust 代码,但Duration::days(n)根据文档n * 24 * 60 * 60秒数,它的实现不是 n 天,因为并非所有天都是24 * 60 * 60秒。
这种行为有据可查:
pub fn days(days: i64) -> Duration
Duration用给定的天数制作一个新的。等同于Duration::seconds(days * 24 * 60 * 60)溢出检查。当持续时间超出范围时出现恐慌。
Rust Chrono 有没有办法获得严格来说是 1 天而不是几秒的持续时间并且与DateTime类型兼容?并非所有的日子都是相同的秒数。seconds并且days是完全不同的单位。如果有这样的函数,那么下面的结果总是会在第二天的同一时间给出?
let start = Local.now();
let one_day_later = start + function_that_returns_a_duration_of_days(1);
Run Code Online (Sandbox Code Playgroud)
同样,Duration:days(1)不是这样的函数,因为它返回1 * 24 * 60 * 60seconds 而不是1 day。
例如,将TZ设置为America/Denver如下:
let …Run Code Online (Sandbox Code Playgroud) 我是铁锈和柴油的新手。并尝试使用火箭框架创建一个小型演示 api。
得到错误:不满足特征界限NaiveDateTime: Deserialize<'_>
我用谷歌搜索并找到了一些有用的链接,比如这里:https : //github.com/serde-rs/serde/issues/759
看起来版本有问题。
这是我的文件:
schema.rs
table! {
department (dept_id) {
dept_id -> Int4,
dept_name -> Nullable<Text>,
created_on -> Nullable<Timestamp>,
created_by -> Nullable<Text>,
modified_on -> Nullable<Timestamp>,
modified_by -> Nullable<Text>,
is_active -> Nullable<Bool>,
}
}
Run Code Online (Sandbox Code Playgroud)
货物.toml
[dependencies]
diesel = { version = "1.4.5", features = ["postgres","chrono","numeric"] }
dotenv = "0.15.0"
chrono = { version = "0.4.19" }
bigdecimal = { version = "0.1.0" }
rocket = "0.4.6"
rocket_codegen = "0.4.6"
r2d2-diesel = "1.0.0" …Run Code Online (Sandbox Code Playgroud) 免责声明:我是 Rust 的新手(以前的经验是 Python、TypeScript 和 Go,按顺序),我完全有可能遗漏了一些非常明显的东西。
我正在尝试构建一个 Rust 时钟接口。我在这里的基本目标是我有一个报告实际时间的小时钟结构,以及一个报告伪造版本以供测试的存根版本。请注意,这些是历史测试而不是单元测试:我的目标是重放历史数据。我认为部分问题也可能是我理解chrono得不够好。这显然是一个伟大的图书馆,但我有在主场迎战类型实例关系的麻烦chrono和chrono_tz。
无论如何,这就是我所拥有的:
use chrono::{DateTime, TimeZone, Utc};
/// A trait representing the internal clock for timekeeping requirements.
/// Note that for some testing environments, clocks may be stubs.
pub trait Clock<Tz: TimeZone> {
fn now() -> DateTime<Tz>;
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是让其他结构dyn Clock在特定时区有一个。该时钟可能是系统时钟(具有适当的时区转换),也可能是某种存根。
这是我对系统时钟垫片的尝试,一切都发生了可怕的错误:
/// A clock that reliably reports system time in the requested time zone.
struct SystemClock<Tz: TimeZone> {
time_zone: std::marker::PhantomData<*const Tz>,
}
impl<Tz: TimeZone> Clock<Tz> …Run Code Online (Sandbox Code Playgroud) 在我当前的项目中,我尝试将 a 存储chrono::Duration在配置结构中,该结构偶尔会使用serde_json.
不幸的是,似乎Serialize和Deserialize并未针对chrono::Duration. 也就是说,它通过其可选功能之一chrono提供支持。serde我尝试使用此方法,但现在编译器抱怨返回方法:
error[E0308]: mismatched types
--> src/config.rs:6:10
|
6 | #[derive(Serialize, Deserialize, Debug, Clone)]
| ^^^^^^^^^ expected struct `DateTime`, found struct `chrono::Duration`
|
= note: expected reference `&DateTime<Utc>`
found reference `&'__a chrono::Duration`
= note: this error originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0308]: mismatched types
--> src/config.rs:6:21
|
6 | #[derive(Serialize, Deserialize, Debug, …Run Code Online (Sandbox Code Playgroud) 我正在使用 chrono crate 在屏幕上显示日期。
目的是在用户首选时间或UTC(如果未设置)中显示日期。
我设置了 UTC 默认值,但我不确定记录用户时区的最佳方法以及如何将其应用于当前日期。
注意:date这里可能没有设置,所以我更愿意修改date而不是使用不同的构造函数。
let mut date: DateTime<UTC> = UTC::now();
//Convert to the User's Timezone if present
if let Some(user) = user {
//Extract the timezone
date.with_timezone(TimeZone::from_offset(&user.timezone));
}
let date_text = date.format("%H:%M %d/%m/%y").to_string();
Run Code Online (Sandbox Code Playgroud)
我想要的是要使用的类型user.timezone以及如何设置日期的示例。
从现在到下一个午夜之间获得持续时间的惯用方法是什么?
我有这样的功能:
extern crate chrono;
use chrono::prelude::*;
use time;
fn duration_until_next_midnight() -> time::Duration {
let now = Local::now(); // Fri Dec 08 2017 23:00:00 GMT-0300 (-03)
// ... how to continue??
}
Run Code Online (Sandbox Code Playgroud)
它应该是Duration1小时,因为下一个午夜是2017年12月9日星期六00:00:00 GMT-0300(-03)
extern crate chrono;
use chrono::{DateTime, Utc};
use std::time::Duration;
pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
start + Duration::from_secs(1)
}
Run Code Online (Sandbox Code Playgroud)
失败了:
error[E0277]: cannot add `std::time::Duration` to `chrono::DateTime<chrono::Utc>`
--> src/lib.rs:7:11
|
7 | start + Duration::from_secs(1_000_000_000)
| ^ no implementation for `chrono::DateTime<chrono::Utc> + std::time::Duration`
|
= help: the trait `std::ops::Add<std::time::Duration>` is not implemented for `chrono::DateTime<chrono::Utc>`
Run Code Online (Sandbox Code Playgroud)
我找不到Add要导入的实现.use chrono::*没有用.
我看到它datetime.rs有一个impl Add<chrono::oldtime::Duration>,但是oldtime是私有的,所以我不知道如何创建一个oldtime::Duration.
我如何得到Add我需要的impl?我如何转换std::time::Duration为chrono::oldtime::Duration?有什么我可以导入隐式转换?
我正在使用 rustc 1.25.0 (84203cac6 2018-03-25)
我很好奇是否有一种惯用的方法来检查 a 是否chrono::DateTime<Utc>在时间范围内。在我的用例中,我只需要检查是否DateTime在当前时间的接下来的半小时内发生。
这是我到目前为止整理的内容。它使用timestamp()属性来获取我可以使用的原始(unix)时间戳。
use chrono::prelude::*;
use chrono::Duration;
#[inline(always)]
pub fn in_next_half_hour(input_dt: DateTime<Utc>) -> bool {
in_future_range(input_dt, 30 * 60)
}
/// Check if a `DateTime` occurs within the following X seconds from now.
pub fn in_future_range(input_dt: DateTime<Utc>, range_seconds: i64) -> bool {
let utc_now_ts = Utc::now().timestamp();
let input_ts = input_dt.timestamp();
let within_range = input_ts > utc_now_ts && input_ts <= utc_now_ts + range_seconds;
within_range
}
Run Code Online (Sandbox Code Playgroud)
我的测试用例是这样的:
fn main() {
let utc_now = Utc::now();
let …Run Code Online (Sandbox Code Playgroud) 我正在尝试将日期时间字符串解析为DateTime对象,但是当我尝试这样做时,我正在获取此ParseError。我不知道发生了什么,有人可以帮我吗?
日期时间字符串: 09-January-2018 12:00:00
码: let date = DateTime::parse_from_str(date.trim(), "%d-%B-%Y %T");
我需要创建一个chrono::DateTime<Local>设置为特定日期和时间的实例。例如,我需要创建一个DateTime<Local>实例,该实例的设置为下午4:43的3/17/2019(或3:17/2019的16:43)。
该DateTime结构的文档显示了如何通过该now函数获取当前日期和时间,以及大量的支持以获取持续时间。似乎有一些令人困惑的特征和转换函数,但是似乎没有什么可以让我直接创建一个DateTime代表特定日期和时间的实例。
是否可以创建这样的实例?如果是这样,怎么办?
rust ×10
rust-chrono ×10
datetime ×3
serde ×2
duration ×1
rust-diesel ×1
rust-rocket ×1
timezone ×1