如何将字符串转换为chrono ::DateTime或chrono::NaiveDateTime
ParseError(NotEnough) 或 ParseError(TooShort) 是什么意思?
虽然chrono
支持以 ISO 8601 兼容格式解析日期、时间和时区,但我无法在 crate 中找到任何方法来解析持续时间字符串,例如PT2M
表示 2 分钟的字符串。
尝试运行一个简单的程序,但由于问题而失败
note: ld: framework not found Security
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我尝试做一些调查,我发现 Cargo.toml 中的 chrono 0.4.19 指出了这一点。一旦我消除了这种依赖,它就会继续构建。我是 mac 世界的新手,看过各种建议,但我对这个问题非常无能为力。
我尝试对其进行 dockerise,然后它在容器上运行良好,但在我的 mac 上却不起作用。谁能指出我正确的方向吗?
我正在使用rust-chrono
,我正在尝试解析这样的日期:
extern crate chrono;
use chrono::*;
fn main() {
let date_str = "2013-02-14 15:41:07";
let date = DateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S");
match date {
Ok(v) => println!("{:?}", v),
Err(e) => println!("{:?}", e)
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
ParseError(NotEnough)
Run Code Online (Sandbox Code Playgroud)
这是什么意思?什么不够?我应该使用其他一些图书馆吗?
使用Chrono-TZ 库,如何获取指定时区的当前时间?
我试过
let naive_dt = Local::now().naive_local();
let dt = Los_Angeles.from_local_datetime(&naive_dt).unwrap();
println!("{:#?}", dt);
Run Code Online (Sandbox Code Playgroud)
但这会在我当前的时区打印日期时间,并附上请求的时区标识符,从而为我提供一个因时区差异而关闭的日期时间。
例如,在美国东部标准时间 18:30 (UTC+10),我询问 PST (UTC-8) 中的当前时间。应该是太平洋标准时间 00:30。相反,我得到 18:30 PST
我无法使用 Rocket 和 Diesel 从填充的 mySQL 数据库中检索日期时间。
这是我的模型:
extern crate chrono;
use diesel::prelude::*;
use diesel::mysql::MysqlConnection;
use schema::chrisms;
use diesel::sql_types::Datetime;
use self::chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
#[derive(Serialize, Deserialize, Queryable)]
pub struct Chrisms {
pub entity_ekklesia_location_id: i32,
pub serie_number: Option<String>,
pub seat_number: Option<String>,
pub date: Datetime,
pub year: i32,
pub deleted: bool,
pub entity_chrism_location_id: Option<i32>,
pub entity_chrism_location_description: Option<String>,
pub entity_rel_mec_id: Option<i32>,
pub entity_rel_mec_description: Option<String>,
pub created_by_user_id: Option<i32>,
pub updated_by_user_id: Option<i32>,
pub deleted_by_user_id: Option<i32>,
pub created_at: Datetime,
pub updated_at: Datetime,
pub …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Chrono 板条箱获取 Rust 中当前的工作日。
JavaScript 的等价物是这样的:
new Date().toLocaleDateString('en-US',{weekday: 'long'});
Run Code Online (Sandbox Code Playgroud)
我使用以下代码获取当前时间戳:
let current_time = chrono::offset::Local::now();
Run Code Online (Sandbox Code Playgroud)
我尝试.weekday()
对结果DateTime
结构调用方法,但未成功。我看到该DateLike
特征在文档中提供了这种性质的东西,但我发现自己无法在没有示例的情况下解析文档并生成相应的代码。
我有一个带有 NaiveDate 字段的结构,我想将此字段插入到表中。这是我的全部代码
use chrono::naive::NaiveDate;
use rusqlite::{params, Connection, Result};
#[derive(Debug)]
struct Person {
id: i32,
name: String,
date: NaiveDate,
}
fn main()->Result<()>{
let date_str = "2020-04-12";
let naive_date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap();
let me = Person {
id: 0,
name: "Steven".to_string(),
date: naive_date,
};
println!("{:?}",me);
let conn = Connection::open_in_memory()?;
conn.execute(
"CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
date TEXT
)",
[],
)?;
conn.execute(
"INSERT INTO person (name, date) VALUES (?1, ?2)",
params![me.name, me.date],
)?;
let mut …
Run Code Online (Sandbox Code Playgroud) 该文档没有提及任何有关此主题的内容。我需要将其转换为 吗Date<Tz>
?即使这样,也没有函数可以从中获取年份部分。
let current_date = chrono::Utc::now();
let year = current_date.year(); //this is not working, it should output the current year with i32/usize type
let month = current_date.month();
let date = current_date.date();
Run Code Online (Sandbox Code Playgroud)
let current_date = chrono::Utc::now();
let year = current_date.year(); //this is not working, it should output the current year with i32/usize type
let month = current_date.month();
let date = current_date.date();
Run Code Online (Sandbox Code Playgroud) 如何将 u64 unix 时间戳转换为DateTime<Utc>
?
let timestamp_u64 = 1657113606;
let date_time = ...
Run Code Online (Sandbox Code Playgroud) rust ×10
rust-chrono ×10
chrono-tz ×1
date ×1
datetime ×1
duration ×1
macos ×1
mysql ×1
rusqlite ×1
rust-cargo ×1
rust-diesel ×1