home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / timefns.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  3KB  |  136 lines

  1. // timefns.cc                                            -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include "dMatrix.h"
  29.  
  30. #include "tree-const.h"
  31. #include "oct-obj.h"
  32. #include "systime.h"
  33. #include "defun.h"
  34.  
  35. #ifdef HAVE_SYS_RESOURCE_H
  36. extern "C"
  37. {
  38. #include <sys/resource.h>
  39. }
  40. #endif
  41.  
  42. #ifndef RUSAGE_SELF
  43. #define RUSAGE_SELF 0
  44. #endif
  45.  
  46.  
  47. DEFUN ("clock", Fclock, Sclock, 1, 0,
  48.   "clock (): return current date and time in vector with elements\n\
  49. \n\
  50.   [ year, month, day-of-month, hour, minute, second ]")
  51. {
  52.   time_t now;
  53.   double fraction = 0.0;
  54.  
  55. #ifdef HAVE_GETTIMEOFDAY
  56.  
  57.   struct timeval tp;
  58.  
  59.   gettimeofday (&tp, 0);
  60.  
  61.   now = tp.tv_sec;
  62.  
  63.   fraction = tp.tv_usec / 1e6;
  64.  
  65. #else
  66.  
  67.   time (&now);
  68.  
  69. #endif
  70.  
  71.   struct tm *tm = localtime (&now);
  72.  
  73.   Matrix m (1, 6);
  74.   m.elem (0, 0) = tm->tm_year + 1900;
  75.   m.elem (0, 1) = tm->tm_mon + 1;
  76.   m.elem (0, 2) = tm->tm_mday;
  77.   m.elem (0, 3) = tm->tm_hour;
  78.   m.elem (0, 4) = tm->tm_min;
  79.   m.elem (0, 5) = tm->tm_sec + fraction;
  80.  
  81.   return m;
  82. }
  83.  
  84. DEFUN ("cputime", Fcputime, Scputime, 0, 0,
  85.   "[total, user, system] = cputime ()\n\
  86. \n\
  87. Return CPU time statistics.")
  88. {
  89.   Octave_object retval (3, Matrix (1, 1, 0.0));
  90.  
  91. #if defined (HAVE_GETRUSAGE)
  92.  
  93.   struct rusage resource_stats;
  94.  
  95.   getrusage (RUSAGE_SELF, &resource_stats);
  96.  
  97.   struct timeval usr = resource_stats.ru_utime;
  98.   struct timeval sys = resource_stats.ru_stime;
  99.  
  100.   double usr_time = usr.tv_sec + usr.tv_usec / 1e6;
  101.   double sys_time = sys.tv_sec + sys.tv_usec / 1e6;
  102.  
  103.   retval (2) = sys_time;
  104.   retval (1) = usr_time;
  105.   retval (0) = usr_time + sys_time;
  106.  
  107. #endif
  108.  
  109.   return retval;
  110. }
  111.  
  112. DEFUN ("date", Fdate, Sdate, 1, 0,
  113.   "date (): return current date in a string, in the form `18-Jul-94'")
  114. {
  115.   Octave_object retval;
  116.  
  117.   time_t now;
  118.   struct tm *tm;
  119.  
  120.   time (&now);
  121.   tm = localtime (&now);
  122.   char date[32];
  123.   int len = strftime (date, 31, "%d-%b-%y", tm);
  124.   if (len > 0)
  125.     retval = date;
  126.  
  127.   return retval;
  128. }
  129.  
  130. /*
  131. ;;; Local Variables: ***
  132. ;;; mode: C++ ***
  133. ;;; page-delimiter: "^/\\*" ***
  134. ;;; End: ***
  135. */
  136.