/src/Types/XDateTime.php

https://github.com/linepogl/oxygen · PHP · 206 lines · 159 code · 28 blank · 19 comment · 25 complexity · 20327ff999e38b8e3a14cc5a674d18d9 MD5 · raw file

  1. <?php
  2. class XDateTime extends XValue implements Serializable {
  3. private $timestamp;
  4. public static function Make($year,$month,$day,$hours=0,$minutes=0,$seconds=0){
  5. return new static(mktime($hours,$minutes,$seconds,$month,$day,$year));
  6. }
  7. public function VarExport() { return '(new '.get_called_class().'('.$this->timestamp.'))'; }
  8. public function __construct($timestamp = null){
  9. if ($timestamp instanceof DateTime) $timestamp = $timestamp->getTimestamp();
  10. $this->timestamp = is_null($timestamp) ? microtime(true) : $timestamp;
  11. }
  12. public function MetaType(){ return MetaDateTime::Type(); }
  13. public function Serialize(){ return serialize( $this->timestamp ); }
  14. public function Unserialize($data){ $this->timestamp = unserialize( $data ); }
  15. public static function Now(){ return new XDateTime(); }
  16. public function GetYear() { return intval(date('Y',$this->timestamp)); }
  17. public function GetMonth() { return intval(date('m',$this->timestamp)); }
  18. public function GetDay() { return intval(date('d',$this->timestamp)); }
  19. public function GetHours() { return intval(date('H',$this->timestamp)); }
  20. public function GetMinutes() { return intval(date('i',$this->timestamp)); }
  21. public function GetSeconds() { return intval(date('s',$this->timestamp)); }
  22. public function GetWeek() { return intval(date('W',$this->timestamp)); }
  23. public function GetDayOfWeek($first_day_of_week = 0) { return ( intval(date('w',$this->timestamp)) + (7-$first_day_of_week) ) % 7; }
  24. public function GetDaysInMonth() { return intval(date('t',$this->timestamp)); }
  25. public function GetTimestamp() { return $this->timestamp; }
  26. public function GetDate() { return new XDate($this->timestamp); }
  27. public function GetTime() { return new XTime($this->timestamp); }
  28. public function AsDate() { return new XDate($this->timestamp); }
  29. public function AsTime() { return new XTime($this->timestamp); }
  30. public function AsInt() { return intval($this->timestamp); }
  31. public function IsDate(){ return $this->Format('His') === '000000'; }
  32. public function IsEqualTo( $x ){
  33. if ($x instanceof XDateTime) return $this->timestamp == $x->timestamp;
  34. if ($x instanceof DateTime) return $this->timestamp == $x->getTimestamp();
  35. if (is_int($x)||is_float($x)) return $this->timestamp == $x;
  36. return parent::IsEqualTo($x);
  37. }
  38. public function CompareTo( $x ){
  39. if ($x instanceof XDateTime) return $this->timestamp - $x->timestamp;
  40. if ($x instanceof DateTime) return $this->timestamp - $x->getTimestamp();
  41. if (is_int($x)||is_float($x)) return $this->timestamp - $x;
  42. return parent::CompareTo($x);
  43. }
  44. public function FromLess( XDateTime $that = null ){ return $that === null ? false : $this->timestamp < $that->timestamp; }
  45. public function FromLessEqual( XDateTime $that = null ){ return $that === null ? false : $this->timestamp <= $that->timestamp; }
  46. public function TillLess( XDateTime $that = null ){ return $that === null ? true : $this->timestamp < $that->timestamp; }
  47. public function TillLessEqual( XDateTime $that = null ){ return $that === null ? true : $this->timestamp <= $that->timestamp; }
  48. /** @return static */ public static function FromMax( XDateTime $x = null, XDateTime $y = null ) { if ($x === null) return $y; if ($y === null) return $x; return $x->CompareTo($y) > 0 ? $x : $y; }
  49. /** @return static */ public static function FromMin( XDateTime $x = null, XDateTime $y = null ) { if ($x === null || $y === null) return null; return $x->CompareTo($y) < 0 ? $x : $y; }
  50. /** @return static */ public static function TillMax( XDateTime $x = null, XDateTime $y = null ) { if ($x === null || $y === null) return null; return $x->CompareTo($y) > 0 ? $x : $y; }
  51. /** @return static */ public static function TillMin( XDateTime $x = null, XDateTime $y = null ) { if ($x === null) return $y; if ($y === null) return $x; return $x->CompareTo($y) < 0 ? $x : $y; }
  52. /** @return XTimeSpan */
  53. public function Diff(XDateTime $value){
  54. return new XTimeSpan(($this->GetTimestamp() - $value->GetTimestamp())*1000);
  55. }
  56. /** @return XDateTime */
  57. public function AddYears($value){
  58. return new XDateTime(mktime(
  59. $this->GetHours()
  60. ,$this->GetMinutes()
  61. ,$this->GetSeconds()
  62. ,$this->GetMonth()
  63. ,$this->GetDay()
  64. ,$this->GetYear() + $value
  65. ));
  66. }
  67. /** @return XDateTime */
  68. public function AddMonths($value){
  69. return new XDateTime(mktime(
  70. $this->GetHours()
  71. ,$this->GetMinutes()
  72. ,$this->GetSeconds()
  73. ,$this->GetMonth() + $value
  74. ,$this->GetDay()
  75. ,$this->GetYear()
  76. ));
  77. }
  78. /** @return XDateTime */
  79. public function AddDays($value){
  80. return new XDateTime(mktime(
  81. $this->GetHours()
  82. ,$this->GetMinutes()
  83. ,$this->GetSeconds()
  84. ,$this->GetMonth()
  85. ,$this->GetDay() + $value
  86. ,$this->GetYear()
  87. ));
  88. }
  89. /** @return XDateTime */
  90. public function AddHours($value){
  91. return new XDateTime(mktime(
  92. $this->GetHours() + $value
  93. ,$this->GetMinutes()
  94. ,$this->GetSeconds()
  95. ,$this->GetMonth()
  96. ,$this->GetDay()
  97. ,$this->GetYear()
  98. ));
  99. }
  100. /** @return XDateTime */
  101. public function AddMinutes($value){
  102. return new XDateTime(mktime(
  103. $this->GetHours()
  104. ,$this->GetMinutes() + $value
  105. ,$this->GetSeconds()
  106. ,$this->GetMonth()
  107. ,$this->GetDay()
  108. ,$this->GetYear()
  109. ));
  110. }
  111. /** @return XDateTime */
  112. public function AddSeconds($value){
  113. return new XDateTime(mktime(
  114. $this->GetHours()
  115. ,$this->GetMinutes()
  116. ,$this->GetSeconds() + $value
  117. ,$this->GetMonth()
  118. ,$this->GetDay()
  119. ,$this->GetYear()
  120. ));
  121. }
  122. /** @return XDateTime */
  123. public function AddTimeSpan(XTimeSpan $value){
  124. return $this->AddSeconds($value->GetTotalSeconds());
  125. }
  126. /** @return static */
  127. public function ToServerTimeZone() { return $this->ToTimeZone(Oxygen::GetServerTimeZone()); }
  128. /** @return static */
  129. public function ToTimeZone($timezone) {
  130. if ($timezone === null) return $this;
  131. $current_timezone = Oxygen::GetClientTimeZone();
  132. if ($timezone === $current_timezone) return $this;
  133. $z1 = new DateTimeZone($current_timezone);
  134. $z2 = new DateTimeZone($timezone);
  135. $d = new DateTime('@'.$this->timestamp);
  136. $o1 = $z1->getOffset($d);
  137. $o2 = $z2->getOffset($d);
  138. return new static( $this->timestamp - $o1 + $o2 );
  139. }
  140. /**
  141. * @param string $value
  142. * @param string $format --see date()
  143. * @return XDateTime
  144. */
  145. public static function Parse($value,$format){
  146. $d = DateTime::createFromFormat($format,$value);
  147. if ($d === false) {
  148. throw new Exception('Invalid date or time.');
  149. }
  150. else
  151. return new static($d->getTimestamp());
  152. }
  153. private static $translations = array('%'=>'%%','d'=>'%d','D'=>'%a','j'=>'%d','l'=>'%A','N'=>'%u','S'=>null,'w'=>'%w','z'=>'%j','W'=>'%W','F'=>'%B','m'=>'%m','M'=>'%b','n'=>null,'t'=>null,'L'=>null,'o'=>'%G','Y'=>'%Y','y'=>'%y','a'=>'%P','A'=>'%p','B'=>null,'g'=>'%l','G'=>null,'h'=>'%I','H'=>'%H','i'=>'%M','s'=>'%S','u'=>null,'e'=>null,'I'=>null,'O'=>'%Z','P'=>null,'T'=>null,'Z'=>null,'c'=>'%Y-%m-%dT%H:%M:%S%Z','r'=>'%a, %e %b %Y %H:%M:%S %Z','U'=>null);
  154. private static $translated_formats_cache = array();
  155. private static function translate_format($format){
  156. if (!array_key_exists($format,self::$translated_formats_cache)){
  157. $f = ''; $escape = false;
  158. foreach (str_split($format) as $x){
  159. if ($escape){ $f .= $x; $escape = false; }
  160. elseif ($x == '\\') { $escape = true; continue; }
  161. else $f .= array_key_exists($x,self::$translations) ? self::$translations[$x] : $x;
  162. }
  163. self::$translated_formats_cache[$format] = $f;
  164. }
  165. return self::$translated_formats_cache[$format];
  166. }
  167. /**
  168. * @param string $format --see date()
  169. * @return string
  170. */
  171. public function Format($format){
  172. $r = strftime(self::translate_format($format),$this->timestamp);
  173. return $r!==false ? Str::ForceUTF8($r) : date($format,$this->timestamp);
  174. }
  175. public static function GetTimeZones(){
  176. return array_filter( timezone_identifiers_list() , function($x){ return $x !== 'UTC'; } );
  177. }
  178. }