File: MicrotimeConverter.php

package info (click to toggle)
php-mock 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: php: 1,740; makefile: 18; xml: 17; sh: 7
file content (45 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

namespace phpmock\functions;

/**
 * Converts PHP's microtime string format into a float and vice versa.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @internal
 */
class MicrotimeConverter
{
    /**
     * Converts a string microtime into a float.
     *
     * @param string $microtime The microtime.
     * @return float The microtime as float.
     */
    public function convertStringToFloat($microtime)
    {
        /*
         * This is from the manual:
         * http://php.net/manual/en/function.microtime.php
         */
        // list($usec, $sec) = explode(" ", $microtime);

        // This seems to be more intuitive as an inverse function.
        list($usec, $sec) = sscanf($microtime, "%f %d");

        return ((float)$usec + (float)$sec);
    }

    /**
     * Converts a float microtime in PHP's microtime() string format.
     *
     * @param float $microtime The microtime.
     * @return string The microtime as string.
     */
    public function convertFloatToString($microtime)
    {
        return sprintf("%0.8F %d", fmod($microtime, 1), $microtime);
    }
}