-1

What would be the best way to convert this date-time form:

Feb 05 2020   09:26 am
Feb 05 2020   01:55 pm

into

2020-02-05 09:26:00
2020-02-05 13:55:00

I know I can used sed to go through the iterations on the day change to the 2020-02-05 format. I could use:

echo "01:55 pm" | sed -e 's/01:\([0-9]\+\) pm/13:\1:00/g'

to iterate through the file for different times.

Would this be the best way to handle it? I'm opening to using python or another method if it works more cleanly.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Stephen Yorke
  • 197
  • 1
  • 3
  • 13

4 Answers4

2

use the built-in function to parse strings into datetime objects:

from datetime import datetime

s = 'Feb 05 2020   09:26 am'
d = datetime.strptime(s, '%b %d %Y   %I:%M %p')
print(d)
print(type(d))

will print

2020-02-05 09:26:00
<class 'datetime.datetime'>
dh762
  • 2,259
  • 4
  • 25
  • 44
  • You are using `%H`. I believe that this will give the wrong answer for pm despite the use of %p - the `%I` for a 12-hour clock should be used in conjunction with `%p`. – alani Sep 02 '20 at 19:10
  • correct. will delete this post as your answer covers this already – dh762 Sep 02 '20 at 19:11
  • As you wish, but your answer has the interesting observation that the output format is just the usual string representation of the datetime object (no actual need for `strftime` on output), so it would be worth correcting and keeping it because I missed that fact. – alani Sep 02 '20 at 19:14
  • ok I corrected it to not provide wrong answers – dh762 Sep 02 '20 at 19:17
  • 1
    Good. I think it is worth keeping both answers so that the OP can see the fact that `strftime` is not actually necessary here, but nonetheless how to use it if a slightly different output format were needed. – alani Sep 02 '20 at 19:20
2

Could you please try following, written and tested with shown samples.

awk '
BEGIN{
  FS="[ :]"
  num=split("jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec",arr,",")
  for(i=1;i<=num;i++){
    month[arr[i]]=i
  }
}
{
  printf("%d-%02d-%02d %02d:%02d:00\n",$3,month[tolower($1)],$2,$(NF-2)+$NF=="pm" && $(NF-2)<12 ? 12 : 0 ),$(NF-1))
}'  Input_file

Explanation: Adding detailed explanation for above solution.

awk '                            ##Starting awk program from here.
BEGIN{                           ##Starting BEGIN section of this program from here.
  FS="[ :]"                      ##Setting field separator as space and colon here.
  num=split("jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec",arr,",")   ##Creating array arr which has all months names in it.
  for(i=1;i<=num;i++){           ##Starting for loop from 1 to till value of arr length here.
    month[arr[i]]=i              ##Creating month array with index of value of arr with index i and its value is variable i.
  }
}
{
  printf("%d-%02d-%02d %02d:%02d:00\n",$3,month[tolower($1)],$2,$(NF-2)+($NF=="pm" && $(NF-2)<12 ? 12 : 0 ),$(NF-1))   ##Printing 3rd field, month value with first field as index, 2nd field, 2nd last field and adding 12 if last field is pm else do not add anything.
}' 
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2

You can use the conversion functions in datetime (see format codes):

import datetime

in_format = '%b %d %Y   %I:%M %p'
out_format = '%Y-%m-%d %H:%M:%S'
value = 'Feb 05 2020   01:55 pm'

print(datetime.datetime.strptime(value, in_format).strftime(out_format))

Gives:

2020-02-05 13:55:00
alani
  • 12,573
  • 2
  • 13
  • 23
0

You have to use for example the module datetime and the function strptime. You should check the Python reference for strptime:

classmethod datetime.strptime(date_string, format)

Return a datetime corresponding to date_string, parsed according to format.

from datetime import datetime
str1 = 'Feb 05 2020   09:26 am'
str2 = 'Feb 05 2020   01:55 pm'

date_1 = datetime.strptime(str1, "%b %d %Y    %I:%M %p")
date_2 = datetime.strptime(str2, "%b %d %Y    %I:%M %p")

print("date =", date_1)
print("date =", date_2)

Result:

date = 2020-02-05 09:26:00
date = 2020-02-05 13:55:00
Peter Ark
  • 244
  • 1
  • 6
  • 1
    Thanks for all of the possible solutions. I ended up going with the Python one as it will serve me better moving forward. – Stephen Yorke Sep 10 '20 at 18:59