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
Related
How can i convert string into XPATH, below is the code
let $ti := "item/title"
let $tiValue := "Welcome to America"
return db:open('test')/*[ $tiValue = $ti]/base-uri()
Here is one way to solve it:
let $ti := "item/title"
let $tiValue := "Welcome to America"
let $input := db:open('test')
let $steps := tokenize($ti, '/')
let $process-step := function($input, $step) { $input/*[name() = $step] }
let $output := fold-left($input, $steps, $process-step)
let $test := $output[. = $tiValue]
return $test/base-uri()
The path string is split into single steps (item, title). With fold-left, all child nodes of the current input (initially db:open('test')) will be matched against the current step (initially, item). The result will be used as new input and matched against the next step (title), and so on. Finally, only those nodes with $tiValue as text value will be returned.
Your question is very unclear - the basic problem is that you've shown us some code that doesn't do what you want, and you're asking us to work out what you want by guessing what was going on in your head when you wrote the incorrect code.
I suspect -- I may be wrong -- that you were hoping this might somehow give you the result of
db:open('test')/*[item/title = $ti]/base-uri()
and presumably $ti might hold different path expressions on different occasions.
XQuery 3.0/3.1 doesn't have any standard way to evaluate an XPath expression supplied dynamically as a string (unless you count the rather devious approach of using fn:transform() to invoke an XSLT transformation that uses the xsl:evaluate instruction).
BaseX however has an query:eval() function that will do the job for you. See https://docs.basex.org/wiki/XQuery_Module
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);
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 want to get the current time rounded to the nearest second using the chrono crate but I don't know how to strip or round the result of
chrono::UTC.now().
It doesn't seem like there are any operations to modify an existing `DateTime.
chrono::UTC.now()
Returns: 2019-05-22T20:07:59.250194427Z
I want to get: 2019-05-22T20:07:59.000000000Z
How would I go about doing that in the most efficient way without breaking up the DateTime value into its components and recreating it?
Use the round_subsecs method with 0 as an argument.
use chrono::prelude::*;
fn main() {
let utc: DateTime<Utc> = Utc::now().round_subsecs(0);
println!("{}", utc);
}
The result is:
2019-05-22 20:50:46 UTC
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).