Converting YYYYMMDD to Julian | 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.

Converting YYYYMMDD to Julian

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-27-2005
dfran1972 dfran1972 is offline
Registered User
 
Join Date: Jan 2004
Last Activity: 28 April 2005, 1:14 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Converting YYYYMMDD to Julian

I am writing some PERL code (and I realize this is a UNIX forum), but was wondering if anyone has a quick routine (PERL or shell scripting) to take a date in YYYYMMDD format and return the 3 digit Julian number.

For instance, my program will have a variable called "$Settlement_Date" and will contain a value like this: "20050422", and I would like it to return a new variable with the value "112".

Any help or insight would be appreciated.
Sponsored Links
    #2  
Old 04-27-2005
dfran1972 dfran1972 is offline
Registered User
 
Join Date: Jan 2004
Last Activity: 28 April 2005, 1:14 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, actually need to TAKE a 3 digit julian and convert it to YYYYMMDD format. Original post was stated incorrectly.
Sponsored Links
    #3  
Old 04-27-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 20 October 2014, 2:38 AM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
something to start with:

Code:
#!/bin/ksh
#-----------------------------------------------------------------------------
# Date manipulation routines - convering to Julian and back
#
# Julian Day Number from calendar date
date2julian() #  year month day
{
  typeset year=$1;  typeset month=$2;  typeset day=$3
  tmpmonth=$((12 * year + month - 3))
  tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
    tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}

# Calendar date from Julian Day Number
julian2date() # julianday
{
  #set -x
  typeset tmpday=$(($1 - 1721119))
  typeset centuries=$(( (4 * tmpday - 1) / 146097))
  tmpday=$((tmpday + centuries - centuries/4))
  typeset year=$(( (4 * tmpday - 1) / 1461))
  tmpday=$((tmpday - (1461 * year) / 4))
  typeset month=$(( (10 * tmpday - 5) / 306))
  day=$((tmpday - (306 * month + 5) / 10))
  month=$((month + 2))
  year=$((year + month/12))
  month=$((month % 12 + 1))
  printf "%s %02d %02d" $year $month $day
}

# Day of week, Monday=1...Sunday=7
dow() # year month day
{
  print $(( $(date2julian $1 $2 $3) % 7 + 1))
}

The Following User Says Thank You to vgersh99 For This Useful Post:
menon (07-11-2010)
    #4  
Old 04-28-2005
dfran1972 dfran1972 is offline
Registered User
 
Join Date: Jan 2004
Last Activity: 28 April 2005, 1:14 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I'm probably just being stupid, but what am I doing wrong? Here is the code and the result I am getting out. Trying to pass in Julian Date of 112 to have it return 20050422...

#! /usr/bin/ksh

# Calendar date from Julian Day Number
julian2date()
{
#set -x
typeset tmpday=$(($1 - 1721119))
typeset centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
typeset year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
typeset month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
printf "%s %02d %02d" $year $month $day
}

julian2date 112


[U]RESULT:[/U]

/home/rsd9/xxgvlqr> test.ksh
-4711 -8 -15/home/rsd9/xxgvlqr>
Sponsored Links
    #5  
Old 04-28-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 16 October 2014, 7:51 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
Date arithmetic is covered in our faq section. You might want to look at that.
Sponsored Links
    #6  
Old 04-28-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 20 October 2014, 2:38 AM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
this seems to work ok for me - you might want to look into FAQ as well [as suggested by Perderabo:

Code:
#!/bin/ksh

# Calendar date from Julian Day Number
julian2date()
{
typeset tmpday=$(($1 - 1721119))
typeset centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
typeset year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
typeset month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
printf "%s %02d %02d" $year $month $day
}

date2julian() #  year month day
{
  typeset year=$1;  typeset month=$2;  typeset day=$3
  typeset tmpmonth=$((12 * year + month - 3))
  typeset tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
  tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}


printf "today->[2005 4 29] julian->[%d]\n" $(date2julian 2005 4 29)
printf "and back from julian->[%s %s %s]\n" $(julian2date $(date2julian 2005 4 29))

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
Converting filenames from julian day to yyyy-mm-dd and retrieving weekly mean values ida1215 Shell Programming and Scripting 3 02-14-2012 05:09 AM
Converting Date from YYYYMMDD to DD-MON-YYYY ady_koolz Shell Programming and Scripting 17 12-15-2011 12:23 PM
How to approach Julian date? mgyeah Homework & Coursework Questions 1 03-06-2011 06:25 PM
Perl in KSH - julian conversion gio001 Shell Programming and Scripting 7 10-03-2008 02:24 AM
Julian Date lesstjm Shell Programming and Scripting 3 04-17-2003 03:39 PM



All times are GMT -4. The time now is 04:12 AM.