简体   繁体   中英

Busybox date to decode time in a custom format

Using Customized Linux, so I need to use built in tools, cannot install anything. Using BusyBox v1.19.4 date applet.

A log line provides me date in a custom format: "Jun 22 03:49:56 2022" I want to do some calculations with that datetime so I need that "date" understands that info but as "date" requires datetime in specifs format, it's not working.

"date" help shows (the same in busybox website) that it has the option "-D" as follows:

Display time (using +FMT), or set time
.
.
.
        -d,--date TIME  Display TIME, not 'now'
        -D FMT          Use FMT for -d TIME conversion

My understanding is that using "-D" I would define to "date" the order of the datetime input im providing but I could not make it work. Like the command below result in error:

date -d 'Jun 22 03:49:56 2022' -D "+%h %d %H:%m:%S %Y"
date: invalid date 'Jun 22 03:49:56 2022'

Am I misunderstanding the purpose of "-D" or making any sintax error in FMT?

EDIT: Looks like busybox is recognizing the datetime format. I took as that it wouldn't, based on:

Recognized TIME formats:
        hh:mm[:ss]
        [YYYY.]MM.DD-hh:mm[:ss]
        YYYY-MM-DD hh:mm[:ss]
        [[[[[YY]YY]MM]DD]hh]mm[.ss] 

But issuing the below it worked.

date -d 'Jun 22 03:49:56 2022'
Wed Jun 22 03:49:56 BRT 2022

also

date -d 'Jun 22 03:49:56 2022' "+%h %d %H:%M:%S %Y"
Jun 22 03:49:56 2022

Thanks to dan.

EDIT 2: I was mixing hh:mm with strptime convention where minutes is %M not %m. Edited above.

You use:

date -d 'Jun 22 03:49:56 2022' -D "+%h %d %H:%m:%S %Y"

but the -d argument does not begin with + and %m is not minutes.

Try:

date -d 'Jun 22 03:49:56 2022' -D "%h %d %H:%M:%S %Y"

But actually, busybox understands this particular format without using -D at all.

Note that -D is for telling busybox the format of the input date ( -d ), not the format that it should use for output (the +... argument)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM