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 am processing a file and filtering out certain fields from it. Then, I am converting the whole result to a CSV file. However, the last column that I am filtering out is the epoch timestamp (upto millisecond).
Searching around, I found that I can use the strftime function in jq for conversion, however; when applying the | strftime inside the whole array, it applies the function to all the values.
TZ='Asia/Kolkata' grep $id ~/hhfh.log | grep -oE '\{.+' | jq -r '[.highPrice, .lowPrice, .openPrice, .closePrice, .volumeTradedToday, .totalBuyQuantity, .totalSellQuantity, .tickTimestamp | strftime("%B %d %Y %I:%M%p %Z")]' | jq -r 'map(tostring) | join(", ")'
Can I modify this so that strftime("%B %d %Y %I:%M%p %Z") only applies to the .tickTimestmap value? If not, Would I have to depend on awk?
Log lines from hhfh.log:
2018-03-06 03:30:04,938 DEBUG KiteTickerSourceLogic [ReadingThread] TickData: {"mode":"full","tradable":false,"instrumentToken":2997505,"lastTradedPrice":740.0,"highPrice":0.0,"lowPrice":0.0,"openPrice":731.5,"closePrice":739.9,"change":0.01351533991080183,"lastTradedQuantity":23.0,"averageTradePrice":0.0,"volumeTradedToday":12.0,"totalBuyQuantity":285.0,"totalSellQuantity":1469.0,"lastTradedTime":1520245282000,"oi":0.0,"tickTimestamp":1520307004000,"openInterestDayHigh":0.0,"openInterestDayLow":0.0,"marketDepth":{"buy":[{"quantity":1,"price":735.55,"orders":1},{"quantity":86,"price":731.5,"orders":1},{"quantity":168,"price":731.0,"orders":1},{"quantity":25,"price":730.1,"orders":1},{"quantity":0,"price":0.0,"orders":0}],"sell":[{"quantity":550,"price":743.6,"orders":1},{"quantity":550,"price":746.6,"orders":1},{"quantity":10,"price":750.0,"orders":1},{"quantity":25,"price":777.0,"orders":1},{"quantity":12,"price":-0.01,"orders":1}]}}
What I am currently generating:
January 01 1970 12:05:08AM UTC, January 01 1970 12:05:02AM UTC, January 01 1970 12:05:04AM UTC, January 01 1970 12:05:06AM UTC, January 19 1970 08:56:53AM UTC, January 01 1970 04:39:20AM UTC, January 01 1970 12:00:00AM UTC, August 31 50144 10:33:20AM UTC
What it should be like:
751.95, 734.1, 745.45, 742.8, 1659987, 6358, 0, <time in IST zone>
Simply use parentheses:
(.tickTimestamp | strftime("%B %d %Y %I:%M%p %Z"))
If you want the time relative to TZ, use strflocaltime, which however requires a more recent version of jq than 1.5.
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
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