Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2009
    Posts
    16

    Unanswered: 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

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    You need to convert the date into a format Unix can use ie strip the dashes. Then output the date using the required format.
    Code:
    # your date
    DATE="2007-11-30"
    
    # strip the dashes
    DATE=`echo $DATE | sed 's/-//g'`
    
    # output the new format as YYDDD
    date -d$DATE '+%y%j'
    Strictly speaking YYDDD is an ordinal date rather than a Julian date but you could Google the difference.

    Mike

  3. #3
    Join Date
    Sep 2009
    Posts
    16
    Hi Mike ,
    this throws an error
    date -d$DATE '+%y%j'


    Code:
    date: illegal option -- d
    Usage: date [-u] [+format]
           date [-u] [mmddhhmm[[cc]yy]]
           date [-a [-]sss.fff]

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    The above is the normal way to provide parameters to the data command but you could try:
    Code:
    # your date
    DATE="2007-11-30"
    
    # strip the dashes
    DATE=`echo $DATE | sed 's/-//g'`
    
    # output the new format as YYDDD
    date --date=$DATE '+%y%j'
    If that fails then I suggest you type the following and read what your system requires:
    Code:
    man date

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Bet it's an SCO system. The date command only displays today's date, and allows updating the system date.

  6. #6
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Since your date does not support the -d flag try this ksh script to calculate ordinal date
    Code:
    #!/bin/ksh
    dt=$1       # date as ccyy-mm-dd
    calcord()   # function to calculate ordinal date
    {
    y=$1 m=$2 d=$3
    set -A odm 0 0 31 59 90 120 151 181 212 243 273 304 334
    echo ${y#??}$(( (m+9)/12*(y/4-(y-1)/4-y/100+(y-1)/100+y/400-(y-1)/400)+odm[m]+d )) # 2 digit year
    #echo $y$(( (m+9)/12*(y/4-(y-1)/4-y/100+(y-1)/100+y/400-(y-1)/400)+odm[m]+d ))     # 4 digit year
    }
    oIFS=$IFS   # save Internal field separators
    IFS='-'     # set Internal field separator
    calcord $dt # do the calc
    IFS=$oIFS   # restore Internal field separators
    Note: all calculations are integer calculations i.e. 5/3=1
    Last edited by pdreyer; 11-19-09 at 06:28. Reason: Note: integer calc

  7. #7
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    I thought I recognized that code.


    Code:
    SUBROUTINE XDATEX                                                   
    DATEXREF,    A                                                         
    RECORD DATEREC                                                         
               CLDRDATE, D8                                                
                   CLDRYEAR, D4 @CLDRDATE                                  
                   CLDRMTH,  D2 @CLDRDATE +4                               
                   CLDRDAY , D2 @CLDRDATE +6                               
               DAYS1901, D10                                               
               DAYOFWK,  D1                                                
               NAMOFDAY, A9                                                
               NAMOFMTH, A9                                                
    RECORD                                                                 
            CALCYEAR ,      D10                                            
            CALCMTH ,       D10                                            
            CALCDAY ,       D10                                            
            DYYR ,          D10                                            
            DYWK ,          D10                                            
            DYMO ,          D10                                            
            TEMPYEAR ,      D10                                            
            TEMPDAY ,       D10                                            
            LEAPYEAR ,      D10                                            
            HOLDDATE ,      D8                                             
            M, A8                                                          
            DAYDATA, 7A9, 'SUNDAY   ','MONDAY   ','TUESDAY  ','WEDNESDAY'  
    &                       ,'THURSDAY ','FRIDAY   ','SATURDAY '              
            MTHDATA, 12A9, 'JANUARY  ','FEBRUARY ','MARCH   ','APRIL   ',     
    &                       'MAY     ','JUNE    ','JULY    ','AUGUST  ',      
    &                       'SEPTEMBER','OCTOBER  ','NOVEMBER ','DECEMBER '   
    PROC                                                                      
    SOJ,                                                                      
            DATEREC=DATEXREF                                                  
            HOLDDATE=CLDRDATE                                                 
            IF (DAYS1901 .NE. 0) GO TO CENTOCLD                               
            LEAPYEAR=2                                                        
            CALCYEAR= CLDRYEAR - 1900                                         
            CALCMTH= CLDRMTH                                                  
            CALCDAY= CLDRDAY                                                  
            TEMPYEAR= CALCYEAR/4                                              
            TEMPYEAR= TEMPYEAR * 4                                            
            IF (TEMPYEAR .EQ. CALCYEAR)  LEAPYEAR=1                           
            DYYR= (CALCMTH * 275)/9 + CALCDAY - 30                            
            IF (CALCMTH .GT. 2) DYYR= DYYR -LEAPYEAR                          
            DAYS1901= CALCYEAR - 1                                            
            DAYS1901= (DAYS1901 * 1461) / 4 + DYYR                            
            CALL    CENTOCLD                                                  
            IF (CLDRDATE .NE. HOLDDATE)                                       
                    BEGIN                                                     
                            DAYS1901= 0       
                            CLDRDATE= 0                              
                            DAYOFWK = 0                              
                            NAMOFDAY=                                
                            NAMOFMTH=                                
                            DATEXREF=DATEREC                         
                            RETURN                                   
                    END                                              
            RETURN                                                   
    CENTOCLD,                                                        
            CALCYEAR=(DAYS1901/1461)                                 
            CALCYEAR=(DAYS1901 - CALCYEAR + 364)/365                 
            DYYR=((CALCYEAR - 1)*1461)/4                             
            DYYR=DAYS1901-DYYR                                       
            LEAPYEAR= 2                                              
            TEMPYEAR= CALCYEAR/4                                     
            TEMPYEAR= TEMPYEAR * 4                                   
            IF (TEMPYEAR .EQ. CALCYEAR) LEAPYEAR=1                   
            TEMPDAY= DYYR                                            
            TEMPYEAR= 61 - LEAPYEAR                                  
            IF (TEMPDAY .GT. TEMPYEAR) TEMPDAY= TEMPDAY + LEAPYEAR   
            CALCMTH= (TEMPDAY * 9 + 269) / 275                       
            DYMO= ((CALCMTH * 275) / 9) - 30                         
            CALCDAY= TEMPDAY - DYMO                                  
            DYMO=CALCDAY   
            DYWK=DAYS1901 + 1            
            DYWK=DYWK-((DYWK/7)*7)+1     
            DAYOFWK=DYWK                 
            CLDRYEAR = CALCYEAR + 1900   
            CLDRMTH= CALCMTH             
            CLDRDAY= CALCDAY             
            NAMOFDAY=  DAYDATA(DYWK)     
            NAMOFMTH=MTHDATA(CALCMTH)    
            DATEXREF=DATEREC             
            RETURN
    Code:
    .subroutine xdate2                                                           
    STRTDATE,    N                                                               
    END1DATE,     N                                                              
    DIFF,        N                                                               
    RECORD                                                                       
            STRT,   D8                                                           
            END1 ,  D8                                                           
            I ,     D8                                                           
            I1 ,    D8                                                           
            COUNT,  D5                                                           
            M, A8                                                                
            HOLIDAY, 20D5, 35795,35886,35938,35976,36008,36043,36078,36152,36153,
    &       36155,00000,00000,00000,00000,00000,00000,00000,00000,00000,00000    
    RECORD DATEREC                                                               
               CLDRDATE, D8                                                      
                   CLDRYEAR, D4 @CLDRDATE                                        
                   CLDRMTH,  D2 @CLDRDATE +4                                     
                   CLDRDAY , D2 @CLDRDATE +6                                     
               DAYS1901, D10                                                     
               DAYOFWK,  D1                                                      
               NAMOFDAY, A9                                                      
               NAMOFMTH, A9                                                      
    PROC                                                                         
    SOJ,   
    SOJ,                                                                         
            DATEREC=                                                             
            CLDRDATE=STRTDATE                                                    
            XCALL XDATEX(DATEREC)                                                
            STRT=DAYS1901                                                        
            DATEREC=                                                             
            CLDRDATE=END1DATE                                                    
            XCALL XDATEX(DATEREC)                                                
            END1=DAYS1901                                                        
            IF (END1 .LT. STRT)                                                  
                    BEGIN                                                        
                    DIFF=0                                                       
                    RETURN                                                       
                    END                                                          
            COUNT=0                                                              
            FOR I = STRT STEP 1 UNTIL END1 DO                                    
            BEGIN                                                                
                    COUNT=COUNT + 1                                              
                    DATEREC=                                                     
                    DAYS1901=I                                                   
                    XCALL XDATEX(DATEREC)                                        
                    IF ((DAYOFWK .EQ. 7) .OR. (DAYOFWK .EQ. 1)) COUNT=COUNT-1    
                    FOR I1=1 STEP 1 UNTIL 20 DO                                  
                    BEGIN 
                            IF (DAYS1901 .EQ. HOLIDAY(I1)) COUNT=COUNT - 1  
                    END                                                     
            END                                                             
            DIFF=COUNT                                                      
            RETURN

  8. #8
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1

    Continuation

    Post was too long, there is a 10000 character limit.
    I found this code in a Computer World Newspaper article about 1970 and translated it into Basic then Fortran, then Cobol, and finally in 1999 to DIBOL.
    All the aritmetic is integer and division provides a remainder.
    The DATEREC record is the data supplied to the subroutine.
    The first converts a date to the number of days since Jan 01 1900, or from the number of days back to YYYYMMDD and the second shows how it can be used.

  9. #9
    Join Date
    May 2009
    Location
    India
    Posts
    66
    Tested on Solaris

    Code:
    # date '+%Y%m%d'
    echo '20091231' | awk '
    BEGIN {
            a_l["01"]=31 ;
            a_l["02"]=60 ;
            a_l["03"]=91 ;
            a_l["04"]=121 ;
            a_l["05"]=152 ;
            a_l["06"]=182 ;
            a_l["07"]=213 ;
            a_l["08"]=244 ;
            a_l["09"]=274 ;
            a_l["10"]=305 ;
            a_l["11"]=335 ;
    
            a_o["01"]=31 ;
            a_o["02"]=60-1 ;
            a_o["03"]=91-1 ;
            a_o["04"]=121-1 ;
            a_o["05"]=152-1 ;
            a_o["06"]=182-1 ;
            a_o["07"]=213-1 ;
            a_o["08"]=244-1 ;
            a_o["09"]=274-1 ;
            a_o["10"]=305-1 ;
            a_o["11"]=335-1 ;
    }
    {
            YYYY=substr($0,1,4) ;
            YY=substr($0,3,2) ;
            MM=substr($0,5,2) ;
            DD=substr($0,7,2) ;
            MD=YYYY%4 ;
            flg=0 ;
            printf("%s:",$0) ;
            if (YYYY%4 != 0) flg=2 ;
            if ( (flg==0) && (YYYY%400 == 0) ) flg=1 ;
            if ( (flg==0) && (YYYY%100 == 0) ) flg=2 ;
            if ( (flg==0) && (YYYY%4 == 0) ) flg=1 ;
            if (flg==2) flg=0 ;
            if (flg==0) printf("%02s%03s\n",YY,a_o[MM-1]+DD) ;
            if (flg!=0) printf("%02s%03s\n",YY,a_l[MM-1]+DD) ;
    }'

  10. #10
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    I hope there's a damn good reason for converting this date over to a Julian date (I still say it's an ordinal date and not a Julian date). It just seems easier to me to keep the file name (or whatever it is) as MyFile_20071130.dat but then I'm really lazy!

  11. #11
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Quote Originally Posted by mike_bike_kite View Post
    I hope there's a damn good reason for converting this date over to a Julian date (I still say it's an ordinal date and not a Julian date). It just seems easier to me to keep the file name (or whatever it is) as MyFile_20071130.dat but then I'm really lazy!
    That's because the systems analyst specified that way, and the design committee approved it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •