8 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9 * @author Evert Pot (http://www.rooftopsolutions.nl/)
10 * @author Paul Voegler
11 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
13 class Sabre_HTTP_Util {
16 * Parses a RFC2616-compatible date string
18 * This method returns false if the date is invalid
20 * @param string $dateHeader
21 * @return bool|DateTime
23 static function parseHTTPDate($dateHeader) {
25 //RFC 2616 section 3.3.1 Full Date
26 //Only the format is checked, valid ranges are checked by strtotime below
27 $month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
28 $weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
29 $wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
30 $time = '[0-2]\d(\:[0-5]\d){2}';
31 $date3 = $month . ' ([1-3]\d| \d)';
32 $date2 = '[0-3]\d\-' . $month . '\-\d\d';
33 //4-digit year cannot begin with 0 - unix timestamp begins in 1970
34 $date1 = '[0-3]\d ' . $month . ' [1-9]\d{3}';
36 //ANSI C's asctime() format
37 //4-digit year cannot begin with 0 - unix timestamp begins in 1970
38 $asctime_date = $wkday . ' ' . $date3 . ' ' . $time . ' [1-9]\d{3}';
39 //RFC 850, obsoleted by RFC 1036
40 $rfc850_date = $weekday . ', ' . $date2 . ' ' . $time . ' GMT';
41 //RFC 822, updated by RFC 1123
42 $rfc1123_date = $wkday . ', ' . $date1 . ' ' . $time . ' GMT';
43 //allowed date formats by RFC 2616
44 $HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)";
46 //allow for space around the string and strip it
47 $dateHeader = trim($dateHeader, ' ');
48 if (!preg_match('/^' . $HTTP_date . '$/', $dateHeader))
51 //append implicit GMT timezone to ANSI C time format
52 if (strpos($dateHeader, ' GMT') === false)
53 $dateHeader .= ' GMT';
56 $realDate = strtotime($dateHeader);
57 //strtotime can return -1 or false in case of error
58 if ($realDate !== false && $realDate >= 0)
59 return new DateTime('@' . $realDate, new DateTimeZone('UTC'));
64 * Transforms a DateTime object to HTTP's most common date format.
66 * We're serializing it as the RFC 1123 date, which, for HTTP must be
69 * @param DateTime $dateTime
72 static function toHTTPDate(DateTime $dateTime) {
74 // We need to clone it, as we don't want to affect the existing
76 $dateTime = clone $dateTime;
77 $dateTime->setTimeZone(new DateTimeZone('GMT'));
78 return $dateTime->format('D, d M Y H:i:s \G\M\T');