Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

How do I convert a julian date?

by oakley (Scribe)
on Dec 05, 2000 at 18:21 UTC ( #44984=perlquestion: print w/replies, xml ) Need Help??
oakley has asked for the wisdom of the Perl Monks concerning the following question:

I have some files that have the format <filename.YYJJJ.dat> in which I need to take those middle 5 digits and convert to a MMDD format. The question thus is, how in the world do I go about doing it? =) Thanks in advance!

Replies are listed 'Best First'.
Re: How do I convert a julian date?
by davorg (Chancellor) on Dec 05, 2000 at 18:39 UTC

    It doesn't actually sound like those are Julian dates that you've got there(1), but days of the year.

    Assuming that I'm right, you could try something like this:

    use Time::Local; my $date = '00250'; my ($yr, $day) = unpack('A2A3', $yr); # Ugly hack to get round the fact that you have # two digit dates. Adjust cutoff to taste. $yr += 100 if $yr < 70; # Get epoch time at start of year. my $when = timelocal(0, 0, 0, 1, 0, $yr); # Increment by number of days $when += 86_400 * $day; # Convert back to MMDD my ($d, $m) = (localtime($when))[3, 4]; ++$m; sprintf('%02d%02d', $d, $m);

    (1) See for more stuff about Julian dates.


    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Not related to perl but here's a short history lesson on the topic. Unfortunately, both days of the year and the Julian Day are often (correctly) referred to as "Julian Dates/Days." I'm an astronautical engineer so I always use Julian Date meaning the number of days and fractional days since 4712 BC but I deal with all the time with systems that call the day of the year (e.g. Jan 1 = 1, Dec 31 = 365 (or 366)) the Julian Day or Julian Date as well. It's a coincidence of history that the two should both be called Julian Days. The numbering of the days of the year from 1 to 365/366 refers to Julius Ceaser (circa 46 BC) who adopted the solar calender having 365 days in a year and a leap year. The Julian Date referring to the number of days and fractional days since 4712 BC refers to Julius Scaliger (circa 1583 A.D. ) and is often used for astronomy and astrodynamics. Because it makes date arithmetic easy, Scaliger's version of the Julian Date is widely used where dates (especially dates that are far apart) need to be added or subtracted. It's the original "Star Date."
Re: How do I convert a julian date?
by davemabe (Monk) on Dec 05, 2000 at 18:28 UTC
Re: How do I convert a julian date?
by coreolyn (Parson) on Dec 05, 2000 at 19:05 UTC

    I'm not positive but I thought Time::Manip had some code to handle that. It would be worth at least checking out.

    coreolyn Duct tape devotee.
    -- That's OO perl, NOT uh-oh perl !-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://44984]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-06-19 18:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.