Hi rust community, Its me again
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 ?
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)?;
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
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.
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.