0

I work in a solaris server and the date -d option is not there. No gdate as well.

Here's what I have which works in Linux:

date -d "$hr:$mi $duration minutes" +'%H%M'

where $hr and $mi are hours and mins, $duration is in minutes.

When I run like this in Linux, I get this:

date -d "23:28 60 minutes" +'%H%M'
0028

When I run this in solaris, I get:

date: illegal option -- d Usage: date [-u] ...

Is there an awk solution here since my perl is ancient?

Requote, I'm having the same problem as here, only I don't have the luxury of using date -d. I need a one liner python or perl.

2
  • Do you have ksh93 shell available to you?
    – fpmurphy
    Commented Nov 11, 2017 at 8:42
  • @fpmurphy1 no, I don't have ksh93. Only ksh or bash.
    – mathB
    Commented Nov 11, 2017 at 8:51

3 Answers 3

2

there is a single little difficulty : splitting arround ":", there is an awk function : split(what,where,sep).

I tried in a solaris (it is a one line command)

echo 23:28 60 |
 awk '{split($1,H,":") ; printf "%2d:%02d\n",H[1]+(H[2]+$2)/60, (H[2]+$2)%60 }'

where

  • split($1,H,":") will put 23 in H[1] and 28 in H[2]

use

echo 23:28 60 |
 awk '{split($1,H,":") ; printf "%2d:%02d\n",(H[1]+(H[2]+$2)/60)%24, (H[2]+$2)%60 }'

to stay in 0-23 range for hour. change format string in printf to "%02d:%02d\n" to have a leading 0

3
  • Interesting. I'll check this out when I reach home. I've decided to install Solaris on a VM and try anyway as this was nagging me for a long time today. Thanks!
    – mathB
    Commented Nov 10, 2017 at 15:36
  • This gives me 24:28. I need it to be 00:28. The 60 in the question is not constant, it varies - sometimes it'd be 120 or even 600. In that case, I don't want the result to show over 24 hours.
    – mathB
    Commented Nov 10, 2017 at 17:36
  • That just about does it! Thank you! echo 23:28 60 | awk '{split($1,H,":") ; printf "%2d:%02d\n",(H[1]+(H[2]+$2)/60)%24, (H[2]+$2)%60 }'
    – mathB
    Commented Nov 11, 2017 at 2:12
0

With awk, you could do the following:

awk -v dayt="2017 11 10 23 28 23" '{tmstmp=mktime(dayt)+60;print strftime("%M:%S",tmstmp)}' <<< ""

Pass the FULL date and time to awk with the -v flag and then use the awk mktime to get a number representation of the date/time, adding 60 and then strftime to convert the representation to the required format.

3
  • I'll check this on Monday as I've lost access to my system. Thanks for the answer and have a good weekend!
    – mathB
    Commented Nov 10, 2017 at 15:23
  • I guess I need %H%M in the answer, and this works in Linux, but not in Solaris. I get syntax error: < unexpected
    – mathB
    Commented Nov 11, 2017 at 2:13
  • 1
    1) I am not sure solaris's awk will accept mktime/strftime function, maybe gawk will. 2) you can use a second variable instead fo 60. 3) you can also use echo | ...
    – Archemar
    Commented Nov 11, 2017 at 13:29
0

This is a bit of a experimental hack but works with modern versions of bash. I tested it on CentOS 7 and Fedora 26

#!/bin/bash

hr="23"
mi="28"
duration="60"

printf '%(%H%M)T\n' "$((-(19 * 60 * 60) + (hr * 60 * 60) + (mi * 60) + (duration * 60) ))"

Result is 0028.

3
  • Hmmm. This works on a Ubuntu bash (I get 1058 instead of 0028 but I guess that's timezone thing?), but not on Solaris bash :D No idea why, but I'm getting this error - bash: printf: '(': invalid format character. I've double checked, did not mess up the braces.
    – mathB
    Commented Nov 11, 2017 at 13:37
  • What version of bash on Solaris? Looks like an old version where %T not supported. What version of Solaris?
    – fpmurphy
    Commented Nov 11, 2017 at 15:54
  • Version 10 of Solaris and version 3.2.51(1) of bash
    – mathB
    Commented Nov 11, 2017 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .