summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/lib/PicoFeed/Parser/DateParser.php
blob: 89f189e7ab2e3f6a2ebcdb6a57006ca00ecc668f (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace PicoFeed\Parser;

use DateTime;
use DateTimeZone;

/**
 * Date Parser
 *
 * @author  Frederic Guillot
 * @package Parser
 */
class DateParser
{
    /**
     * Timezone used to parse feed dates
     *
     * @access public
     * @var string
     */
    public $timezone = 'UTC';

    /** 
     * Supported formats [ 'format' => length ]
     *
     * @access public
     * @var array
     */
    public $formats = array(
        DATE_ATOM => null,
        DATE_RSS => null,
        DATE_COOKIE => null,
        DATE_ISO8601 => null,
        DATE_RFC822 => null,
        DATE_RFC850 => null,
        DATE_RFC1036 => null,
        DATE_RFC1123 => null,
        DATE_RFC2822 => null,
        DATE_RFC3339 => null,
        'D, d M Y H:i:s' => 25,
        'D, d M Y h:i:s' => 25,
        'D M d Y H:i:s' => 24,
        'j M Y H:i:s' => 20,
        'Y-m-d H:i:s' => 19,
        'Y-m-d\TH:i:s' => 19,
        'd/m/Y H:i:s' => 19,
        'D, d M Y' => 16,
        'Y-m-d' => 10,
        'd-m-Y' => 10,
        'm-d-Y' => 10,
        'd.m.Y' => 10,
        'm.d.Y' => 10,
        'd/m/Y' => 10,
        'm/d/Y' => 10,
    );

    /**
     * Try to parse all date format for broken feeds
     *
     * @access public
     * @param  string  $value  Original date format
     * @return integer         Timestamp
     */
    public function getTimestamp($value)
    {
        $value = trim($value);

        foreach ($this->formats as $format => $length) {

            $truncated_value = $value;
            if ($length !== null) {
                $truncated_value = substr($truncated_value, 0, $length);
            }

            $timestamp = $this->getValidDate($format, $truncated_value);
            if ($timestamp > 0) {
                return $timestamp;
            }
        }

        $date = new DateTime('now', new DateTimeZone($this->timezone));
        return $date->getTimestamp();
    }

    /**
     * Get a valid date from a given format
     *
     * @access public
     * @param  string  $format   Date format
     * @param  string  $value    Original date value
     * @return integer           Timestamp
     */
    public function getValidDate($format, $value)
    {
        $date = DateTime::createFromFormat($format, $value, new DateTimeZone($this->timezone));

        if ($date !== false) {

            $errors = DateTime::getLastErrors();

            if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
                return $date->getTimestamp();
            }
        }

        return 0;
    }
}