LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Cal command (https://www.linuxquestions.org/questions/programming-9/cal-command-452518/)

Blackout_08 06-07-2006 03:16 PM

Cal command
 
I was wondering if there is a possible way to figure out the julian date for a specific day. For example September 2nd 1984 is the 246th day. I want to be able to capture and use the 246 number. Also I have been using If statements (a ton of them) to figure out if a date is valid. Curious if there is a quick way. Im using /bin/bash. Here is a sample of my code If there a quicker way of figuring this out. Thanks

if [ $YR -gt `date +%Y` ]
then
echo "You entered $BIRTHDATE, which is a future date, please enter your birthdate in the form YYYYMMDD"
elif [ $MTH -gt "12" ] || [ $MTH -eq 0 ] || [ $YR -eq 0 ] || [ $DAY -eq 0 ]
then
echo "You entered $BIRTHDATE, which is an invalid date. Please enter your birthdate in the form YYYYMMDD"

david_ross 06-07-2006 03:23 PM

Why not just check for the Julian day number and the return code from the date command:
Code:

rossy - trigger - Wed Jun 07 21:24:32
~> date -d 20060607 +%j
158
rossy - trigger - Wed Jun 07 21:24:41
~> echo $?
0
rossy - trigger - Wed Jun 07 21:24:44
~> date -d 20060631 +%j
date: invalid date `20060631'
rossy - trigger - Wed Jun 07 21:24:50
~> echo $?
1


Blackout_08 06-07-2006 05:24 PM

I tried

date -d 20060631 +%j

and it still gave me a date. Anyway to generate an error?

jlinkels 06-08-2006 07:08 AM

My version of date does the same, that is it doesn't return an error in case of an invalid date. However, if you do this:

Code:

mydate="20060632"
systemdate=`date -d "mydate" %Y%m%d`
if [ "$systemdate" != "$mydate"]
  bla bla

What I mean is, if you feed an invalid date to "date", it will return a valid date anyway. When the two do match, the date apparently was valid.

(I am sure this can be programmed in a more efficient way, but I am not good at it)

jlinkels

david_ross 06-08-2006 02:04 PM

I'd be interested to know what version you are using because it seems very odd behaviour for it to return another value - I'm using slack current with:
rossy - trigger - Thu Jun 08 20:06:38
~> date --version
date (GNU coreutils) 5.96

Blackout_08 06-08-2006 02:08 PM

version

date (coreutils) 4.5.8

Hko 06-08-2006 02:16 PM

Quote:

Originally Posted by Blackout_08
I tried

date -d 20060631 +%j

and it still gave me a date. Anyway to generate an error?

My version responds correctly:
Code:

$ date -d 19780319 +%j
078

$ date -d 20060631 +%j
date: invalid date `20060631'

$ date --version
date (GNU coreutils) 5.94
[..snip copyright blurp..]


Blackout_08 06-08-2006 02:18 PM

Got ne other suggestions?? My friend suggested using cal month year | grep day but i don't know how to use it effectively

jlinkels 06-08-2006 02:57 PM

jlinkels@jlinkels_lt:~$ date --version
date (coreutils) 5.2.1
Written by David MacKenzie.

jlinkels@jlinkels_lt:~$ date -d 20060632
Sun Jul 2 00:00:00 CEST 2006

Weird uh?

Blackout, doesn't my suggestion work then? You check whether the dat is valid, and then you output the julian date?

jlinkels

Blackout_08 06-08-2006 05:56 PM

jinkel ure idea works but only after yr 1910. i need it to go till 0001


All times are GMT -5. The time now is 11:19 AM.