Rust: Get and save the time when a file was created

Hi rust community, Its me again :smiley:
I try chrono crate to get the localtime by now

extern crate chrono;
use chrono::prelude::*;

let time: DataTime<Local> = Local::now();
println!("{}", time)

output;

eqelibari@rust:~/Projects/1/BEJ$ cargo run
   Compiling BEJ v0.1.0 (/home/eqelibari/Projects/1/BEJ)
warning: crate `BEJ` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `bej`

    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target/debug/BEJ`
2020-03-09 19:01:14.408989229 +01:00

but what if I want to create a directory named with the current date/month/year, and to save a file inside this directory with the name of current time ?

You want to use the timestamp as the file name? One option is to use format! which is like println! except it provides a String.

let now: DateTime<Local> = Local::now();
let now_str = format!("{}", now);
let file = File::create(&now_str);
1 Like

actually this is not i really want to do, i will edit my post now.
Better for me is to create a directory and the name of directory to be the date/month/year, after i want to save the file inside this directory and the name will be the current time.
I know how to create a directory,and i know how to create a file,but i dont know how save a file in a specific directory. can you help me on this ?

use chrono::prelude::*;
use std::{fs::{File, create_dir}, path::Path};

let date = Local::now().format("%Y-%m-%d").to_string();
let dir_path = Path::new(&date);
create_dir(dir_path)?;

let file_path = dir_path.join("my_file.txt");
let file = File::create(file_path)?;
1 Like

and this is the full solution(here i just save the file isnide the data/time/year directory by the day month and hours).

extern crate chrono;

use chrono::prelude::*;
use std::{fs::{File, create_dir}, path::Path};

fn main() {

 let date = Local::now().format("%Y-%m-%d").to_string();
 let dir_path = Path::new(&date);
 create_dir(dir_path);

 let time = Local::now().format("%a %b %e %T").to_string();
 let file_path = dir_path.join(&time);
 let file = File::create(file_path);

}

Thank you @mbrubeck for supporting me everytime,and thank you too @alice :smiley:

Hmm sorry,again me... Something is going wrong here,and actually i cannot understand why..(i have used this method before for writing bytes into a file,and it works,but now it wont work), check it out ;

extern crate chrono;

use chrono::prelude::*;
use std::io;
use std::{fs::{File, create_dir}, path::Path};

fn give_price() -> String {

  let mut write = String::new();
  let _io_warning = io::stdin().read_line(&mut write);
  return write;

}

fn main() -> std::io::Result<()> {

 let date = Local::now().format("%Y-%m-%d").to_string();
 let dir_path = Path::new(&date);
 create_dir(dir_path);
 
 let money_in = give_price();

 let time = Local::now().format("%a %b %e %T").to_string();
 let file_path = dir_path.join(&time);
 let mut file = File::create(file_path);
 file.write_all(&money_in)

}

Output;

error[E0599]: no method named `write_all` found for type `std::result::Result<std::fs::File, std::io::Error>` in the current scope
  --> src/main.rs:26:7
   |
26 |  file.write_all(&money_in)
   |       ^^^^^^^^^ method not found in `std::result::Result<std::fs::File, std::io::Error>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `BEJ`.

To learn more, run the command again with --verbose.

Do something about the error if the create operations fails.

1 Like

I did this ;

let mut file = File::create(file_path).expect("fail");

and output's ;

error[E0308]: mismatched types
  --> src/main.rs:27:13
   |
27 |  file.write(&money_in);
   |             ^^^^^^^^^ expected slice `[u8]`, found struct `std::string::String`
   |
   = note: expected reference `&[u8]`
              found reference `&std::string::String`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `BEJ`.

To learn more, run the command again with --verbose.

..

You can use the as_bytes method to borrow your string as a byte array.

2 Likes

ohh yes, omg how i forgott this :frowning: .. thank you again <3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.