CFV
CFV

Reputation: 792

How can I use the Chrono library to get seconds since Unix epoch?

I can get the date, the hours, minutes, seconds and nanoseconds in the date format, but I can't get seconds as a floating point or integer number.

extern crate chrono;
use chrono::prelude::*;
fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{}", local); 
} 

I have already read the docs.

Upvotes: -4

Views: 2518

Answers (2)

CFV
CFV

Reputation: 792

I solved the problem using the function timestamp as written in the docs. It didn't work before because I forgot to use the brackets to call timestamp.

extern crate chrono;

use chrono::prelude::*;

fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{}", local.timestamp()); // I forgot the brackets after timestamp
}

Thanks to mcarton.

Upvotes: 4

Shepmaster
Shepmaster

Reputation: 430290

Use Timelike::second:

extern crate chrono;

use chrono::prelude::*;

fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{}", local.second()); 
} 

Upvotes: 2

Related Questions