Passed
Push — master ( 2d2cc5...c6a2fc )
by Corey
03:13
created

Time::parse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
1
<?php
2
namespace common\components;
3
4
use yii;
5
use \DateTime;
6
use \DateTimeZone;
7
8
class Time extends \yii\base\BaseObject implements \common\interfaces\TimeInterface {
9
10
  public const EARLIEST_DATE = '2014-01-01';
11
12
  public $timezone;
13
14
  public function __construct(String $timezone, $config = []) {
15
    $this->timezone = $timezone;
16
    parent::__construct($config);
17
  }
18
19
  /*
20
   * Parses the supplied string into a `\DateTime` object of the
21
   * given `$format`. It assumes the supplied string is in the
22
   * timezone specified in $this->timezone.
23
   *
24
   * @param string $time the questionable time to parse
25
   * @param string $format the format `$time` is expected to be in
26
   * @return \DateTime the parsed time or false
27
   */
28
  public function parse(string $time, string $format = 'Y-m-d') {
29
    $dt = DateTime::createFromFormat($format, $time, new DateTimeZone($this->timezone));
30
    if($dt) {
31
      // for some reason, using createFromFromat adds in the time. The regular DateTime constructor _does not_ do this. We manually zero out the time here to make the DateTime objects match.
32
      $dt->setTime(0, 0, 0);
33
      $formatted = $dt->format($format);
34
      if($formatted === $time && $this->inBounds($dt)) {
35
        return $dt;
36
      }
37
    }
38
    return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type DateTime.
Loading history...
39
  }
40
41
  /*
42
   * Checks if the given `\DateTime` is within acceptable date bounds.
43
   * It does no good to have the date be far in the past or in the future.
44
   *
45
   * @param \DateTime $dt
46
   * @return boolean
47
   */
48
  public function inBounds(DateTime $dt) {
49
    $first = strtotime((new DateTime(self::EARLIEST_DATE))->format('Y-m-d'));
50
    $test  = strtotime($dt->format('Y-m-d'));
51
    $now   = strtotime($this->getLocalDate());
52
53
    if($first <= $test && $test <= $now) {
54
      return true;
55
    } else {
56
      return false;
57
    }
58
  }
59
60
  public function convertLocalToUTC($local, $inc_time = true) {
61
    $fmt = $inc_time ? "Y-m-d H:i:s" : "Y-m-d";
62
63
    $timestamp = new DateTime($local, new DateTimeZone($this->timezone));
64
    $timestamp->setTimeZone(new DateTimeZone("UTC"));
65
    return $timestamp->format($fmt);
66
  }
67
68
  public function convertUTCToLocal($utc, $iso = true) {
69
    $fmt = $iso ? Datetime::ATOM : "Y-m-d H:i:s";
70
71
    $timestamp = new DateTime($utc, new DateTimeZone("UTC"));
72
    $timestamp->setTimeZone(new DateTimeZone($this->timezone));
73
    return $timestamp->format($fmt);
74
  }
75
76
  public function getLocalTime($timezone = null) {
77
    if($timezone === null)
78
      $timezone = $this->timezone;
79
80
    $timestamp = new DateTime("now", new DateTimeZone($timezone));
81
    return $timestamp->format("Y-m-d H:i:s");
82
  }
83
84
  public function getLocalDate($timezone = null) {
85
    if($timezone === null)
86
      $timezone = $this->timezone;
87
88
    return (new DateTime("now", new DateTimeZone($timezone)))
89
      ->format("Y-m-d");
90
  }
91
92
  public function alterLocalDate($date, $modifier) {
93
    return (new DateTime("$date $modifier", new DateTimeZone($this->timezone)))
94
      ->format("Y-m-d");
95
  }
96
97
  public function getUTCBookends($local) {
98
    $local = trim($local);
99
    if(strpos($local, " ")) {
100
      return false;
101
    }
102
103
    $start = $local . " 00:00:00";
104
    $end   = $local . "23:59:59";
105
106
    $front = self::convertLocalToUTC($start);
0 ignored issues
show
Bug Best Practice introduced by
The method common\components\Time::convertLocalToUTC() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
    /** @scrutinizer ignore-call */ 
107
    $front = self::convertLocalToUTC($start);
Loading history...
107
    $back  = self::convertLocalToUTC($end);
108
109
    return [$front, $back];
110
  }
111
}
112