I need to convert data for all files which have below :
TO_DATE('01/31/1970 12:00:00 AM','MM/DD/YYYY HH:MI:SS AM')
to simple 31-JAN-1970 format using some automation (bash/Linux/UNIX command should do)
You can use construction like this:
TO_CHAR(TO_DATE('01/31/1970 12:00:00 AM','MM/DD/YYYY HH:MI:SS AM'),'DD-MON-YYYY')
BTW consider using POSIX time as this can simplify a lot of operations between dates in shell.
Related
my script is checking for files in several directorys. If there is e.g. an mp3 I get the creation date via
time.ctime(os.path.getctime(audio_file_path))
What I need is an Isorepresentation of the date similar to
datetime.datetime.now().isoformat()
my result at the moment looks like this:
Thu May 28 13:58:45 2020
but what I need is this:
2020-06-03T13:36:48.740664"
Is there an easy way to transform the timestamp?
os.path.getctime returns a timestamp, so you can use:
datetime.datetime.fromtimestamp(os.path.getctime(audio_file_path)).isoformat()
I'm trying to work with a curl request and the following string:
?created_from={FROM_TIMESTAMP}&created_to={TO_TIMESTAMP}
I've been working with the documentation quite a bit, so I know that brackets are not needed. Unfortunately, the only information I have is to us "Gregorian Timestamp" which I haven't found a lot of documentation on.
I have tried a number of different combinations. Using timestamps for 10-01-2018 to 10-02-18. Here's what I've tried:
?created_from=2019-02-01T00:00:00&created_to=2019-02-02T00:00:00)
This just returns logs from the past 30 days, not the specified date range.
Then I tried:
?created_from=1538352000&created_to=1538438400
This returns the following error message: "message":"created_to 63718254228 is more than 2682000 seconds from created_from 1538352000","cause":63718254228 which doesn't make sense to me because the created_from is 1538352000 not 63718254228.
I have also tried a bunch of other syntax and combinations of formats like the following:
?created_from=2018-10-29T00:00:00Z&created_to=2018-11-01T00:00:00Z
And many other tries. Does anyone know how to properly write a Gregorian timestamp in a curl variable request? I've searched everywhere.
Gregorian timestamps have an epoch date of 0-01-01 00:00:00 as opposed to the standard UNIX epoch of 1970-01-01 00:00:00, which as a Gregorian timestamp is 62167219200.
So, you can simply add that to a UNIX timestamp to get the Gregorian value.
For example, consider the datetime 02/24/2019 # 10:17pm (UTC), its UNIX timestamp is 1551046620. To obtain its Gregorian counterpart:
1551046620 + 62167219200 = 63718265820
I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)
This does not work (error included):
user#server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user#server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“
(date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“ = date: invalid date „Do 19 Dez 2013 22:33:28 CET“)
The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!
But how do I do it?
To gerneralize the issue:
If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.
DATE=$(date "+$format1")
date --date="$DATE" "+$format2" # Error in most cases!
This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.
Update:
I have found something like a solution:
# Modify $user_format so it can be parsed later
user_format="$user_format %s"
# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)
# This will only display what $user_format used to be
echo ${output% *}
# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")
This is working and might be helpful to others. So I will share this with you.
Not possible with --date as of GNU coreutils 8.22. From the date manual:
‘-d datestr’
‘--date=datestr’
Display the date and time specified in datestr instead of the current
date and time. datestr can be in almost any common format. It can
contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For
example, --date="2004-02-27 14:19:13.489392193 +0530" specifies the
instant of time that is 489,392,193 nanoseconds after February 27,
2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east
of UTC.
Note: input currently must be in locale independent format. E.g., the
LC_TIME=C below is needed to print back the correct date in many
locales:
date -d "$(LC_TIME=C date)"
http://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date
Note it says that the input format cannot be in a locale-specific format.
There may be other libraries or programs that would recognize more date formats, but for a given date format it would not be difficult to write a short program to convert it to something date recognizes (for example, with Perl or awk).
Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?
The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.
You may use libdatetime-format-flexible-perl.
#!/usr/bin/perl
use DateTime::Format::Flexible;
my $date_str = "So 22 Dez 2013 07:29:35 CET";
$parser = DateTime::Format::Flexible->new;
my $date = $parser->parse_datetime($date_str);
print $date
Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:
printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.
$ export LC_TIME=ru_RU.UTF-8
$ NOW="$(date "+%c")"
$ date --date="$NOW" '+%d.%m.%Y'
22.12.2013
If you meant the formatting is wrong, I think what you want is:
NOW=$(date +%c)
date --date="$NOW" +%d/%m/%Y
note the lowercase %d and %m.
Locally, this is what I get:
root#server2:~# NOW=$(date +%c)
root#server2:~# date --date="$NOW" +%d/%m/%Y
19/12/2013
I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)
This does not work (error included):
user#server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user#server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“
(date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“ = date: invalid date „Do 19 Dez 2013 22:33:28 CET“)
The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!
But how do I do it?
To gerneralize the issue:
If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.
DATE=$(date "+$format1")
date --date="$DATE" "+$format2" # Error in most cases!
This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.
Update:
I have found something like a solution:
# Modify $user_format so it can be parsed later
user_format="$user_format %s"
# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)
# This will only display what $user_format used to be
echo ${output% *}
# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")
This is working and might be helpful to others. So I will share this with you.
Not possible with --date as of GNU coreutils 8.22. From the date manual:
‘-d datestr’
‘--date=datestr’
Display the date and time specified in datestr instead of the current
date and time. datestr can be in almost any common format. It can
contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For
example, --date="2004-02-27 14:19:13.489392193 +0530" specifies the
instant of time that is 489,392,193 nanoseconds after February 27,
2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east
of UTC.
Note: input currently must be in locale independent format. E.g., the
LC_TIME=C below is needed to print back the correct date in many
locales:
date -d "$(LC_TIME=C date)"
http://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date
Note it says that the input format cannot be in a locale-specific format.
There may be other libraries or programs that would recognize more date formats, but for a given date format it would not be difficult to write a short program to convert it to something date recognizes (for example, with Perl or awk).
Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?
The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.
You may use libdatetime-format-flexible-perl.
#!/usr/bin/perl
use DateTime::Format::Flexible;
my $date_str = "So 22 Dez 2013 07:29:35 CET";
$parser = DateTime::Format::Flexible->new;
my $date = $parser->parse_datetime($date_str);
print $date
Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:
printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.
$ export LC_TIME=ru_RU.UTF-8
$ NOW="$(date "+%c")"
$ date --date="$NOW" '+%d.%m.%Y'
22.12.2013
If you meant the formatting is wrong, I think what you want is:
NOW=$(date +%c)
date --date="$NOW" +%d/%m/%Y
note the lowercase %d and %m.
Locally, this is what I get:
root#server2:~# NOW=$(date +%c)
root#server2:~# date --date="$NOW" +%d/%m/%Y
19/12/2013
I have a string of the format
20110724T080000Z
and I want to convert that to local time in a shell script on linux. I thought I simply could give it as input to date, but I don't seem to be able to tell date what format my input date has.
this
date -d "20110724T080000Z" -u
would make date complain
date: invalid date `20110724T080000Z'
Also, what is the format of the form "20110724T080000Z" called? I have had little success trying to google for it.
That's ISO8601 "basic format" for a combined date and time. date does not seem to be able to parse 20110724T080000Z, but if you are prepared to do a few string substitutions it parses 20110724 08:00:00Z correctly.
The date program recognizes yyyy-mm-ddTHH:MM:SS (as well as yyyy-mm-dd HH:MM:SS), so:
a=20110724T080000Z
b=${a:0:4}-${a:4:2}-${a:6:5}:${a:11:2}:${a:13:2}
date +%F_%T -d "${b} +0"
Would print 2011-07-24_12:30:00 in my locale.
its called Zulu time. Its the same as UCT, which used to be referred to as GMT. It's used with the military to specify UCT so there is no confusion on correspondance.
http://en.wikipedia.org/wiki/Date_(Unix)
this command should work according to wikipedia:
date [-u|--utc|--universal] [mmddHHMM[[cc]yy][[.SS]] The only valid option for this form specifies Coordinated Universal Time.
You might try taking advantage of perl:
perl -e 'print scalar localtime(shift), "\n"' 20110724T080000Z
Though u might have to tweak this a little to get it to work correctly. Ok, I don't know exactly why the perl version doesn't do it well, but here is a Ruby version I've tried, though I can't pick the time out well:
ruby -e "require 'date';print Date.strptime('20110724T080000Z','%Y%m%dT%H%M%SZ').ctime"
gives:
Sun Jul 24 00:00:00 2011