Perl in KSH - julian conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl in KSH - julian conversion
# 1  
Old 10-02-2008
Perl in KSH - julian conversion

Hello Everyone,
I have this code misbehaving in one of my scripts, I have a var containing the sequential number of the day for the year and I am suppose to get the regular date for that day and its weekday.
If I set the day to 273 I get back 2008/09/31 which is not a proper date.
can you help me understand why?
.................
syy=2008
newstart=272
perl -e 'use Time::Local; $yr = $ARGV[0] - 1900; $yday = $ARGV[1]; $mytime = timelocal(1,0,0,1,1,$yr); $mytime += ( 86400 * $yday ); ($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = localtime($mytime);
$yr += 1900; printf("%d/%02d/%02d%s", $yr, $mon, $day, " " );use POSIX qw(strftime);$fmt = "%A"; # %a = abbreviated weekday %u = weekday number;
$weekday = strftime($fmt, 0, 0, 0, $day , $mon - 1, $yr - 1900, -1, -1, -1);printf("%s%s%s", "$weekday"," ",$ARG[2]);' $syy $newstart >> pppp

newstart=273
perl -e 'use Time::Local; $yr = $ARGV[0] - 1900; $yday = $ARGV[1]; $mytime = timelocal(1,0,0,1,1,$yr); $mytime += ( 86400 * $yday ); ($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = localtime($mytime);
$yr += 1900; printf("%d/%02d/%02d%s", $yr, $mon, $day, " " );use POSIX qw(strftime);$fmt = "%A"; # %a = abbreviated weekday %u = weekday number;
$weekday = strftime($fmt, 0, 0, 0, $day , $mon - 1, $yr - 1900, -1, -1, -1);printf("%s%s%s", "$weekday"," ",$ARG[2]);' $syy $newstart >> pppp

newstart=274
perl -e 'use Time::Local; $yr = $ARGV[0] - 1900; $yday = $ARGV[1]; $mytime = timelocal(1,0,0,1,1,$yr); $mytime += ( 86400 * $yday ); ($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = localtime($mytime);
$yr += 1900; printf("%d/%02d/%02d%s", $yr, $mon, $day, " " );use POSIX qw(strftime);$fmt = "%A"; # %a = abbreviated weekday %u = weekday number;
$weekday = strftime($fmt, 0, 0, 0, $day , $mon - 1, $yr - 1900, -1, -1, -1);printf("%s%s%s", "$weekday"," ",$ARG[2]);' $syy $newstart >> pppp

the contents of pppp:
2008/09/30 Tuesday 2008/09/31 Wednesday 2008/10/01 Wednesday

Thanks for your help.
# 2  
Old 10-02-2008
Shouldn't the month in the call to timelocal be zero rather than one?

Did you notice the Time::Local manual page has an example using the "timelocal_nocheck" facility which does pretty much this?
# 3  
Old 10-02-2008
I have looked and still it behaves incorrectly, could there be a better way than the one I am using to get to a date by knowing the years and the days since the beginning of that year?
Thanks.
# 4  
Old 10-02-2008
You may want to look at this solution:


How do I convert a julian date?
# 5  
Old 10-02-2008
If you are using ksh93 there is no need to use Perl. You can use the printf %T facility to do the conversion.
Code:
$ printf "%(%Y-%m-%d)T\n" "2008-273"
2008-09-29
$

# 6  
Old 10-02-2008
how do we connect bcp to sybase in kornshell???

Pls Reply
# 7  
Old 10-03-2008
I do not run ksh93....How about a solution in ksh88? Is it possible?
Let me know.
Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in Julian date conversion

All, I am facing an issue with julian date conversion.. current command: echo `date +%Y%j` `cat -n /home/user/FILENAME.dat |awk '{printf "%08s", $2}'` The above command is working good. But in the above bolded part, it is converting system date to julian date. However I want to... (8 Replies)
Discussion started by: cmaroju
8 Replies

2. Shell Programming and Scripting

Timestamp conversion in PERL

Hi, I have a file as below I need to overwrite the 2 nd column alone to numeric format like "06122011030414012345" as per the timestamp value output file should be the microseconds can be neglected if required. Any help will be appreciated. Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

3. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

4. Shell Programming and Scripting

Typeset conversion problem from ksh to bash

Hi, typeset -l sgf # all lowercase letters typeset -u SGF # all uppercase letters sgf=$1 SGF=$sgf these lines used in my scripts . It ran fine in ksh but when we convert this to bash it erroring out. I like to know what the use of typeset ?? Thanks & Regards kanagaraj (3 Replies)
Discussion started by: kanagaraj
3 Replies

5. Shell Programming and Scripting

Perl Conversion Tool

I want a perl script that converts special characters (French, German, Italy, Denmark character set) into database compatible format and retrieves the back in the original format. A generic functionality will be developed which converts any special characters (having > 8 bit format). I tried... (3 Replies)
Discussion started by: perl2ask
3 Replies

6. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

7. Shell Programming and Scripting

Timestamp to date conversion in ksh

Hi, I have a file containing timestamp( Example given below). How can i get date(mmd-dd-yyyy) from it? ($> cat file1.txt 2008-11-24 05:17:00.7043) Thanks, Sri (2 Replies)
Discussion started by: srilaxmi
2 Replies

8. Shell Programming and Scripting

Does KSH support data type conversion?

Hello,everyone here. I'm coding with KSH to achieve exploring the disk space and judging whether it closes to overflow.But It seems that no one way to convert a string variable to integer. df | read A B C D E F G H I J K L print ${L} Can I convert L to integer type? Thanks for... (2 Replies)
Discussion started by: joshuaduan
2 Replies

9. Shell Programming and Scripting

Conversion question about ksh

Hi all, New to ksh and had a few questions to see if this is doable in ksh or if I am going to have to call out to a tcl procedure. I have an Ascii file I want to convert to hex then search and remove all hex chars '0A' and then convert back to Ascii. Here is an example of an Ascii file I am... (2 Replies)
Discussion started by: hgjdv
2 Replies

10. Shell Programming and Scripting

csh -> ksh conversion question

Having been a long-time csh person I now need to convert a lot of stuff to run under ksh. Can anyone tell me how to do a ksh equivalent of the csh history substitution !* that can be used in an alias, ie how would I do: alias cd "cd \!*; pwd" alias find "find . -name... (2 Replies)
Discussion started by: jonnywilkins
2 Replies
Login or Register to Ask a Question