Ruby date time in string conversion to date time with milliseconds - ruby

This is a Ruby question (1.9.1)
I have the following date and time in a string:
29 Sep 2013 12:25:00.367
I first want to convert it from string to date and time and then
add 10 seconds to it and convert it back to the same string format as
above.
I wrote this code:
format = "%d %b %Y %H:%M:%S"
date_time = "29 Sep 2013 22:11:30.195"
parsed_time = DateTime.strptime(date_time, format)
puts " new date time is #{parsed_time}"
Which outputs:
new date time is 2013-09-29T22:11:30+00:00
I did not see "195". I tried format = "%d %b %Y %H:%M:%S.%3N" and this gives:
fileOpTest:34:in `strptime': invalid date (ArgumentError) from fileOpTest:34:in `<main>'

This can be done very easily using the Time class. You can add to a Time by adding seconds. Then use #strftime
t= Time.parse('29 Sep 2013 12:25:00.367')
=> 2013-09-29 12:25:00 -0400
t=t + 10
=> 2013-09-29 12:25:10 -0400
t.strftime("%d %b %Y %H:%M:%S.%3N")
=> "29 Sep 2013 12:25:10.367"

Related

'Date.parse("05/11/19")' parses in YY/MM/DD?

I have a date in string format "05/11/19" I used Date.parse, but when I use strftime("%d %b %Y"), it parsed the date as 19 Nov 2005. Is there another way to parse my string to date without messing up the day and year fields?
You can pass a format String when parsing:
require 'date'
datestring = '05/11/19' # 05 Nov 2019
date = Date.strptime(datestring, '%d/%m/%y')
puts date.strftime('%d %b %Y') # => 05 Nov 2019

Convert human readable time into EPOCH using shell script

I have a human readable time as
08-18-2016 09:18:25
I want it to be converted into epoch time using shell script.
I tried with date "+%s" but I am getting the error
date: invalid date `08-18-2016 09:32:42'
The canonical way to convert a datetime into epoch is to use:
date "+%s" # for this moment's date
date -d" some date" "+%s" # for a specific date
However, in this case the format is not valid:
$ date -d"08 18 2016 09:18:25" "+%s"
date: invalid date ‘08 18 2016 09:18:25’
You need, then, to massage the string a bit before passing it to date -d.
This converts the two first spaces into slashes:
$ sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25"
08/18/2016 09:18:25
So this works:
$ date -d"$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")" "+%s"
1471504705
Or using variables:
$ nice_date=$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")
$ date -d"$nice_date" "+%s"
1471504705
Thanks for the explanation fedorqui. But 1471511905 is the epoch time
for 08 18 2016 09:18:25, not 1471504705. – Mohit Rane
date -u … will print Coordinated Universal Time.

Ruby Time.strptime vs Date.strptime

When parsing "some dates" (in Ruby) with Time.strptime and Date.strptime have different behaviour.
For example if we try to convert "30 Feb" (a date that does not exist), we have:
Time.strptime('30 Feb 2015', '%d %b %Y') # will result in this date: 2015-03-02
Date.strptime('30 Feb 2015', '%d %b %Y') # ArgumentError: invalid date
At the same time trying to parse "32 Feb" results in error for both classes.
Time.strptime('32 Feb 2015', '%d %b %Y') # ArgumentError: invalid strptime format - `%d %b %Y'
Date.strptime('32 Feb 2015', '%d %b %Y') # ArgumentError: invalid date
What is the reason for the different behaviour? Why Time "attempts to adjust" an invalid date?
OK, I did a bit more digging and found a couple of "issues" reported on this topic: https://bugs.ruby-lang.org/issues/9549 and the main one: https://bugs.ruby-lang.org/issues/10588
It seams that Time behaves this way for a reason. As Akira Tanaka says:
Invalid date/time is difficult to determine.
It is almost impossible by application because it depends various factors:
Month, leap year, summer time, leap seconds, time zone definition change.
Sometimes application needs a Time object near given year/month/day/hour/minute/second.
So Time tries to compensate possible 'almost correct' times. That's why it successfully parses this:
Time.strptime('29 Mar 2015 3:30:00 +02000', '%d %b %Y %T %z') to
2015-03-29 04:30:00 +0300 (3:30 is invalid time on 29th March 2015 because of daylight saving time, from 3:00 the clock moves to 4:00)

Bash How to format a date

I want to grep a file with the following date format:
Thu Apr 24
At the moment I only have date +"%d %m %Y" and that's returning 24 04 2014.
How do I format to get "Thu Apr 24"?
So I need the day month and date?
man date would suggest date +"%a %b %d"
You can try
date +"%a %b %d"
where
%a locale's abbreviated weekday name (e.g., Sun)
%b locale's abbreviated month name (e.g., Jan)
%d day of month (e.g., 01)

Is there Shell script date Format (dd month year time) (29 Oct 2013 05:26:30)

I want the date format in shell script as (dd month year time)
Example:
29 Oct 2013 05:26:30
Please can anyone help me in solving this.
Like this:
$ date "+%d %b %Y %T"
29 Oct 2013 10:45:08
From man date:
%d day of month (e.g., 01)
%b locale's abbreviated month name (e.g., Jan)
%Y year
%T time; same as %H:%M:%S

Resources