;+
; NAME: st2date
;
; PURPOSE: Convert systime(1) type elapsed seconds to calendar dates.
;
; CALLING SEQUENCE: st2date, st, yr,mn,day,hr,min,sec,WDAY=wday,MON=mon,
; 		    FORM=form
;
; INPUTS:
; 	st: The double number of seconds since Jan 1,1970 (as returned
; 	    by systime(1)).
;
; OPTIONAL OUTPUTS: (all outputs optional)
;	yr:   The year
;	mn:   The month
;	day:  The day of the month
;	hr:   The hour of the day
;	min:  The minute
;	sec:  The second, including fractional part.
;	KEYWORDS:
;	WDAY=wday: The day of the week, abbreviated as a string of length 3
;	MON=mon:   The month, abbreviated as a string of length 3
;	FORM=form: The entire date formatted as in systime(0)
;
; MODIFICATION HISTORY:
;	9/19/98:	Written, JDS
;
; PROCEDURES: DAYCNV (from the NASA Astro package.)
;
; NOTES:  The date returned by systime(1) may or may not be in UT,
; 	depending on the operating system.
;-

pro st2date, st,yr,mn,day,hr,min,sec,WDAY=wday,MON=mon,FORM=form
   on_error,2
   if size(st,/TYPE) ne 5 then begin 
      message,'Input systime must be of type double.'
      return
   endif 
   days=st/86400.0D             ;convert seconds to days.
   jd=days+2440587.5D           ;add julian date of Jan. 1,1970, 0.0h
   daycnv, jd, yr, mn, day, hr  
   min=(hr-floor(hr))*60.0D
   sec=(min-floor(min))*60.0D
   hr=floor(hr)
   min=floor(min)
   
   ;; find the calendar day -- midnight-midnight, not noon-noon (add .5)
   wday=(['Sun','Mon','Tue','Wed','Thu','Fri','Sat'])[floor((jd+1.5) mod 7)]
   mon=(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov', $
         'Dec'])[mn-1]
   
   if arg_present(form) then $
    form=string(FORMAT='(A3," ",A3," ",I2," ",2(I2.2,":"),I2.2," ",I4)', $
                wday,mon,day,hr,min,floor(sec),yr)
end

