1

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.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Asnim P Ansari
  • 1,932
  • 1
  • 18
  • 41
  • `Kolkata` is not a type, [it is a *value* of type `Tz`](https://docs.rs/chrono-tz/0.5.3/chrono_tz/Asia/constant.Kolkata.html). You cannot use values where types are expected. – Shepmaster Oct 13 '20 at 20:15
  • Ok. So Is there way to ensure the value passed is of Kolkta timetamp – Asnim P Ansari Oct 13 '20 at 20:16
  • @AsnimPAnsari: The compile-time solution requires `chrono` be reworked to use [const generics](https://github.com/rust-lang/rust/issues/44580) (or roll your own), but you're probably really after a run-time check e.g. `assert_eq!(date_time.timezone(), Kolkata);` (or, more likely, a `match` statement with suitable error handling). – eggyal Oct 13 '20 at 22:26
  • Why do you want to ensure that the passed time is in the Kolkata Timezone? Couldn't you just convert it? – standard_revolution Oct 14 '20 at 12:46

1 Answers1

-2

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)

Ralph Bisschops
  • 1,888
  • 1
  • 22
  • 34
  • Can you expand your answer to clearly and concisely demonstrate how it applies to the question: *How do I pass a chrono::DateTime of one particular timezone to a function*? – Shepmaster Jul 06 '21 at 20:34