Passed
Push — master ( fd5fbe...6ba584 )
by Sebastian
03:41
created

Microtime::getMySQLDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils;
6
7
use DateTime;
8
use DateTimeZone;
9
use Exception;
10
11
class Microtime extends DateTime implements Interface_Stringable
12
{
13
    const ERROR_FAILED_CREATING_DATE_OBJECT = 88601;
14
    const ERROR_FAILED_CONVERTING_STRING = 88602;
15
16
    const FORMAT_ISO = 'Y-m-d H:i:s.u';
17
18
    /**
19
     * @param string $datetime
20
     * @param DateTimeZone|null $timeZone
21
     * @throws ConvertHelper_Exception
22
     *
23
     * @see Microtime::ERROR_FAILED_CREATING_DATE_OBJECT
24
     * @see Microtime::ERROR_FAILED_CONVERTING_STRING
25
     */
26
    public function __construct($datetime='now', DateTimeZone $timeZone=null)
27
    {
28
        if($timeZone === null)
29
        {
30
            $timeZone = new DateTimeZone(date_default_timezone_get());
31
        }
32
33
        if(empty($datetime) || $datetime === 'now')
34
        {
35
            $dateObj = DateTime::createFromFormat('0.u00 U', microtime(), new DateTimeZone('America/Denver'));
36
37
            if($dateObj === false) {
38
                throw new ConvertHelper_Exception(
39
                    'Failed to create microseconds date.',
40
                    '',
41
                    self::ERROR_FAILED_CREATING_DATE_OBJECT
42
                );
43
            }
44
45
            $dateObj->setTimezone($timeZone);
46
            $datetime = $dateObj->format(self::FORMAT_ISO);
47
        }
48
49
        try
50
        {
51
            parent::__construct($datetime, $timeZone);
52
        }
53
        catch (Exception $e)
54
        {
55
            throw new ConvertHelper_Exception(
56
                'Failed to create date from string.',
57
                sprintf(
58
                    'Source date string: [%s].',
59
                    strval($datetime)
60
                ),
61
                self::ERROR_FAILED_CONVERTING_STRING
62
            );
63
        }
64
    }
65
66
    public static function createFromDate(DateTime $date) : Microtime
67
    {
68
        return new Microtime($date->format(self::FORMAT_ISO), $date->getTimezone());
69
    }
70
71
    /**
72
     * Gets the microseconds part of the date.
73
     * @return int Six-digit microseconds value.
74
     */
75
    public function getMicroseconds() : int
76
    {
77
        return intval($this->format('u'));
78
    }
79
80
    /**
81
     * ISO formatted date with microseconds, in the
82
     * format `Y-m-d H:i:s.u`.
83
     *
84
     * @return string
85
     */
86
    public function getISODate() : string
87
    {
88
        return $this->format(self::FORMAT_ISO);
89
    }
90
91
    /**
92
     * Date formatted for storing in a MySQL database column.
93
     *
94
     * NOTE: To store microseconds in MySQL, a DateTime column
95
     * needs to be used, with a length of 6 (3 for the milliseconds,
96
     * +3 for the microseconds). Without the length specified,
97
     * the milliseconds information will be stripped out.
98
     *
99
     * @return string
100
     */
101
    public function getMySQLDate() : string
102
    {
103
        return $this->getISODate();
104
    }
105
106
    public function __toString()
107
    {
108
        return $this->getISODate();
109
    }
110
}
111