I am parsing set of xml records that has time stamp strings. My end goal is to parse these records and put them in mysql database. I am new to ruby and script was put together by looking at other examples in online:
Here is how my xml records looks like:
<row Id="253828" UserId="56959" Name="Teacher" Date="2009-03-08T00:27:31.807" />
<row Id="253829" UserId="22221" Name="Popular Question" Date="2009-03-08T00:32:32.217" />
This is how the ruby method looks like:
require 'date'
@st = @my.prepare("insert into badge(id, user_id, name, created) values(?, ?, ?, ?)")
if element == 'row'
@st.execute(attributes['Id'], attributes['UserId'], attributes['Name'], DateTime.parse(attributes['Date']).strftime("%F %T"))
end
But the problem is after parsing close to 10,000 something records I get a mysql error:
Incorrect datetime value: '2009-03-08 02:02:32' for column 'created' at row 1 (Mysql::Error)
I started to check this in repl and here is the out put of repl:
irb(main):016:0> DateTime.parse('2009-03-08T00:27:31.807').strftime("%F %T")
=> "2009-03-08 00:27:31"
irb(main):017:0> DateTime.parse('2009-03-08T00:32:32.217').strftime("%F %T")
=> "2009-03-08 00:32:32"
If you look carefully you see that orginal time string had 2009-03-08T00:27:31.807 and value 807 didn't make any impact on the conversion. Because if we change the two time strings to be exact except the different is the last three digits after . then I have a feeling the conversion is incorrect. I wonder whats the best way to convert this to valid mysql time stamp while keep the the integrity of the data.
Hypothetically, lets say time string is 2009-03-08T00:27:31.807 and 2009-03-08T00:27:31.507 the trying this on repl would give us:
irb(main):018:0> DateTime.parse('2009-03-08T00:27:31.807').strftime("%F %T")
=> "2009-03-08 00:27:31"
irb(main):019:0> DateTime.parse('2009-03-08T00:27:31.507').strftime("%F %T")
=> "2009-03-08 00:27:31"
They are both same time stamp but are they?
I found following post in StackExchange DataBase Admin section: https://dba.stackexchange.com/questions/48704/mysql-5-6-datetime-incorrect-datetime-value-2013-08-25t1700000000-with-er not sure if I should do something like that.
To give a complete picture from the mysql side: Here is how my table looks like:
CREATE TABLE badge
(
id INT NOT NULL PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(40) NULL,
created TIMESTAMP
);
%Nto get the milliseconds.DateTime.parse('2009-03-08T00:27:31.807').strftime("%F %T.%N") => "2009-03-08 00:27:31.807000000"