Perl in KSH - julian conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl in KSH - julian conversion
# 8  
Old 10-03-2008
Again, the Time::Local manual page contains (the moral equivalent of) this example.

Code:
vnix$ perl -MTime::Local=timelocal_nocheck -le '
    print scalar localtime timelocal_nocheck 0, 0, 0, $ARGV[1], 0, $ARGV[0]-1900' 2008 274
Tue Sep 30 00:00:00 2008

By omitting the "print scalar" you obtain an array value which you can pass to strftime or whatever. (You don't really need strftime for a trivial thing like weekday conversion, though -- unless you need to have weekdays translated through locale support. print (qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday))[$wday])

Here's a version of your script which has the offsets corrected; it seems to work for the example dates at least.

Code:
perl -e 'use Time::Local; $yr = $ARGV[0] - 1900; $yday = $ARGV[1]-1;
  $mytime = timelocal(1,0,0,1,0,$yr); $mytime += ( 86400 * $yday );
  ($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = localtime($mytime);
  $yr += 1900; ++$mon;
  printf("%d/%02d/%02d %s\n", $yr, $mon, $day,
    (qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday))[$wday])' $syy $newstart

And here's a further simplificiation using the timelocal_nocheck facility.

Code:
perl -MTime::Local=timelocal_nocheck -e '($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = 
    localtime timelocal_nocheck(1,0,0,$ARGV[1],0,$ARGV[0]-1900);
  printf("%d/%02d/%02d %s\n", $yr+1900, $mon+1, $day,
    (qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday))[$wday])' $syy $newstart


Last edited by era; 10-03-2008 at 03:47 AM.. Reason: Corrected versions of script
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