Actually, ignore all this completely, and don't bother trying to get the date from the job list at all. You don't need to, unless you need a pinpoint accurate printjob timestamp (and even then, it is not immediately apparent from the output or the manual of lpstat whether the given datetime is of when the job was sent or when it was completed).
You don't need to do it at all because you can just generate a datestamp of 'now' at the moment you detect your job in the "completed" jobs list.
._. printJob=$(lp somefile | awk '{print $4}')
._. printJobCompleted=$(lpstat -W "completed" | grep $printJob )
._. printedDatestamp=$(date +%s)
Though of course you can't tell the job is completed till it is. So you might want to loop and sleep till you detect it is.
If you do want the date that's in the queue, perhaps do something like this:
Capture your print job id when you send the instruction to print:
._. printJob=$(lp somefile | awk '{print $4}')
._. echo $printJob
Kyocera-ECOSYS-P5021cdw-564
(It's necessary to parse lp's output through awk because it seems lp has no option to simply return the print job):
._. lp somefile
request id is Kyocera-ECOSYS-P5021cdw-568 (1 file(s))
(awk '{print $4}' get's the fourth word of that output string).
Then use lpstat to get the completed job details:
._. lpstat -W "completed" | grep $printJob
Kyocera-ECOSYS-P5021cdw-564 ming 2048 Sun 18 Apr 2021 11:20:56 BST
Isolating that part of it that includes the date is a bit awkward, because lpstat seems to offer now direct way to do it, and the date consists of multiple words. So perhaps either:
._. printJobDateString=$(lpstat -W "completed" | grep $printJob | awk '{print $4,$5,$6,$7,$8,$9}')
._. echo $printJobDateString
Sun 18 Apr 2021 11:20:56 BST
(And neither does awk seem to offer of way of capturing a range - please correct me if I am wrong).
... or do the same with the shell built-in read, which, for reasons I do not know, does treat the whole date string as a single word (the fourth word in the output string, where read marks the first four words with the names a,b,c,d):
._. printJobDateString=$(lpstat -W "completed" | grep $printJob | while read a b c d; do echo "$d"; done)
._. echo $printJobDateString
Sun 18 Apr 2021 11:20:56 BST
Finally, perhaps turn it into a seconds-since-epoch datestamp, for ease of comparison:
._. printedDatestamp=$(date --date="$printJobDateString" +%s)
._. echo $printedDatestamp
1618741256
printJobDateString will be empty if lpstat had no record of the job in its completed list. In that case, date will assume it to be 1/1/1970 when you convert it to epoch seconds.
Actually though, it might be quite unlikely that your print job does not end up in the "completed" jobs list, because that list seems to collect cancelled jobs as well as completed jobs. So ignore the warning below. Ha.
Beware though, if lpstat doesn't have your print job, it will return an empty string.
And if you feed 'date' an empty string, it will spit out today's date.
And if you have that in a script, it might mislead you into thinking the job has completed, because it will give you today's date.
For example:
._. date -d @$(date --date="" +%s) +"%F %T %z"
2021-04-17 00:00:00 +0100
Whereas, 'date' takes 0 to be the start of the Unix 'epoch' (the dawn of groovy time) - 1 January 1970:
._. date -d @0 +"%F %T %z"
1970-01-01 01:00:00 +0100
This might more usefully indicate a non-completed job than today's date. So turn the empty string to a zero (if you get one) before feeding it to 'date', perhaps like this:
._. emptyString=""
._. zero=$( [[ -z "$emptyString" ]] && echo 0 || echo "$emptyString" )
._. echo zero
0
In other words, generate the datestamp like this:
._. printedDatestamp=$(date --date="$( [[ -z "$printJobDateString" ]] && echo 0 || echo "$printJobDateString" )" +%s)
Finally, all the steps together:
._. printJob=$(lp -P "1-4" -o fit-to-page -o number-up=2 -o sides=two-sided-short-edge -o KCEcoprint=On "$fn" | awk '{print $4}')
._. printJobDateString=$(lpstat -W "completed" | grep $printJob | while read a b c d; do echo "$d"; done)
._. printedDatestamp=$(date --date="$( [[ -z "$printJobDateString" ]] && echo 0 || echo "$printJobDateString" )" +%s)
P.S.
Use sleep as well, after sending the print job, in a loop that keeps going till you get an empty string, by grepping your printJob over the output of lpstat -W "not-completed". Only then poll lpstat -W "completed".
There: the timestamp of a print job. And who was it who said that linux print control was humane? Nobody. Nobody said any such thing. And anything they did say about it they muttered under their breath.