How can verify if a date is in the format dd mon ' yy in Ruby? - ruby

I have a column full of dates in the format dd mon 'yy (Ex: 20 Jan '16). How can I verify that each date value in the column has this same format in Ruby?

You can use Date.strptime which fails with an exception when it cannot parse the date:
require 'date'
Date.strptime("20 Jan '16", "%d %b '%y")
#=> <Date: 2016-01-20 ((2457408j,0s,0n),+0s,2299161j)>
Date.strptime("31 Feb '16", "%d %b '%y")
#=> ArgumentError: invalid date

Related

SH: how to convert the date "Nov 26 23:59:00 2022 GMT" to timestamp?

I want to convert the string "Nov 26 23:59:00 2022 GMT" to a timestamp 1669507140000. I checked the man date to get this command:
date -d "Nov 26 23:59:00 2022 GMT" +"%s"
But his returns: 1669507140 (the last 3 zeros are missing).
Can anyone tell me what I am missing? Thank you.
The version of the date command that I have on my system (YMMV) will show us what it is doing if we add the --debug flag to our invocation...
$ date -d "Nov 26 23:59:00 2022 GMT" +"%s" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.000000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140
The debug log shows us the %s format will result in displaying seconds since epoch. Alternatively, nanosecond precision can be achieved using a modified format string (I added a .001 in the date string to prove it works):
$ date -d "Nov 26 23:59:00.001 2022 GMT" +"%s.%N" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.001000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140.001000000
We can use a pipe to awk to round up to milliseconds for our final form (note I used .0005 in the date string here to show rounding):
$ date -d "Nov 26 23:59:00.0005 2022 GMT" +"%s.%N" | awk '{ printf("%d\n", int($0 * 1000) + (int($0 * 2000) % 2)) }'
1669507140001
One further note.... The following is the version of the date command I am using:
$ date --version
date (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.

Ruby, date converstion 2018 Mar 28 to 2018-03-28

I have a column in a table that list the date of creation of each row, the column show dates like 2018 Mar 28, the date search picker dates are like 2018-03-28
How to convert each of the above format to the other one :
2018 Mar 28 to 2018-03-28
AND
2018-03-28 to 2018 Mar 28
Thanks
Back and forth:
▶ ["2018 Mar 28", "2018-03-28"].zip(["%F", "%Y %b %d"]).
▷ map { |date, format| Date.parse(date).strftime format }
#⇒ ["2018-03-28", "2018 Mar 28"]
All possible formatters for Date#strftime.
This is probably a duplicate question but:
You can use '2018-03-28'.strftime("%Y %b %d")
See:
https://apidock.com/ruby/DateTime/strftime
and
https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-i-strftime
'2018 Mar 28'.to_date.to_s
or Date.parse('2018 Mar 28').to_s should just work in reverse you can do Date.strptime('2018-03-28', '%Y-%m-%d')

Convert epoch time in Ruby (Long string)

Epoch time with leading 3 zeros is returning an invalid year.
Time.at(1520486517000).to_datetime
=> Wed, 19 Apr 50152 19:20:00 +0530
After removing ending 3 zeros, it's returning and valid time stamp.
Time.at(1520486517).utc.to_datetime
=> Thu, 08 Mar 2018 05:21:57 +0000
Is there any way in ruby to churn the epoch time to valid timestamp, when the input has lengthy epoch numbers?
Use DateTime#strptime with "%Q" formatter for parsing milliseconds.
require 'date'
DateTime.strptime 1520486517000.to_s, '%Q'
#⇒ #<DateTime: 2018-03-08T05:21:57+00:00 ((2458186j,19317s,0n),+0s,2299161j)>
I have found the following way to handle the long timestamp.
https://www.ruby-forum.com/topic/85822
Time.at(1520486517000/1000).utc.to_datetime
=> Thu, 08 Mar 2018 05:21:57 +0000

string "Fri Dec 16 16:12:24 CST 2016" to timestamp by shell

I have some string (read from a file) ,such as "Fri Dec 16 16:12:24 CST 2016", and How to convert this string to timestamp, by shell?
I need timestamp,such as Fri Dec 16 16:27:28 CST 2016 -> 1481876854
shell cmd date print Fri Dec 16 16:12:24 CST 2016,so I think date have some method to do it.
but I can't find a solution,Can someone help me?
macos 10.12.2
For the FreeBSD date, you can use the -j flag with format as -f "%a %b %d %T %Z %Y"
date -j -f "%a %b %d %T %Z %Y" "Fri Dec 16 16:27:28 CST 2016" +"%s"
1481876854
If you are using GNU based date command, you can convert to EPOCH timestamp using the -d flag, i.e.
date -d"Fri Dec 16 16:27:28 CST 2016" +%s
1481876854
Based on your spec (time format) and if date doesn't work or is not available, the heavy tool
echo "Fri Dec 16 16:27:28 CST 2016" \
| gawk -F '[[:blank:]:]' 'BEGIN{
split( "Jan Feb Mar Apr May Jun Jul Agu Sep Oct Nov Dec", Temp, " ")
for (t in Temp) Month[ Temp[t]] = t < 10 ? "0" t : t
}
{
# mktime assume ISO C time format: "YYYY MM DD HH MM SS [DST]"
ISOTime = sprintf( "%d %s %s %s %s %s\n", $8, Month[ $2], $3 < 10 ? "0" $3 : $3, $4, $5, $6)
print mktime( ISOTime)
}
'

How to compute a relative date from a given date?

The GNU date command can output date computed from relative items such as -1 hour or 1 month ago. It can also output date from different input format (for instance calendar date, seconds since epoch, etc...).
Is it possible to combine both a date format and a relative time to compute a new date from a given date using the GNU§ date utility?
Something like:
date_epoch=`date "+%s"`
date --date="#$date_epoch -1 month"
The second command gives an "incorrect date" error...
You can do it, sure:
$ date -d"$(date -d#$date_epoch) -1 month" "+%Y-%m-%d"
2013-09-16
Which comes from:
$ date_epoch=$(date "+%s")
$ date -d#$date_epoch
Wed Oct 16 23:46:50 CEST 2013
that you can use it "hardcoded":
$ date -d"Wed Oct 16 23:46:50 CEST 2013 -1 month"
Mon Sep 16 23:46:50 CEST 2013
Or using the variable:
$ date -d"$(date -d#$date_epoch) -1 month"
Mon Sep 16 23:46:50 CEST 2013

Resources