PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/build46/harbour/source/rtl/seconds.c

#
C | 207 lines | 120 code | 22 blank | 65 comment | 15 complexity | e4eecec67b8c259ba390873bd9769b8d MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: seconds.c 6583 2006-02-04 16:16:48Z druzus $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * SECONDS() function
  7. *
  8. * Copyright 1999 Jose Lalin <dezac@corevia.com>
  9. * www - http://www.harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this software; see the file COPYING. If not, write to
  23. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  25. *
  26. * As a special exception, the Harbour Project gives permission for
  27. * additional uses of the text contained in its release of Harbour.
  28. *
  29. * The exception is that, if you link the Harbour libraries with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the Harbour library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by the Harbour
  39. * Project under the name Harbour. If you copy code from other
  40. * Harbour Project or Free Software Foundation releases into a copy of
  41. * Harbour, as the General Public License permits, the exception does
  42. * not apply to the code that you add in this way. To avoid misleading
  43. * anyone as to the status of such modified files, you must delete
  44. * this exception notice from them.
  45. *
  46. * If you write modifications of your own for Harbour, it is your choice
  47. * whether to permit this exception to apply to your modifications.
  48. * If you do not wish that, delete this exception notice.
  49. *
  50. */
  51. #include "hbapi.h"
  52. #include "hbdate.h"
  53. #include <time.h>
  54. #if defined( HB_OS_BSD)
  55. #include <sys/time.h>
  56. #else
  57. #include <sys/timeb.h>
  58. #endif
  59. #if defined( OS_UNIX_COMPATIBLE )
  60. #include <sys/times.h>
  61. #include <unistd.h>
  62. #endif
  63. #if defined( HB_OS_WIN_32 )
  64. #include <windows.h>
  65. #endif
  66. HB_EXPORT double hb_dateSeconds( void )
  67. {
  68. #if defined(_MSC_VER)
  69. #define timeb _timeb
  70. #define ftime _ftime
  71. #endif
  72. #if defined(HB_OS_BSD)
  73. struct timeval tv;
  74. struct timezone tz;
  75. #else
  76. struct timeb tb;
  77. #endif
  78. time_t seconds;
  79. time_t fraction;
  80. struct tm * oTime;
  81. HB_TRACE(HB_TR_DEBUG, ("hb_dateSeconds()"));
  82. #if defined(HB_OS_BSD)
  83. gettimeofday( &tv, &tz );
  84. seconds = tv.tv_sec;
  85. fraction = tv.tv_usec / 1000U;
  86. #else
  87. ftime( &tb );
  88. seconds = tb.time;
  89. fraction = tb.millitm;
  90. #endif
  91. oTime = localtime( &seconds );
  92. return ( oTime->tm_hour * 3600 ) +
  93. ( oTime->tm_min * 60 ) +
  94. oTime->tm_sec +
  95. ( ( double ) fraction / 1000.0 );
  96. }
  97. HB_FUNC( SECONDS )
  98. {
  99. hb_retnd( hb_dateSeconds() );
  100. }
  101. HB_FUNC( HB_CLOCKS2SECS )
  102. {
  103. hb_retnd((double) hb_parnl( 1 ) / CLOCKS_PER_SEC );
  104. }
  105. #ifdef HB_COMPAT_FLAGSHIP
  106. /*
  107. secondsCPU(n) -> nTime
  108. FlagShip/CLIP compatible function, which reports how many CPU and/or
  109. system seconds have elapsed since the beginning of the program execution.
  110. n == 1 utime -> user CPU time of the current process
  111. n == 2 stime -> system CPU time behalf of the current process
  112. n == 3 u + s -> sum of utime + stime (default)
  113. n == 11 cutime -> sum of the user CPU time of the current + child process
  114. n == 12 cstime -> sum of the system CPU time of the current + child process
  115. n == 13 cu+cs -> sum of cutime + cstime
  116. */
  117. HB_EXPORT double hb_secondsCPU( int n )
  118. {
  119. double d = 0.0;
  120. #if defined( HB_OS_WIN_32 )
  121. FILETIME Create, Exit, Kernel, User;
  122. static BOOL s_fInit = FALSE, s_fWinNT = FALSE;
  123. if( !s_fInit )
  124. {
  125. s_fInit = TRUE ;
  126. s_fWinNT = hb_iswinnt() ;
  127. }
  128. #endif
  129. if( ( n < 1 || n > 3 ) && ( n < 11 || n > 13 ) )
  130. n = 3;
  131. #if defined( OS_UNIX_COMPATIBLE )
  132. {
  133. struct tms tm;
  134. times(&tm);
  135. if( n > 10 )
  136. {
  137. n -= 10;
  138. if( n & 1 )
  139. d += tm.tms_cutime;
  140. if( n & 2 )
  141. d += tm.tms_cstime;
  142. }
  143. if( n & 1 )
  144. d += tm.tms_utime;
  145. if( n & 2 )
  146. d += tm.tms_stime;
  147. /* In POSIX-1996 the CLK_TCK symbol is mentioned as obsolescent */
  148. /* d /= CLK_TCK; */
  149. d /= (double) sysconf(_SC_CLK_TCK);
  150. }
  151. #else
  152. if( n > 10 )
  153. n -= 10;
  154. #if defined( HB_OS_WIN_32 )
  155. if( s_fWinNT &&
  156. GetProcessTimes( GetCurrentProcess(), &Create, &Exit, &Kernel, &User ) )
  157. {
  158. if( n & 1 )
  159. {
  160. d += ( double ) ( ( ( HB_LONG ) User.dwHighDateTime << 32 ) +
  161. ( HB_LONG ) User.dwLowDateTime );
  162. }
  163. if( n & 2 )
  164. {
  165. d += ( double ) ( ( ( HB_LONG ) Kernel.dwHighDateTime << 32 ) +
  166. ( HB_LONG ) Kernel.dwLowDateTime );
  167. }
  168. d /= 10000000.0;
  169. }
  170. else
  171. #endif
  172. {
  173. /* TODO: this code is only for DOS and other platforms which cannot
  174. calculate process time */
  175. if( n & 1 )
  176. d = hb_dateSeconds( );
  177. }
  178. #endif
  179. return d;
  180. }
  181. HB_FUNC( SECONDSCPU )
  182. {
  183. hb_retnd( hb_secondsCPU( hb_parni( 1 ) ) );
  184. }
  185. #endif