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
Related
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.
I have a time in GMT, that looks like this:
Wed Feb 13 04:46:54 +0000 2019
In a bash script on MacOS 10.14.3, I want to convert it to the user's local timezone. i.e., this one is 04, so changing it to my timezone would be -6, so it would end up something like this:
Tue Feb 12 22:46:54 CST 2019
Thanks in advance.
Is this what you are looking for ?
date -d "Wed Feb 13 04:46:54 +0000 2019 - 6 hour"
dt= "Wed Feb 13 04:46:54 +0000 2019"
tz="-6"
date -d "$dt $tz hour"
I ended up using something like this:
postYear=`TZ="${timeZone}" date -jf "%Y-%m-%d %H:%M:%S %z" "${postYear}-${postMonth}-${postDay} ${postHourGmt}:${postMinute}:${postSecond} +0000" +%Y`
...where the variables were giving input into the date that I wanted to pull something out of...
...and the year (%Y) was what I was pulling out.
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')
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
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