1<?php 2 3/* 4 * This file is part of MailSo. 5 * 6 * (c) 2014 Usenko Timur 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace MailSo\Base; 13 14/** 15 * @category MailSo 16 * @package Base 17 */ 18class DateTimeHelper 19{ 20 /** 21 * @access private 22 */ 23 private function __construct() 24 { 25 } 26 27 /** 28 * @staticvar \DateTimeZone $oDateTimeZone 29 * 30 * @return \DateTimeZone 31 */ 32 public static function GetUtcTimeZoneObject() 33 { 34 static $oDateTimeZone = null; 35 if (null === $oDateTimeZone) 36 { 37 $oDateTimeZone = new \DateTimeZone('UTC'); 38 } 39 return $oDateTimeZone; 40 } 41 42 /** 43 * Parse date string formated as "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" 44 * RFC2822 45 * 46 * @param string $sDateTime 47 * 48 * @return int 49 */ 50 public static function ParseRFC2822DateString($sDateTime) 51 { 52 $sDateTime = \trim($sDateTime); 53 if (empty($sDateTime)) 54 { 55 return 0; 56 } 57 58 $sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', $sDateTime)); 59 $oDateTime = \DateTime::createFromFormat('D, d M Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); 60 return $oDateTime ? $oDateTime->getTimestamp() : 0; 61 } 62 63 /** 64 * Parse date string formated as "10-Jan-2012 01:58:17 -0800" 65 * IMAP INTERNALDATE Format 66 * 67 * @param string $sDateTime 68 * 69 * @return int 70 */ 71 public static function ParseInternalDateString($sDateTime) 72 { 73 $sDateTime = \trim($sDateTime); 74 if (empty($sDateTime)) 75 { 76 return 0; 77 } 78 79 if (\preg_match('/^[a-z]{2,4}, /i', $sDateTime)) // RFC2822 ~ "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" 80 { 81 return \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDateTime); 82 } 83 84 $oDateTime = \DateTime::createFromFormat('d-M-Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); 85 return $oDateTime ? $oDateTime->getTimestamp() : 0; 86 } 87 88 /** 89 * Parse date string formated as "2011-06-14 23:59:59 +0400" 90 * 91 * @param string $sDateTime 92 * 93 * @return int 94 */ 95 public static function ParseDateStringType1($sDateTime) 96 { 97 $sDateTime = \trim($sDateTime); 98 if (empty($sDateTime)) 99 { 100 return 0; 101 } 102 103 $oDateTime = \DateTime::createFromFormat('Y-m-d H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); 104 return $oDateTime ? $oDateTime->getTimestamp() : 0; 105 } 106 107 /** 108 * Parse date string formated as "2015-05-08T14:32:18.483-07:00" 109 * 110 * @param string $sDateTime 111 * 112 * @return int 113 */ 114 public static function TryToParseSpecEtagFormat($sDateTime) 115 { 116 $sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', \trim($sDateTime))); 117 $sDateTime = \trim(\preg_replace('/(:[\d]{2})\.[\d]{3}/', '$1', \trim($sDateTime))); 118 $sDateTime = \trim(\preg_replace('/(-[\d]{2})T([\d]{2}:)/', '$1 $2', \trim($sDateTime))); 119 $sDateTime = \trim(\preg_replace('/([\-+][\d]{2}):([\d]{2})$/', ' $1$2', \trim($sDateTime))); 120 121 return \MailSo\Base\DateTimeHelper::ParseDateStringType1($sDateTime); 122 } 123 124 /** 125 * @param string $sTime 126 * 127 * @return int 128 */ 129 public static function TimeToSec($sTime) 130 { 131 $iMod = 1; 132 $sTime = \trim($sTime); 133 if ('-' === \substr($sTime, 0, 1)) 134 { 135 $iMod = -1; 136 $sTime = \substr($sTime, 1); 137 } 138 139 $aParts = \preg_split('/[:.,]/', (string) $sTime); 140 141 $iResult = 0; 142 if (isset($aParts[0]) && \is_numeric($aParts[0])) 143 { 144 $iResult += 3600 * ((int) $aParts[0]); 145 } 146 147 if (isset($aParts[1]) && \is_numeric($aParts[1])) 148 { 149 $iResult += 60 * ((int) $aParts[1]); 150 } 151 152 return $iResult * $iMod; 153 } 154} 155