Convert string Fri, 04 Aug 2017 13:07:44 into 04-08-2017 in unix - unix

I would like to convert the date " Fri, 04 Aug 2017 13:07:44 GMT " into 04-08-2017 .
And code should recursively convert all string format into specified format.

You may use the -d option with date command.
Execute this in your command line to get the desired output.
date -d 'Fri, 04 Aug 2017 13:07:44 GMT' +'%d-%m-%Y'
Edit :
If you want to populate it in a script you may use something like this:
#!/bin/bash
input_date="Fri, 04 Aug 2017 13:07:44 GMT"
output_date=$(date -d "$input_date" +'%d-%m-%Y')
echo $output_date

Related

how get this specific format of date in pre-request of postman

I have this in c#:
var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
and the result is like this:
date = "Tue, 27 Dec 2022 13:30:35 GMT";
I want to have this result in pre-request of postman to pass this variable as date.
But this command doesn't give me the exact result:
var date = new Date();
//result: Tue Dec 27 2022 16:26:00 GMT+0100 (Central European Standard Time)
As I'm using this date variable for encryption, it's important to have it in the special format I have in c#.
Do you have any idea how can I have this result in postman?
To display time, you can use momentjs, that's already included in postman. The cons is it doesn't support timezone, so the code would be:
const moment = require('moment')
let datetime = moment().format("ddd, DD MMM YYYY HH:mm:ss ") + "GMT"
//Wed, 28 Dec 2022 08:08:36 GMT
Using reg expression in pre-request section
var date = new Date();
// Tue Dec 27 2022 12:10:39 GMT-0500 (Eastern Standard Time)
console.log(date);
let match = /(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})/.exec(date);
// 0: "Tue Dec 27 2022 12:10:39 GMT"
// 1: "Tue"
// 2: "Dec"
// 3: "27"
// 4: "2022"
// 5: "12"
// 6: "10"
// 7: "39"
// 8: "GMT"
// newDate = "Tue, 27 Dec 2022 13:30:39 GMT";
newDate = `${match[1]}, ${match[3]} ${match[2]} ${match[4]} ${match[5]}:${match[6]}:${match[7]} ${match[8]}`
console.log(newDate);
Result in console
Tue Dec 27 2022 12:22:39 GMT-0500 (Eastern Standard Time)
Tue, 27 Dec 2022 12:22:39 GMT
Test string set in https://regex101.com/
Regular Expression
(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})
In Reg Expression Visualization https://regexper.com/

Parsing timestamp in BigQuery with two different timezones

I'm having trouble parsing this datetime field in BigQuery that takes in two different time zone formats.
It is stored as a string. The timestamps look like either of these.
DateTime:
Thu Mar 03 2022 18:18:38 GMT+0000 (GMT)
Thu Mar 03 2022 00:04:32 GMT-0800 (Pacific Standard Time)
What I want:
DateTime:
Thu Mar 03 2022 10:18:38 GMT-0800 (Pacific Standard Time)
Thu Mar 03 2022 00:04:32 GMT-0800 (Pacific Standard Time)
I get errors with datetime and parse_timestamp trying to do something like this:
parse_timestamp("%a %b %d %E4Y %T", DateTime , 'US/Pacific')
How can I get this to work?
Consider below options
select *,
datetime(parse_timestamp("%a %b %d %Y %T GMT%Z", regexp_replace(DateTime, r' \([ \w]+\)', '')), 'US/Pacific'),
format_datetime("%a %b %d %Y %T", datetime(parse_timestamp("%a %b %d %Y %T GMT%Z", regexp_replace(DateTime, r' \([ \w]+\)', '')), 'US/Pacific')),
from your_table
if applied to sample data in your question - output is

How to Extract logs between 2 timestamps in Unix

I need to extract the logs between two timestamps from a file in Unix. I basically kind of need those logs to be copied and output in a different file so that I can copy them.
Is there an efficient way to do this? The log format looks like this - The timestamp is in a separate line from the actual logs.
Tue 21 Apr 14:00:00 GMT 2020
{"items":[{"cpu.load": "0.94","total.memory": "6039.798 MB","free.memory": "4367.152 MB","used.memory": "1672.646 MB","total.physical.system.memory": "16.656 GB","total.free.physical.system.memory": "3860.197 MB","total.used.physical.system.memory": "12.796 GB","number.of.cpus": "8"}]}
Tue 21 Apr 18:00:00 GMT 2020
{"items":[{"cpu.load": "0.76","total.memory": "6039.798 MB","free.memory": "4352.656 MB","used.memory": "1687.142 MB","total.physical.system.memory": "16.656 GB","total.free.physical.system.memory": "3858.203 MB","total.used.physical.system.memory": "12.798 GB","number.of.cpus": "8"}]}
I am doing this but it only prints out the timestamp and not the actual logs
cat file.txt | awk -F, '{ if ($1>"Fri 21 Aug 14:00:00 GMT 2020" && $1<"Sat 22 Aug 18:00:00 GMT 2020") print }'
Can someone advice.

Formatting bootstrap-datepicker result

So here is how i setup my datepicker
$('#picker').datepicker({
format: 'yyyy-mm-dd',
multidate: true
});
and then here is how i showing the result
$('#picker').on('changeDate', function(event) {
console.log( $('#picker').datepicker("getDates"));
});
here is the result from console
[Tue Jun 02 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 10 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 17 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat)]
How can i make it to yyyy-mm-dd format ?
So here is how i fix it
$('#picker').datepicker("getFormattedDate")
maybe it will help others
Give moment.js a try. It is a very powerful tool not just only to format Date objects, but to manipulate them and make custom validations.

Changing a string to datetime format in where clause for comparison

I have a dataset that looks like this:
datetime count
18:28:20.602 UTC DEC 08 2016 1
20:42:32.017 UTC DEC 08 2016 5
15:33:40.691 UTC DEC 08 2016 1
17:11:54.008 UTC DEC 08 2016 3
20:28:57.861 UTC DEC 08 2016 0
.
.
.
.
The datetime column is in the string format. I'm having difficulty in converting it to a timestamp.
How do I write a Impala/Hive query so that I get the data between '18:28:00.000 UTC DEC 08 2016' to '18:33:00.000 UTC DEC 08 2016'
With Hive:
cast(from_unixtime(unix_timestamp(SHITTY_FORMAT, 'HH:mm:ss.SSS zzz MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, then into String standard format (in local timezone because that's the Hive convention), then into a Timestamp.
There is no easier way, unfortunately. And you may have some edge cases because of the 1h overlap in summer/winter times.
Source: the Hive documentation, of course...
With Impala (which does not support the zzz format modifier):
cast(from_unixtime(unix_timestamp(regexp_replace(SHITTY_FORMAT, ' UTC ', ' '), 'HH:mm:ss.SSS MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, assuming that all your inputs are in UTC, then into String standard format (in UTC timezone because that's the Impala convention), then into a Timestamp.

Resources