[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A routine for converting 2-digit years.



 
  There may be a number of routines like this floating around, but here is another one.
  Two digit years may be politically incorrect right now, but they'll soon be back in use
  so a good way to deal with them is useful.  The routine below is very simple but should
  not break any time soon (I think it might have a problem when years need more than 16
  bit integers, but it will be easy to fix and there is time to worry about that later).  I included
  a keyword to give a base year so 500 years from now somebody could deal with 2 digit years
  from the mid-twentieth century without a lot of trouble.

  One of these days I'll try to get my IDL library updated.  I thought this routine might be useful
  right now.

-- 

  Ray Sterner                     sterner@tesla.jhuapl.edu 
  The Johns Hopkins University    North latitude 39.16 degrees.
  Applied Physics Laboratory      West longitude 76.90 degrees.
  Laurel, MD 20723-6099
------------------------------------------------------------------------
;-------------------------------------------------------------
;+
; NAME:
;       YY2YYYY
; PURPOSE:
;       Convert a 2 digit year to a 4 digit year.
; CATEGORY:
; CALLING SEQUENCE:
;       yyyy = yy2yyyy( yy)
; INPUTS:
;       yy = 2 digit year.          in
; KEYWORD PARAMETERS:
;       Keywords:
;         /PAST means 4 digit year is current or past.
;           Use this for birthdates or any dates known to be past.
;           By default closest 4 digit year is returned.
;         BASE=base  Use the year given in base instead of the
;           current year to figure out the 4 digit years.
; OUTPUTS:
;       yyyy = 4 digit year.        out
; COMMON BLOCKS:
; NOTES:
;       Notes: 2 digit years will always be useful, so a
;        good way to convert them to 4 digit years is also
;        useful.  This routine should not break in the future.
; MODIFICATION HISTORY:
;       R. Sterner, 1999 Aug 2
;
; Copyright (C) 1999, Johns Hopkins University/Applied Physics Laboratory
; This software may be used, copied, or redistributed as long as it is not
; sold and this copyright notice is reproduced on each copy made.  This
; routine is provided as is without any express or implied warranties
; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
;-
;-------------------------------------------------------------
        function yy2yyyy, yy0, past=past, base=base, help=hlp
 
        if (n_params(0) lt 1) or keyword_set(hlp) then begin
          print,' Convert a 2 digit year to a 4 digit year.'
          print,' yyyy = yy2yyyy( yy)'
          print,'   yy = 2 digit year.          in'
          print,'   yyyy = 4 digit year.        out'
          print,' Keywords:'
          print,'   /PAST means 4 digit year is current or past.'
          print,'     Use this for birthdates or any dates known to be past.'
          print,'     By default closest 4 digit year is returned.'
          print,'   BASE=base  Use the year given in base instead of the'
          print,'     current year to figure out the 4 digit years.'
          print,' Notes: 2 digit years will always be useful, so a'
          print,'  good way to convert them to 4 digit years is also'
          print,'  useful.  This routine should not break in the future.'
          return,''
        endif
 
        yy = yy0 + 0                            ; Force input to be numeric.
 
        ;-------------------------------------------------------------
        ;  Find current year (or working year from BASE)
        ;-------------------------------------------------------------
        if n_elements(base) ne 0 then begin
          yn = base+0                     ; Work with year given in base.
        endif else begin
          t = systime()                   ; Current time.
          yn = strmid(t,strlen(t)-4,4)+0  ; Pick off year.
        endelse
 
        ;-------------------------------------------------------------
        ;  Current century
        ;-------------------------------------------------------------
        cn = 100*fix(yn/100)              ; Current century.
 
        ;-------------------------------------------------------------
        ;  Make list of potential centuries
        ;-------------------------------------------------------------
        list = [cn-100,cn]                ; List of last and current centuries.
        if not keyword_set(past) then $   ; If not restricted to past years
          list = [cn+100,list]            ;   then include next century.
 
        ;-------------------------------------------------------------
        ;  Set up storage for output
        ;-------------------------------------------------------------
        yy4 = yy                          ; Copy input to output variable.
 
        ;-------------------------------------------------------------
        ;  Loop through all input years
        ;-------------------------------------------------------------
        for i=0,n_elements(yy)-1 do begin
          t = yy(i)                       ; Grab i'th year.
          if t lt 100 then begin          ; 2 digits?
            lst = list + t                ; Possible 4 digit years.
            d = abs(yn-lst)               ; Years away from now.
            w = where(d eq min(d))        ; Look for closest to now (or base).
            t = lst(w(0))                 ; Pull it from list.
          endif
          yy4(i) = t                      ; Insert 4 digit year.
        endfor
 
        return, yy4                       ; Return 4 digit year.
 
        end