Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Julian Date Calculator from YYYY-MM-DD

Ramnarayan (Programmer) (OP)
13 Jan 06 10:19
Hi,

I am trying to write a script to calculate the Julian date of the year from the given string which is of the format "YYYY-MM-DD". Can someone help me out here. I am at a lost after going through all the various Date::Calc modules and nothing seems to work for me. Can someone be kind enough to help me out in this!

Thanks very much
audiopro (Programmer)
13 Jan 06 11:10
I too failed to find a Perl solution but I got round the problem by populating a MySQL database with all the date details for the previous 5 years and the next 14 years.
Using MySQL queries allows any date calculations to be carried out.
A flat file would also work but would require more processing.
Just a suggestion which works for me.

Keith
www.studiosoft.co.uk

icrf (Programmer)
13 Jan 06 23:59
Really? A simple google turned up an old FAQ with a perl sub and the module DateTime::Calendar::Julian, both look suited to the task. Something wrong with these?

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo

Ramnarayan (Programmer) (OP)
14 Jan 06 0:06
I have already seen this. I think I did not pose the correct question. It should have been the other way round. I.e. to calculate the date from the julian date!

So if the julian date is 13, it should prompt me the date corresponding to this julian date i.e. 2006-01-13.

The perl sub gives the opposite. If you know how to reverse this I will be grateful for that!
TonyGroves (Programmer)
14 Jan 06 4:51
Seems to be some confusion here. "Julian Date" has several radically different meanings, according to http://en.wikipedia.org/wiki/Julian_date . Do you mean you want the yyyy-mm-dd corresponding to a specified nth day of the year? If so, you could use something like:

$year=2006;
$juliandate=50;

use POSIX;
$jan1=POSIX::mktime(0,0,0,1,0,$year-1900);
$dt=$jan1+$juliandate*86400-1;
@ymd=localtime($dt);
$ymd=sprintf("%d-%02d-%02d",1900+$ymd[5],$ymd[4]+1,$ymd[3]);
print "$ymd\n";

TonyGroves (Programmer)
14 Jan 06 4:54
Sorry, that should have been:

$dt=$jan1+($juliandate-1)*86400;
PaulTEG (TechnicalUser)
15 Jan 06 11:04
Tony, might not be a bad idea to retrofit that into Date::Manip, with the authors permission of course ;)

Spend an hour a week on CPAN, helps cure all known programming ailments

Ramnarayan (Programmer) (OP)
16 Jan 06 10:46
Hey Tony,

Thanks for the tip. However when I execute your statement, It is returning $ymd as 2006-02-19!. Is there some numbers to be changed inorder to get the correct julian date.

Yes I mean the julian day for the year. Hence for today, (2006-01-16), the julian day is 16.
PaulTEG (TechnicalUser)
16 Jan 06 20:06
all arrays should be 0-based, so that should be accounting for at least two of the missing $days--, the other one ... $ymd[4]+1, dunno, but that could be it ...

Spend an hour a week on CPAN, helps cure all known programming ailments

rharsh (TechnicalUser)
17 Jan 06 3:00
This is roughly the same as Tony's code except shoved into a sub.  I tried to avoid changing the date values around except where necessary.

CODE

print "16th Day: ", DOY_to_YMD(16), "\n";

sub DOY_to_YMD {
    use POSIX;
    my $DoY = shift; # Day of Year
    my ($day, $month, $year) = (localtime)[3..5];
    my $jan1 = POSIX::mktime(0,0,0,0,0,$year);  # Actually Dec 31 of prev year
    my @time = localtime($jan1 + $DoY * 86400);
    return sprintf "%4d-%02d-%02d", $time[5]+1900, $time[4]+1, $time[3];
}
TonyGroves (Programmer)
17 Jan 06 5:14
Ramnarayan:

The test date used in my code was day 50 of 2006, which is 2006-02-19. You would obviously use your own data instead.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close