Ruby gem Mysql2 return date type cannot be parsed - ruby

I got the result in hash format:
"date"=>#'<'Date: 4911851/2,0,2299161>
Anyone know how to parse this format?
I'd like to parse it in to a ruby Date object and do further processing.

The result is still an timestamp object as far as I know. Just use it as it is inside an Date or DateTime to extract any data.
Alle the best.

Related

How to change Faker date format to yield date in "DD/MM/YYYY" format?

I am using Faker to generate a random Birthdate and feed the same in a Date input box in DD/MM/YYYY format through my automation script.
I tried this below snippet, however, it returns a date in YYYY/MM/DD format. I tried changing the from and to date format to DD/MM/YYYY as well, but no luck.
def self.get_birth_date
Faker::Date.between(from: '1950/01/01', to: '2001/12/31')
end
Would appreciate, if someone from the group can help. Thanks!
You could use strftime method:
Faker::Date.between(from: '1950/01/01', to: '2001/12/31').strftime("%d/%m/%Y")
It will return "29/05/1969"

TO_TIMESTAMP_TZ Functionality

Any one have any idea how 'TO_TIMESTAMP_TZ' function works internally in oracle.
I want to know how it converts timestamp to timezone
The documentation is very handy for questions like this.
TO_TIMESTAMP_TZ converts a string into a timestamp with timezone information. It doesn't convert something that's already a timestamp into a timestamp with timezone information, without first having a conversion back into a string - which, as I'm sure you're aware, you should always do explicitly.

Comparing dates in Xpath with different formats

I have a date in this format "21-Mar-2014"
I already tried to search for Xpath formulas but with no use
I am getting the first format 21-Mar-2014 using SeleniumIDE and want to check if this value is the date of today,
So how can xpath generate today's date and compare it for the date extracted using Selenium
You can parse the string into a date with an xpath function such as date:date(string) (described in this question).
Today's date already in xml date format is supplied by fn:current-date() (see here)
You can get the value, sore it in a variable, and then compare it. Eg:
storeText //xpath/goes/here myDate
storeEval (new Date(storedVars['myDate']) > new Date()) isAfterToday
Note, the storedVars array holds all the variables.
new Date should parse most formats, otherwise you can use any javascript in the storeEval stage to reformat it.

In ruby on rails Is there a way to dictate the format of fields inserted into the database via Activerecord?

I'm inserting some strings (that look dates) into a sqlite3 database via my ActiveRecord and the database is altering the field and making it look like a date. For example:
"4-2" => "2-Apr" #Not only a date but the order has been reversed.
"6-7-12" => "7/12/2006" #Again not only a date but the order has been changed.
I looked for a ruby string function that would prevent this from happening but haven't been able to come up with anything.
Has anyone else come across something like this and are you aware of any workarounds or fixes.
If you can predict the format of the string, use the following code to parse the string into date object before saving it.
Date.strptime '6-7-12', '%m-%d-%y'
=> Thu, 07 Jun 2012

Activerecord removes all zeros from the timestamp field

I am using Ruby on rails. I am trying to retrieve timestamp column from particular table.
Activerecord returns me the date, if timestamp contains all zeros.
e.g: If timestamp stored is 2010-09-06 00:00:00:000, it returns me 2010-09-06.
But if I have 2010-09-06 00:00:20:000, it returns me in the expected format(i.e: 2010-09-06 00:00:20:000).
This leads to inconsistency. Please help me improve on this.
All suggestions are welcome.
Thanks in Advance.
Sachin
What is the type of the returned object? If that's String, you may manually add missing parts, but if that is a Time object, then you may use a .strftime() method:
t + " 00:00:00.0000" if t =~ /^[-0-9]{10}$/
t.strftime("%Y-%m-%d %H:%M:%S.") + t.usec.to_s

Resources