PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/php/ext/standard/microtime.c

https://gitlab.com/envieidoc/tomato
C | 166 lines | 106 code | 25 blank | 35 comment | 13 complexity | 22966b960af0ebdad5950d71d10ec69c MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2014 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Paul Panotzki - Bunyip Information Systems |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. #ifdef PHP_WIN32
  24. #include "win32/time.h"
  25. #elif defined(NETWARE)
  26. #include <sys/timeval.h>
  27. #include <sys/time.h>
  28. #else
  29. #include <sys/time.h>
  30. #endif
  31. #ifdef HAVE_SYS_RESOURCE_H
  32. #include <sys/resource.h>
  33. #endif
  34. #ifdef HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <stdio.h>
  40. #include <errno.h>
  41. #include "microtime.h"
  42. #include "ext/date/php_date.h"
  43. #define NUL '\0'
  44. #define MICRO_IN_SEC 1000000.00
  45. #define SEC_IN_MIN 60
  46. #ifdef HAVE_GETTIMEOFDAY
  47. static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
  48. {
  49. zend_bool get_as_float = 0;
  50. struct timeval tp = {0};
  51. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) {
  52. return;
  53. }
  54. if (gettimeofday(&tp, NULL)) {
  55. RETURN_FALSE;
  56. }
  57. if (get_as_float) {
  58. RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
  59. }
  60. if (mode) {
  61. timelib_time_offset *offset;
  62. offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info(TSRMLS_C));
  63. array_init(return_value);
  64. add_assoc_long(return_value, "sec", tp.tv_sec);
  65. add_assoc_long(return_value, "usec", tp.tv_usec);
  66. add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
  67. add_assoc_long(return_value, "dsttime", offset->is_dst);
  68. timelib_time_offset_dtor(offset);
  69. } else {
  70. char ret[100];
  71. snprintf(ret, 100, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec);
  72. RETURN_STRING(ret, 1);
  73. }
  74. }
  75. /* {{{ proto mixed microtime([bool get_as_float])
  76. Returns either a string or a float containing the current time in seconds and microseconds */
  77. PHP_FUNCTION(microtime)
  78. {
  79. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  80. }
  81. /* }}} */
  82. /* {{{ proto array gettimeofday([bool get_as_float])
  83. Returns the current time as array */
  84. PHP_FUNCTION(gettimeofday)
  85. {
  86. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  87. }
  88. #endif
  89. /* }}} */
  90. #ifdef HAVE_GETRUSAGE
  91. /* {{{ proto array getrusage([int who])
  92. Returns an array of usage statistics */
  93. PHP_FUNCTION(getrusage)
  94. {
  95. struct rusage usg;
  96. long pwho = 0;
  97. int who = RUSAGE_SELF;
  98. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &pwho) == FAILURE) {
  99. return;
  100. }
  101. if (pwho == 1) {
  102. who = RUSAGE_CHILDREN;
  103. }
  104. memset(&usg, 0, sizeof(struct rusage));
  105. if (getrusage(who, &usg) == -1) {
  106. RETURN_FALSE;
  107. }
  108. array_init(return_value);
  109. #define PHP_RUSAGE_PARA(a) \
  110. add_assoc_long(return_value, #a, usg.a)
  111. #if !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct */
  112. PHP_RUSAGE_PARA(ru_oublock);
  113. PHP_RUSAGE_PARA(ru_inblock);
  114. PHP_RUSAGE_PARA(ru_msgsnd);
  115. PHP_RUSAGE_PARA(ru_msgrcv);
  116. PHP_RUSAGE_PARA(ru_maxrss);
  117. PHP_RUSAGE_PARA(ru_ixrss);
  118. PHP_RUSAGE_PARA(ru_idrss);
  119. PHP_RUSAGE_PARA(ru_minflt);
  120. PHP_RUSAGE_PARA(ru_majflt);
  121. PHP_RUSAGE_PARA(ru_nsignals);
  122. PHP_RUSAGE_PARA(ru_nvcsw);
  123. PHP_RUSAGE_PARA(ru_nivcsw);
  124. PHP_RUSAGE_PARA(ru_nswap);
  125. #endif /*_OSD_POSIX*/
  126. PHP_RUSAGE_PARA(ru_utime.tv_usec);
  127. PHP_RUSAGE_PARA(ru_utime.tv_sec);
  128. PHP_RUSAGE_PARA(ru_stime.tv_usec);
  129. PHP_RUSAGE_PARA(ru_stime.tv_sec);
  130. #undef PHP_RUSAGE_PARA
  131. }
  132. #endif /* HAVE_GETRUSAGE */
  133. /* }}} */
  134. /*
  135. * Local variables:
  136. * tab-width: 4
  137. * c-basic-offset: 4
  138. * End:
  139. * vim600: sw=4 ts=4 fdm=marker
  140. * vim<600: sw=4 ts=4
  141. */