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

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

Related

Bash / Date / French local env

I need to retrieve the month of a date (dd.month.year) and transform it into the digit YYYY-MM
the source month is in french, so i try :
# date --date="$(printf "01 %s" "January 2020")" +"%Y-%m"
2020-01
# LC_TIME=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
# LANG=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
it works when I use the month in English.
But with the month in French even by forcing the language or local environment parameters it does not recognize the month correctly.
even with date simply it still announces in us :
# LANG=fr_FR date
Sun May 17 23:45:40 CEST 2020
# LC_TIME=fr_FR date
Sun May 17 23:45:48 CEST 2020
# date
Sun 17 May 2020 11:45:53 PM CEST
any idea ?
info :
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-8-amd64
Architecture: x86-64
Although the parsing done by date is flexible and amazing at times, it doesn't handle localization (here and the array is static here). You have to build an translation array that would translate Janvier to January yourself, and then pass the result to date.
It will be easy to use an associative bash array for that:
arr=([Janvier]=January etc)
echo "${arr[Janvier]}"

Email.date formatting

When using email.date.to_s
I receive a date in this format
Wed, 3 Jun 2015 14:57:46 -0700
but I want it to be in this format
06/03/15
You can use strftime to format the string:
Time.now.strftime("%m/%d/%y")
#=> 09/16/15

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)

Ruby date time in string conversion to date time with milliseconds

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"

Create a valid rss date

I want to convert a date like
Sat Dec 31 22:42:58 CET 2011
to a valid rss date which normaly looks like
Mon, 06 Sep 2009 16:45:00 +0000
Seconds should always be 00. Is there anyway to do so? Besides it would be could to find out the current as a valid rss but that's optional ;)
Update
With your new sample input, you don't even need to reformat. It's as simple as
$ time="Sat Dec 31 22:42:58 CET 2011"; date -Rd "$time"
Sat, 31 Dec 2011 13:42:58 -0800
The trick is to reformat your date into a string that date can understand and take as input. In this case it is 2011-12-31 13:37. I'm using awk to do this, but there are number of different utilities that will suffice.
#!/bin/bash
time="12-31-11 13:37"
date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Output
$ time="12-31-11 13:37"; date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Sat, 31 Dec 2011 13:37:00 -0800

Resources