A routine for converting 2-digit years. [message #17225] |
Mon, 20 September 1999 00:00 |
Ray Sterner
Messages: 10 Registered: December 1997
|
Junior Member |
|
|
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<br> There may be a number of routines like this floating around,
but here is another one.
<br> Two digit years may be politically incorrect right now, but
they'll soon be back in use
<br> so a good way to deal with them is useful. The routine
below is very simple but should
<br> not break any time soon (I think it might have a problem
when years need more than 16
<br> bit integers, but it will be easy to fix and there is time to
worry about that later). I included
<br> a keyword to give a base year so 500 years from now somebody
could deal with 2 digit years
<br> from the mid-twentieth century without a lot of trouble.
<p> One of these days I'll try to get my IDL library updated.
I thought this routine might be useful
<br> right now.
<pre>--
Ray Sterner &am p;nbsp; &am p;nbsp; &am p;nbsp; 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</pre>
<pre> ------------------------------------------------------------ ------------ </pre>
<pre> ;----------------------------------------------------------- --
;+
; NAME:
;   ; YY2YYYY
; PURPOSE:
;   ; Convert a 2 digit year to a 4 digit year.
; CATEGORY:
; CALLING SEQUENCE:
;   ; yyyy = yy2yyyy( yy)
; INPUTS:
;   ; yy = 2 digit year. & nbsp; 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. & nbsp; 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. & nbsp; in'
print,' yyyy = 4 digit year. & nbsp; 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 & ;nbsp; & ;nbsp; & ;nbsp; ; Work with year given in base.
endif else begin
t = systime() & amp;nbsp; & amp;nbsp; & amp;nbsp; ; Current time.
yn = strmid(t,strlen(t)-4,4)+0 ; Pick off year.
endelse
;----------------------------------------------------------- --
; Current century
;----------------------------------------------------------- --
cn = 100*fix(yn/100) & nbsp; & nbsp; ; 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] &nb sp; &nb sp; ; then include next century.
;----------------------------------------------------------- --
; Set up storage for output
;----------------------------------------------------------- --
yy4 = yy &nbs p; &nbs p; &nbs p; &nbs p; ; Copy input to output variable.
;----------------------------------------------------------- --
; Loop through all input years
;----------------------------------------------------------- --
for i=0,n_elements(yy)-1 do begin
t = yy(i) & nbsp; & nbsp; & nbsp; ; Grab i'th year.
if t lt 100 then begin & nbsp; ; 2 digits?
lst = list + t   ;   ; ; Possible 4 digit years.
d = abs(yn-lst)   ;   ; ; Years away from now.
w = where(d eq min(d)) &am p;nbsp; ; Look for closest to now (or base).
t = lst(w(0)) & amp;nbsp; & amp;nbsp; ; Pull it from list.
endif
yy4(i) = t   ;   ;   ; ; Insert 4 digit year.
endfor
return, yy4 &nb sp; &nb sp; &nb sp; ; Return 4 digit year.
end</pre>
<pre></pre>
</html>
|
|
|