How does one convert a u64 unix timestamp into a DateTime<Utc>?
let timestamp_u64 = 1657113606;
let date_time = ...
There are many options.
Assuming we want a chrono::DateTime. The offset page suggests:
Using the TimeZone methods on the UTC struct is the preferred way to construct DateTime instances.
There is a TimeZone method timestamp_millis_opt we can use.
use chrono::{TimeZone, Utc};
let timestamp_i64 = 1657113606;
let date_time = Utc.timestamp_millis_opt(timestamp_i64).unwrap();
Another option uses the appropriately named from_timestamp_millis method, but needs more code to do it if you want DateTime instead of NaiveDateTime.
use chrono::{DateTime, NaiveDateTime, Utc};
let timestamp_i64 = 1657113606;
let naive_date_time = NaiveDateTime::from_timestamp_millis(timestamp_i64).unwrap();
let date_time = DateTime::<Utc>::from_utc(naive_date_time, Utc);
Related
If I print the current time directly, it includes many trailing decimals:
println!("{:?}", chrono::offset::Utc::now());
2022-12-01T07:56:54.242352517Z
How can I get it to print like this?
2022-12-01T07:56:54Z
You can use to_rfc3339_opts. It accepts arguments for the format of the seconds and if the Z should be present.
let time = chrono::offset::Utc::now();
let formatted = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
println!("{:?}", time); // 2022-12-01T08:32:20.580242150Z
println!("{}", formatted); // 2022-12-01T08:32:20Z
Rust Playground
I want to simplify this and use it as a starttime parameter.
let d= format_datetime(now(),'yyyy-MM-dd');
let t= "T10:00:00.000Z";
let str= strcat(d,t);
let dt= todatetime(str);
print dt
5/6/2020, 10:00:00.000 AM
I always want to return today's yyyy-MM-dd but hard code the time
The way I have done it works but I'm hoping there's a better way
Thanks
A better variant would be avoiding string creation and parsing, and using datetime arithmetic operations instead:
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/scalarfunctions#datetimetimespan-functions
let dt = startofday(now())+10h;
print dt
I simply want to retrieve the current Time and Date and store it in a variable.
For this, I tried to use the chrono::DateTime.
In the documentation I found this:
use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
let dt = DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11), Utc);
This lets me store a specific Date and Time but I couldn't figure out how to retrieve the actual current date and time and put it in my DateTime-Variable.
Use rust, use it now:
use chrono;
fn main() {
println!("{:?}", chrono::offset::Local::now());
println!("{:?}", chrono::offset::Utc::now());
}
To answer the question in the title of how to get the current timestamp:
use chrono::Utc;
let dt = Utc::now();
let timestamp: i64 = dt.timestamp();
println!("Current timestamp is {}", timestamp);
Standart Rust timestamp method:
use std::time::SystemTime;
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis(); // See struct std::time::Duration methods
println!("{}", now);
I am using openresty/1.7.7.2 with Lua 5.1.4. I am receiving int64 in request and i have it's string format saved in DB (can't change DB schema or request format). I am not able to match both of them.
local i = 913034578410143848 --request
local p = "913034578410143848" -- stored in DB
print(p==tostring(i)) -- return false
print(i%10) -- return 0 ..this also doesn't work
Is there a way to convert int64 to string and vice versa if possible?
update:
I am getting i from protobuf object. proto file describe i as int64. I am using pb4lua library for protobuf.
ngx.req.read_body()
local body = ngx.req.get_body_data()
local request, err = Request:load(body)
local i = request.id
Lua 5.1 can not represent integer values larger than 2^53.
Number literal not excaption. So you can not just write
local i = 913034578410143848.
But LuaJIT can represent int64 values like boxed values.
Also there exists Lua libraries to make deal with large numbers.
E.g. bn library.
I do not know how your pb4lua handle this problem.
E.g. lua-pb library uses LuaJIT boxed values.
Also it provide way to specify user defined callback to make int64 value.
First I suggest figure out what real type of your i value (use type function).
All other really depends on it.
If its number then I think pb4lua just loose some info.
May be it just returns string type so you can just compare it as string.
If it provide LuaJIT cdata then this is basic function to convert string
to int64 value.
local function to_jit_uint64(str)
local v = tonumber(string.sub(str, 1, 9))
v = ffi.new('uint64_t', v)
if #str > 9 then
str = string.sub(str, 10)
v = v * (10 ^ #str) + tonumber(str)
end
return v
end
My goal is to convert utc into loc:
use chrono::{Local, UTC, TimeZone};
let utc = chrono::UTC::now();
let loc = chrono::Local::now();
println!("{:?}", utc);
println!("{:?}", loc);
println!("{:?}", utc.with_timezone(&Local));
println!("{:?}", Local.from_utc_datetime(&utc.naive_local()));
... which produced the following output:
2015-02-26T16:22:27.873593Z
2015-02-26T17:22:27.873663+01:00
2015-02-26T15:22:27.873593+00:00
2015-02-26T15:22:27.873593+00:00
The loc time shown in the second row is what I want to see when converting utc.
How do I properly convert a DateTime<UTC> instance to DateTime<Local>?
Meta
I am using chrono 0.2.2. In the DateTime.from_utc method it's even telling me I should use the TimeZone trait. However, I am missing something.
Starting with chrono 0.4.7 you can convert them between with using from trait in a simpler way:
use chrono::prelude::*;
fn main() {
let utc = Utc::now();
let local = Local::now();
let converted: DateTime<Local> = DateTime::from(utc);
}
Oops, thank you for reporting. This is a bug and registered as the issue #26. This should be fixed in Chrono 0.2.3.
Besides from the bug, utc.with_timezone(&Local) is indeed a correct way to convert to the local time. There is an important identity that utc.with_timezone(&Local).with_timezone(&UTC) should be equal to utc (except for the exceptional case, where the local time zone has been changed).