function [yr, jday, hr, mn, sc, ms] = etoh( e )
% this script is the inverse of htoe()
% compute the year, julian day, hour, minute, second, millisecond
% given the epoch time (time in seconds after midnight, January 1, 1970)
%
% input:
%          e, the number of secobds after January 1, 1970.
%
% output:
%          yr, an integer, e.g. 2010
%          jday, an integer, the day of the year, where January 1 is 1
%          hr, an integer, the hour of the day, starting with 0
%          mn, an integer, the minute of the hour, starting with 0
%          sc, an integer, the second of the minute, starting with 0
%          ms, an integer, the milliseconds of the second, starting with 0
%
%
% Note 1: the monthday() script can calculate month and day from julian day
%
% Note 2: The calculation does not include leap seconds.  It will give correct
% dates/times for for GPS and TIA clock standards, but will be off for the UTC standard.
% Typically, this function is used in conjunction with htoe() to increment
% a date/time by a time interval, that is:
%      epoch1 = htoe( yr1, jday1, hr1, mn1, sc1, ms1 );
%      epoch2 = epoch1+interval;
%      [yr2, jday2, hr2, mn2, sc2, ms2] = etoh( epoch2 );
% The overall calculation could be off by as much as 2 seconds per length of the
% interval in years.

        daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

        
        if( e<0 )
            sprintf('error: negative epochs not implemented')
            return;
        end
  
        epoch=0.0;
        frac = e - floor(e);
        i=1970;
        while (i<5000)
            epoch=epoch+365.0*86400.0;
            if( isleap(i) )
                epoch=epoch+86400.0;
            end
            if( epoch>e )
                break;
            end
            i=i+1;
         end
         yr=i; mo=1; day=1; hr=0; mn=0; sc=0; ms=0;
         jday = julian( yr, mo, day );
         epoch=htoe(yr, jday, hr, mn, sc, ms);
         for i = [1:12]
              epoch=epoch+86400.0*daysinmonth(i);
              if( (i==2) && isleap(yr) ) 
                  epoch=epoch+86400.0;
              end
              if( epoch>e)
                  break;
              end
          end
          mo=i;
          jday = julian( yr, mo, day );
          epoch=htoe(yr, jday, hr, mn, sc, ms);
          j=daysinmonth(mo);
          if( (mo==2) && isleap(yr) ) 
              j=j+1;
          end
          for i = [1:j]
              epoch=epoch+86400.0;
              if( epoch>e)
                  break;
              end
          end
          day=i;
          jday = julian( yr, mo, day );
          epoch=htoe(yr, jday, hr, mn, sc, ms);
          for i= [0:23]
              epoch=epoch+3600.0;
              if( epoch>e )
                  break;
              end
          end
          hr=i;
          epoch=htoe(yr, jday, hr, mn, sc, ms);
          for i = [0:59]
              epoch=epoch+60.0;
              if( epoch>e)
                  break;
              end
          end
          mn=i;
          epoch=htoe(yr, jday, hr, mn, sc, ms);
          for i = [0:60]
              epoch=epoch+1.0;
              if( epoch>e)
                  break;
              end
          end
          sc=i;
          ms = floor((1000.0*frac)+0.5);

return;
