Linux Bash - Parse Date in Custom Format

linux bash - Parse date in custom format

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.

Parse non standard date format

Here is what the man date page says about format for the --date option:

The --date=STRING is a mostly free format human readable date string such as
"Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next
Thursday". A date string may contain items indicating calendar date, time of day,
time zone, day of week, relative time, relative date, and numbers. An empty
string indicates the beginning of the day. The date string format is more
complex than is easily documented here but is fully described in the info
documentation.

Hence you can use, for example:

date --date "2014-06-12 09:03 UTC" --utc +%d.%m.%Y,\ %H:%M\ UTC
# Output: 12.06.2014, 09:03 UTC

to get what you desire.

You could get this second form easily from your first output with a sed line as follows:

sed 's/\([0-9]\{2\}\)\.\([0-9]\{2\}\)\.\([0-9]\{4\}\), \(.*\)/\3-\2-\1 \4/'
<<< '12.06.2014, 09:03 UTC'
# Output: 2014-06-12 09:03 UTC

Note that it would probably be faster to output date at ISO 8601 format in the first time for reuse, e.g. with:

date --utc +%F\ %H:%M\ UTC
# Output: 2014-06-12 10:12 UTC

Parsing date and time format - Bash

You don't need to call out to awk to date at all, use bash's substring operations

d="20141105 11:30:00"
yr=${d:0:4}
mo=${d:4:2}
dy=${d:6:2}
hr=${d:9:2}
mi=${d:12:2}
printf -v dir "/bla/%s/%s/%s/%s/%s/\n" $yr $mo $dy $hr $mi
echo "$dir"
/bla/2014/11/05/11/30/

Or directly, without all the variables.

printf -v dir "/bla/%s/%s/%s/%s/%s/\n" ${d:0:4} ${d:4:2} ${d:6:2} ${d:9:2} ${d:12:2}

Given your log file:

while read -r date time file; do
d="$date $time"
printf -v dir "/bla/%s/%s/%s/%s/%s/\n" ${d:0:4} ${d:4:2} ${d:6:2} ${d:9:2} ${d:12:2}
mkdir -p "$dir"
mv "$file" "$dir"
done < filename

or, making a big assumption that there are no whitespace or globbing characters in your filenames:

sed -r 's#(....)(..)(..) (..):(..):.. (.*)#mv \6 /blah/\1/\2/\3/\4/\5#' | sh

Convert date formats in bash

#since this was yesterday
date -dyesterday +%Y%m%d

#more precise, and more recommended
date -d'27 JUN 2011' +%Y%m%d

#assuming this is similar to yesterdays `date` question from you
#http://stackoverflow.com/q/6497525/638649
date -d'last-monday' +%Y%m%d

#going on @seth's comment you could do this
DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d

#or a method to read it from stdin
read -p " Get date >> " DATE; printf " AS YYYYMMDD format >> %s" `date
-d"$DATE" +%Y%m%d`

#which then outputs the following:
#Get date >> 27 june 2011
#AS YYYYMMDD format >> 20110627

#if you really want to use awk
echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash

#note | bash just redirects awk's output to the shell to be executed
#FS is field separator, in this case you can use $0 to print the line
#But this is useful if you have more than one date on a line

More on Dates

note this only works on GNU date

I have read that:

Solaris version of date, which is unable
to support -d can be resolve with
replacing sunfreeware.com version of
date

Convert string to date in bash

This worked for me :

date -d '20121212 7 days'
date -d '12-DEC-2012 7 days'
date -d '2012-12-12 7 days'
date -d '2012-12-12 4:10:10PM 7 days'
date -d '2012-12-12 16:10:55 7 days'

then you can format output adding parameter '+%Y%m%d'

Linux shell - parse date in odd formats

Convert date with GNU date and bash:

d="Jan, 4, 2021, 1:20:30 PM"

IFS=',:' read mon day year hour min sec a <<<"$d"
date -d "$mon $day $year $hour:$min:$sec $a" '+%Y-%m-%d %H:%M:%S'

Output:


2021-01-04 13:20:30

YYYY-MM-DD format date in shell script

In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date).

As such:

# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1

# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1

# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal

In bash (<4.2):

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')

# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')

# print current date directly
echo $(date '+%Y-%m-%d')

Other available date formats can be viewed from the date man pages (for external non-bash specific command):

man date

How to parse a date that has been formatted with different locale

If you have Python and a de locale installed, one way would be to call

% python /path/to/convert.py "08 Dez 2015"
2015-12-08 00:00:00

where convert.py is this python script:

import sys, locale, datetime as DT
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
print(DT.datetime.strptime(sys.argv[1], '%d %b %Y'))

Formatting a Go date in a bash-friendly manner

Unfortunately you are out of luck.

The parameter value passed to the template execution is of type imageOutputParams, which is declared in images.go:

type imageOutputParams struct {
Tag string
ID string
Name string
Digest string
CreatedAt string
Size string
}

As you can see, the CreatedAt field is of type string, not a time.Time, so you can't call time.Time methods on it. Neither can you do any useful date/time processing on it without custom registered functions. But since you're just supplying the template text, you can't register custom functions either.

The template you pass is executed in function outputUsingTemplate() like this:

err = tmpl.Execute(os.Stdout, params)

Where params is a value of the above mentioned struct.

Recommend the project owners to add a new field holding the CreatedAt timestamp as a time.Time value, so you can get the epoch seconds using Time.Unix(). The template would look like this then:

{{.CreatedAtTime.Unix}}


Related Topics



Leave a reply



Submit