Perl date conversions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl date conversions
# 1  
Old 06-16-2008
Perl date conversions

Is there a perl date conversion that will convert this type of date:

2008/100:01:56:10.000

to this type of date:

2008 04 09 01 56 10 000

I am using this right now:

sub jd2date
{
$space=" ";
$zero="0";
my ($jd) = @_;
if (($jd<1) || ($jd>782028))
{
print "julian day out of range\n";
return 1;
}
use integer;
$standard_jd=$jd+2400001;
use integer;
$temp1 = $standard_jd + 68569;
use integer;
$temp2 = (4*$temp1)/146097;
$temp3 = $temp1 - ((146097 * $temp2 + 3) / 4);
use integer;
$year = 4000 * ($temp3 + 1) / 1461001;
use integer;
$temp4 = $temp3 - 1461 * $year/4 + 31;
use integer;
$month = 80 * $temp4 / 2447;
use integer;
$day = $temp4 - 2447 * $month / 80;
use integer;
$temp5 = $month / 11;
use integer;
$month = $month + 3 - 12 * $temp5;
use integer;
$year = 100 * ($temp2 - 49) + $year + $temp5;
if (length($month)==1)
{
use integer;
$tempmonth=$zero . $month;
}
else
{
use integer;
$tempmonth=$month;
}
if (length($day)==1)
{
use integer;
$tempday=$zero . $day;
}
else
{
use integer;
$tempday=$day;
}
use integer;
$wholedate=$year . $space . $tempmonth . $space . $tempday;
return $wholedate;
}

It works with a date like this:

2008/100:01:56:10.000

to this type of date:

2008 04 09 01 56 10 000


but gives the wrong date for this type of date:

2008/009:01:56:10.000

I am getting this date:

2007 13 09 01 56 10 000

I think it might be the julian date conversion, but I can't figure it out.

Is there a way to convert this date without doing a julian date conversion?

Thanks.

Allyson
# 2  
Old 06-16-2008
TIME::PIECE supprts strptime:
Code:
my $t = Time::Piece->strptime("2008/100:01:56:10.000",
                                "%Y/%j:%H:%M:%S");  
print $t->strftime("%Y %m %d %H %M %S 000"), "\n";

The fractional second value is
# 3  
Old 06-16-2008
I added use Time::Piece; to the beginning of my code and am getting this error:

BatchPDS_perl.ksh /home/agwin/bin/b2.conf
Can't locate Time/Piece.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8.0/sun4-solaris /usr/local/lib/perl5/5.8.0 /usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris /usr/local/lib/perl5/site_perl/5.8.0 /usr/local/lib/perl5/site_perl .) at ./PDSBatch.pl line 2.
BEGIN failed--compilation aborted at ./PDSBatch.pl line 2.

Does this mean that I can't use this? Could it be somewhere else?

Thanks for the help.

Allyson
# 4  
Old 06-16-2008
I have these:

Local.pm
gmtime.pm
localtime.pm
tm.pm

Do any of these have the subroutine that I can use instead of Time::Piece->strptime($daytime,"%Y/%j:%H:%M:%S");?

Thanks.

Allyson
# 5  
Old 06-16-2008
I downloaded the POSIX-strptime-0.05 (that is the version we have) and now I really don't know what to do with it. I am pretty new to perl and have never installed anything like this. Any suggestions? Thanks.

Allyson
# 6  
Old 06-16-2008
CPAN has several good introductory tutorials on installing Perl modules for system-wide use, or for your own private use.. Probably the easiest way to get started is to simply ditch the thing you downloaded, and start over in the CPAN shell. See e.g. CPAN - query, download and build perl modules from CPAN sites
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

HD GB conversions

Sorry if this is in the wrong area. I am not sure where to put this question. HD manufacturers usually use the 1000 scale for measuring their HD capacity instead of 1024 which is way more common with computers. What would 750 GB on the 1000 scale convert to on the 1024 scale and how would you... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

3. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

4. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

5. UNIX for Advanced & Expert Users

Vi line endings conversions

I was reading these 2 articles. Why does the wikia one think :e ++ff=dos? Or am I just misunderstanding it? :e ++ff=unix :e ++ff=dos File format - Vim Tips Wiki Managing/Munging Line-Endings with Vi/Vim | Jeet Sukumaran (1 Reply)
Discussion started by: cokedude
1 Replies

6. Shell Programming and Scripting

Unix to Linux Conversions?

I have over 800 scripts (Korn and Perl) wrote on a Unix platform (Solaris) that I need to covert to a Linux platform (Redhat). All the path's and binaries need review. Are there any tools out there that would simplify or assist me with this conversion? (3 Replies)
Discussion started by: soupbone38
3 Replies

7. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

8. UNIX for Advanced & Expert Users

Time Calculations & Conversions

I have script that runs based on time variables passed in the command line. The first command argument is a timer, in seconds, of how often to execute a certain loop in the script. The second command argument is the end time of the script, in military time. Below is an example of the command line:... (1 Reply)
Discussion started by: Nysif Steve
1 Replies
Login or Register to Ask a Question