String to timestamp


#1

Hi there

i’m trying to send data from a postgresql database to an other so firstly when a new entry appears on the first databse i take the data and send them to the other :

 for x in counter_rl+1 .. counter_rl2+1 {
				for row in &db_local.query("SELECT id, date_add, date_read, astmcontent, lis_name from astm_receive where id = $1", &[&x]).unwrap(){
					let entre = AstmR {
						uuid: my_uuid,
						id: row.get(0),
						date_add: row.get(1),
						date_read: row.get(2),
						astmcontent: row.get(3),
						lis_name: row.get(4),
					};
					//let &test = entre.uuid;
					peer1.send("channel", (entre.date_add.to_string() + "/" + &entre.date_read.to_string() + "/" + &entre.astmcontent + "/" + &entre.lis_name)).expect("error during send message");
				}
			}

and i have this struct :

struct  AstmR {
	uuid: Uuid,
	id: i32,
	date_add: chrono::NaiveDateTime,
	date_read: chrono::NaiveDateTime,
	astmcontent: String,
	lis_name: String,
}

my probleme is on date_add and date_read which are date format. and the only way i have to send them is to convert them to string

So

When i receive the data :

      		if mess.channel == "channel" {
    			let v :Vec<&str> = mess.content.split('/').collect();
    			// println!("{:?}",v[0]);
    			// println!("{:?}",v[1]);
    			// println!("{:?}",v[2]);
    			// println!("{:?}",v[3]);
			db_local.execute("INSERT INTO astm_receive (date_add, date_read, astmcontent, lis_name) VALUES ($1, $2, $3, $4)",
				&[&v[0], &v[1], &v[2], &v[3]]).unwrap();
			println!("done");
    		}

i have this kind of error :

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Conversion(WrongType(Timestamp))', ../src/libcore/result.rs:799
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/main` (exit code: 101)

So i think i have to convert them again to a date format but i have no idea on how i can do this. So if someone has any idea to help me :slight_smile:

Thanks for the attention


#2

You shouldn’t need to convert to String - enable the with-chrono feature on postgres: https://docs.rs/postgres/0.13.4/postgres/types/trait.ToSql.html#types