LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2006, 03:16 PM   #1
Blackout_08
Member
 
Registered: Jan 2004
Posts: 35

Rep: Reputation: 15
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"
 
Old 06-07-2006, 03:23 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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
 
Old 06-07-2006, 05:24 PM   #3
Blackout_08
Member
 
Registered: Jan 2004
Posts: 35

Original Poster
Rep: Reputation: 15
I tried

date -d 20060631 +%j

and it still gave me a date. Anyway to generate an error?
 
Old 06-08-2006, 07:08 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 06-08-2006, 02:04 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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
 
Old 06-08-2006, 02:08 PM   #6
Blackout_08
Member
 
Registered: Jan 2004
Posts: 35

Original Poster
Rep: Reputation: 15
version

date (coreutils) 4.5.8
 
Old 06-08-2006, 02:16 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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..]
 
Old 06-08-2006, 02:18 PM   #8
Blackout_08
Member
 
Registered: Jan 2004
Posts: 35

Original Poster
Rep: Reputation: 15
Got ne other suggestions?? My friend suggested using cal month year | grep day but i don't know how to use it effectively
 
Old 06-08-2006, 02:57 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 06-08-2006, 05:56 PM   #10
Blackout_08
Member
 
Registered: Jan 2004
Posts: 35

Original Poster
Rep: Reputation: 15
jinkel ure idea works but only after yr 1910. i need it to go till 0001
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cal and the country code ekdya Debian 1 04-14-2006 08:13 PM
"cal" command question tanawana Debian 2 10-13-2005 04:25 AM
unix cal command in an odd situation GUIPenguin General 5 10-12-2005 04:10 PM
seeking clarification- Samba CAL License usage with W2K AD Domain network environment integr8er Linux - Enterprise 7 10-06-2004 05:50 PM
just arrived Cal state last week shoot2kill General 7 03-27-2003 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration