convert Julian date to calender date | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

convert Julian date to calender date

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-07-2007
srikanthus2002's Avatar
srikanthus2002 srikanthus2002 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 14 October 2011, 3:22 PM EDT
Location: Can u guess...!
Posts: 161
Thanks: 0
Thanked 0 Times in 0 Posts
convert Julian date to calender date

Hi,

I have script in unix which creates a julian date like 126 or 127

I want convert this julian date into calender date

ex : input 127
output 07/may/2007 or 07/05/2007 or 07/05/07


rgds
srikanth
Sponsored Links
    #2  
Old 05-07-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 21 November 2014, 4:45 AM EST
Location: Bangalore,India
Posts: 2,091
Thanks: 12
Thanked 131 Times in 126 Posts
Check datecalc
Sponsored Links
    #3  
Old 05-07-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 November 2014, 10:33 PM EST
Location: NM
Posts: 10,282
Thanks: 288
Thanked 815 Times in 757 Posts
try:

Code:
#!/bin/ksh

date_from_yday()
{
	perl -e '
	  use Time::Local;

      $yr = $ARGV[0] - 1900;
      $yday = $ARGV[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;
      printf("%02d/%02d/%d\n", $day, $mon, $yr);

	' $2  $1
}

date_from_yday 75  2007

    #4  
Old 05-07-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 November 2014, 10:48 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
Quote:
Originally Posted by srikanthus2002
Hi,

I have script in unix which creates a julian date like 126 or 127

I want convert this julian date into calender date

ex : input 127
output 07/may/2007 or 07/05/2007 or 07/05/07
day-of-year != julian date
Read Julian day. (I love the way that article self-adjusts it's examples to the current day.) datecalc implements Modified Julian Day as described in that article:
$ datecalc -j 2007 5 7
54227
$

To convert that to day-of year:
$ echo $(( $(datecalc -j 2007 5 7) - $(datecalc -j 2007 1 1) +1))
127

To convert back, just reverse the operation:
$ datecalc -j $(( 127 + $(datecalc -j 2007 1 1) -1))
2007 5 7
$
Sponsored Links
    #5  
Old 05-07-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by srikanthus2002
Hi,

I have script in unix which creates a julian date like 126 or 127

I want convert this julian date into calender date

ex : input 127
output 07/may/2007 or 07/05/2007 or 07/05/07

Julian day 127 is more than 4,000 years BC.

If you mean day of the year, and have GNU date (which may be installed as gdate on non-GNU systems):


Code:
day=127
date -d "`date +%Y`-01-01 +$(( ${day} - 1 ))days" +%Y-%m-%d

Sponsored Links
    #6  
Old 05-07-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 November 2014, 10:48 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
The US Naval Obseravtory has an online Julian Date Calculator. JD 127 starts at noon on May 7, 4713 BC and extends until noon on the next day.
Sponsored Links
    #7  
Old 05-08-2007
srikanthus2002's Avatar
srikanthus2002 srikanthus2002 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 14 October 2011, 3:22 PM EDT
Location: Can u guess...!
Posts: 161
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara
try:

Code:
#!/bin/ksh

date_from_yday()
{
	perl -e '
	  use Time::Local;

      $yr = $ARGV[0] - 1900;
      $yday = $ARGV[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;
      printf("%02d/%02d/%d\n", $day, $mon, $yr);

	' $2  $1
}

date_from_yday 75  2007

thanks jim,,

i have done some change according to my requirements


Code:
#!/bin/ksh

date_from_yday()
{
        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("%02d/%02d/%d\n", $day, $mon, $yr);

#my $now = localtime time;
#        print "It is now $now\n";

        ' $2  $1
}
date_from_yday $1 $2

now it is working fine
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need a unix script to convert date into Julian format in a text file ajiwww Shell Programming and Scripting 4 09-14-2011 07:16 AM
Julian date to Calendar date conversion RahulJoshi Shell Programming and Scripting 3 03-07-2011 10:01 PM
Get Julian date from date string ananth4mu Homework & Coursework Questions 2 11-01-2010 08:06 AM
Conversion of date to Julian date SeenuGuddu Shell Programming and Scripting 4 11-17-2009 02:23 AM
Find julian date for given corresponding date srikanthus2002 Shell Programming and Scripting 2 10-10-2006 10:33 PM



All times are GMT -4. The time now is 02:24 AM.