How do I pass a chrono::DateTime of one particular timezone to a function?

800 Views Asked by At

I am trying to pass a date in a particular timezone to a function and try to print it:

use chrono::{DateTime, TimeZone, Utc};  
use chrono_tz::Asia::Kolkata;

fn print_ist_time(date_time: DateTime<Kolkata>) {
    println!("Time in IST {:?}", date_time)
}


fn main() {
    let current_time = Utc::now().naive_utc();
    println!("{}", current_time);
    let ist_time = Kolkata.from_utc_datetime(&current_time);
    print_ist_time(ist_time)
}

When I compile this, I get the following error:

error[E0107]: wrong number of const arguments: expected 0, found 1
 --> src/main.rs:4:39
  |
4 | fn print_ist_time(date_time: DateTime<Kolkata>) {
  |                                       ^^^^^^^ unexpected const argument

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:4:30
  |
4 | fn print_ist_time(date_time: DateTime<Kolkata>) {
  |                              ^^^^^^^^^^^^^^^^^ expected 1 type argument

I am not able to figure out the issue.

1

There are 1 best solutions below

1
On

It you want to know the timezone in different countries give the chrono-tz library a try.

As this is not in Chrono itself (except for Local in some sense, but this then also depends on the system time, I think)