Operating System - HP-UX
1833173 Members
3125 Online
110051 Solutions
New Discussion

Re: Convert Any Date Into A 3-digit Decimal Number

 
Suren Selva_1
Advisor

Convert Any Date Into A 3-digit Decimal Number

Hello All,

I need to convert any date into a 3 digit decimal number for scripting purposes.

Is there an HP-UX command that can do that?

Thanks,
Suren S.
Live and let live!
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Convert Any Date Into A 3-digit Decimal Number

Well, if all you need is the current date then

DAYS=$(date '+%j')
echo "Days = ${DAYS}"

will do it.

but if you want to be able to input the date, say in MO DAY YEAR order and the number you desire is a number in the range 1-366 then this should do it:

#!/usr/bin/sh

typeset -i10 MO=${1}
typeset -i10 DA=${2}
typeset -i10 YR=${3}
shift 3
typeset -i10 DAYS=$(($(caljd.sh ${MO} ${DA} ${YR} - $(caljd.sh 1 1 ${YR}) + 1))
echo "Days = ${DAYS}"

You can also get a nice ordered number (though not 3 digits) with
caljd.sh MO DAY YR

This would yield the number of days since 4712 BCE.

Invoke as caljd.sh -u for full usage; you might also choose to simply yank out the cal_jdate function and use it.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Convert Any Date Into A 3-digit Decimal Number

I suppose that if you must convert the output to a leading-zero padded result then a small modification is in order:

typeset -i10 MO=${1}
typeset -i10 DA=${2}
typeset -i10 YR=${3}
shift 3
typeset -i10 DAYS=$(($(caljd.sh ${MO} ${DA} ${YR} - $(caljd.sh 1 1 ${YR}) + 1))
typeset -Z3 ZDAYS=${DAYS}
echo "Days = ${ZDAYS}"
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: Convert Any Date Into A 3-digit Decimal Number

Hi Suren,

Well if all you're looking for is the day of the year in a 3-digit output, the date command alone can do that - as follows:

date +%j

will give you that.
man date for details.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Suren Selva_1
Advisor

Re: Convert Any Date Into A 3-digit Decimal Number

Hi Clay,

My goal is to be able to convert any date into a 3 or 4 digit number.

There is a missing ')' in the script. Any idea where it should be?

When I ran this script by assigning a ) at the end it hung my system.

Thanks,
Suren

Live and let live!
A. Clay Stephenson
Acclaimed Contributor

Re: Convert Any Date Into A 3-digit Decimal Number

typeset -i10 DAYS=$(($(caljd.sh ${MO} ${DA} ${YR}) - $(caljd.sh 1 1 ${YR}) + 1))
typeset -Z3 ZDAYS=${DAYS}


The missing 'paren' was after the first ${YR}, the above should work.

You really need to beter define your problem because it is not at all clear how you want to handle year boundaries.
If it ain't broke, I can fix that.
Jeroen Peereboom
Honored Contributor

Re: Convert Any Date Into A 3-digit Decimal Number

Suren,

if you consider a period of a few years, it is impossible to convert a date to a 3 digit number without having duplicates (two dates having the same 3 digit number (or 4 digits).

It seems to me a minimum requirement that all dates are given a unique number.

So indeed you should refine your question.

JP.