Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Time::JulianDay

Status
Not open for further replies.

chazoid

Technical User
Dec 2, 2002
300
US
Hopefully someone can help me out with this..
I'm trying to use the Time::JulianDay module to get the current Julian day.. not including the year - (ex. today would be 340)
The documentation for the module isn't very clear and I'm kind of confused. I'm not too experienced with using modules. It looks pretty simple but I'm obviously missing something.

the documentation can be found here:


Thanks!
 

maybe I should just explain it this way -
I need to know the number of days since Jan 1 2002.
 
hrm, the slow and painful process would be the many many ifs.

if($month = 2) { $monthcount += 31; }
if($month = 3) { $monthcount += 58; }

just a thought if you get stuck without hope.
sorry i cannot help further. Happieness is like peeing your pants.
Everyone can see it, but only you can feel the warmth.
 
Thanks Zas...
Here's what I ended up doing -

Code:
use Time::JulianDay;

$difference = 2452275; 
$seconds_since_1970 = time();
$jd = local_julian_day($seconds_since_1970);
$jd = $jd - $difference;
print $jd;

local_julian_day gives you the number of days since some time in the 1300's or something.. so I subtracted 341 from that and it seems to work.. I don't like it, but I guess it's good enough. I'll have to change it when we hit 2003, which is exactly when this program needs to run, so i hope i don't forget! (I better put a conditional in there instead)



 
#!/usr/local/perl/bin/perl

# if you are on unix: the comand 'date +%j' will do the same
# in 'c' the member 'wday' of 'struct tm' will contain this value
# when you have to calculate it, simulate a 'switch'

printf("%d\n",daysyear(7,12,2002));
exit(0);

sub daysyear
{

# assumed jan = 0, dec = 11

my($day,$mon,$year) = @_;

$day += 30 if($mon >10);
$day += 31 if($mon >9);
$day += 30 if($mon >8);
$day += 31 if($mon >7);
$day += 31 if($mon >6);
$day += 30 if($mon >5);
$day += 31 if($mon >4);
$day += 30 if($mon >3);
$day += 31 if($mon >2);

$day += 28+(!($year % 4) &&
($year % 100) || !($year % 400)) if($mon >1);

$day += 31 if($mon);

return($day);
}
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
IGNORE PREVIOUS POST, this version is correct !

#!/usr/local/perl/bin/perl

# if you are on unix: the comand 'date +%j' will do the same
# in 'c' the member 'wday' of 'struct tm' will contain this value
# when you have to calculate it, simulate a 'switch'

printf("%d\n",daysyear(7,12,2002));
exit(0);

sub daysyear
{

# assumed jan = 1, dec = 12

my($day,$mon,$year) = @_;

$day += 30 if($mon >11); # add nov days
$day += 31 if($mon >10); # add oct days
$day += 30 if($mon >9); # add sep days
$day += 31 if($mon >8); # add aug days
$day += 31 if($mon >7); # add jul days
$day += 30 if($mon >6); # add jun days
$day += 31 if($mon >5); # add mai days
$day += 30 if($mon >4); # add apr days
$day += 31 if($mon >3); # add mar days

$day += 28+(!($year % 4) &&
($year % 100) || !($year % 400)) if($mon >2);

$day += 31 if($mon >1); # add jan days

return($day);
}
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Do you have to use Time::JulianDay?
You can do this will Date::Calc fairly succinctly.
Code:
use Date::Calc qw(Delta_Days);

my ($year1, $month1, $day1) = (2002, 1, 1);
my ($year2, $month2, $day2) = (2002, 12, 7);

my $dd = Delta_Days($year1, $month1, $day1, $year2, $month2, $day2);
jaa
 
Thanks jamisar.. I do have the option of running this on a unix machine, so i'll try it out..
Thanks guys for your help!
 
Hey justice.. somehow I missed your post when I sent that last reply. That should work too..
I just went back to the script I was originally working on and started playing around with the localtime function and found this method accidentally (then it occurred to me to check perldoc, duh) - would there be any reason not to do it this way? I'm using this for a file time/date stamping routine.

@arrTime = localtime();

$day_of_year = $arrTime[7] + 1;

print "Day of year - $day_of_year\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top