PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PelEntryTime.php

http://github.com/lsolesen/pel
PHP | 318 lines | 115 code | 19 blank | 184 comment | 23 complexity | cd60ea6feacd556b656ec3d2fefe21c2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * PEL: PHP Exif Library.
  4. * A library with support for reading and
  5. * writing all Exif headers in JPEG and TIFF images using PHP.
  6. *
  7. * Copyright (C) 2004, 2005, 2006, 2007 Martin Geisler.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program in the file COPYING; if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  22. * Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * Class for holding a date and time.
  26. *
  27. * This class can hold a timestamp, and it will be used as
  28. * in this example where the time is advanced by one week:
  29. * <code>
  30. * $entry = $ifd->getEntry(PelTag::DATE_TIME_ORIGINAL);
  31. * $time = $entry->getValue();
  32. * print('The image was taken on the ' . date('jS', $time));
  33. * $entry->setValue($time + 7 * 24 * 3600);
  34. * </code>
  35. *
  36. * The example used a standard UNIX timestamp, which is the default
  37. * for this class.
  38. *
  39. * But the Exif format defines dates outside the range of a UNIX
  40. * timestamp (about 1970 to 2038) and so you can also get access to
  41. * the timestamp in two other formats: a simple string or a Julian Day
  42. * Count. Please see the Calendar extension in the PHP Manual for more
  43. * information about the Julian Day Count.
  44. *
  45. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  46. * @package PEL
  47. */
  48. namespace lsolesen\pel;
  49. class PelEntryTime extends PelEntryAscii
  50. {
  51. /**
  52. * Constant denoting a UNIX timestamp.
  53. */
  54. const UNIX_TIMESTAMP = 1;
  55. /**
  56. * Constant denoting a Exif string.
  57. */
  58. const EXIF_STRING = 2;
  59. /**
  60. * Constant denoting a Julian Day Count.
  61. */
  62. const JULIAN_DAY_COUNT = 3;
  63. /**
  64. * The Julian Day Count of the timestamp held by this entry.
  65. *
  66. * This is an integer counting the number of whole days since
  67. * January 1st, 4713 B.C. The fractional part of the timestamp held
  68. * by this entry is stored in {@link $seconds}.
  69. *
  70. * @var int
  71. */
  72. private $day_count;
  73. /**
  74. * The number of seconds into the day of the timestamp held by this
  75. * entry.
  76. *
  77. * The number of whole days is stored in {@link $day_count} and the
  78. * number of seconds left-over is stored here.
  79. *
  80. * @var int
  81. */
  82. private $seconds;
  83. /**
  84. * Make a new entry for holding a timestamp.
  85. *
  86. * @param integer $tag
  87. * the Exif tag which this entry represents. There are
  88. * only three standard tags which hold timestamp, so this should be
  89. * one of the constants {@link PelTag::DATE_TIME}, {@link
  90. * PelTag::DATE_TIME_ORIGINAL}, or {@link
  91. * PelTag::DATE_TIME_DIGITIZED}.
  92. * @param integer|string|double $timestamp
  93. * the timestamp held by this entry in the correct form
  94. * as indicated by the third argument. For {@link UNIX_TIMESTAMP}
  95. * this is an integer counting the number of seconds since January
  96. * 1st 1970, for {@link EXIF_STRING} this is a string of the form
  97. * 'YYYY:MM:DD hh:mm:ss', and for {@link JULIAN_DAY_COUNT} this is a
  98. * floating point number where the integer part denotes the day
  99. * count and the fractional part denotes the time of day (0.25 means
  100. * 6:00, 0.75 means 18:00).
  101. * @param integer $type
  102. * the type of the timestamp. This must be one of
  103. * {@link UNIX_TIMESTAMP}, {@link EXIF_STRING}, or
  104. * {@link JULIAN_DAY_COUNT}.
  105. */
  106. public function __construct($tag, $timestamp, $type = self::UNIX_TIMESTAMP)
  107. {
  108. $this->tag = $tag;
  109. $this->setValue($timestamp, $type);
  110. }
  111. /**
  112. * Return the timestamp of the entry.
  113. *
  114. * The timestamp held by this entry is returned in one of three
  115. * formats: as a standard UNIX timestamp (default), as a fractional
  116. * Julian Day Count, or as a string.
  117. *
  118. * @param integer $type
  119. * the type of the timestamp. This must be one of
  120. * {@link UNIX_TIMESTAMP}, {@link EXIF_STRING}, or
  121. * {@link JULIAN_DAY_COUNT}.
  122. * @return integer|string|false the timestamp held by this entry in the correct form
  123. * as indicated by the type argument. For {@link UNIX_TIMESTAMP}
  124. * this is an integer counting the number of seconds since January
  125. * 1st 1970, for {@link EXIF_STRING} this is a string of the form
  126. * 'YYYY:MM:DD hh:mm:ss', and for {@link JULIAN_DAY_COUNT} this is a
  127. * floating point number where the integer part denotes the day
  128. * count and the fractional part denotes the time of day (0.25 means
  129. * 6:00, 0.75 means 18:00).
  130. */
  131. public function getValue($type = self::UNIX_TIMESTAMP)
  132. {
  133. switch ($type) {
  134. case self::UNIX_TIMESTAMP:
  135. $seconds = $this->convertJdToUnix($this->day_count);
  136. if ($seconds === false) {
  137. /*
  138. * We get false if the Julian Day Count is outside the range
  139. * of a UNIX timestamp.
  140. */
  141. return false;
  142. } else {
  143. return $seconds + $this->seconds;
  144. }
  145. break;
  146. case self::EXIF_STRING:
  147. list ($year, $month, $day) = $this->convertJdToGregorian($this->day_count);
  148. $hours = (int) ($this->seconds / 3600);
  149. $minutes = (int) ($this->seconds % 3600 / 60);
  150. $seconds = $this->seconds % 60;
  151. return sprintf('%04d:%02d:%02d %02d:%02d:%02d', $year, $month, $day, $hours, $minutes, $seconds);
  152. case self::JULIAN_DAY_COUNT:
  153. return $this->day_count + $this->seconds / 86400;
  154. default:
  155. throw new PelInvalidArgumentException('Expected UNIX_TIMESTAMP (%d), ' . 'EXIF_STRING (%d), or ' . 'JULIAN_DAY_COUNT (%d) for $type, got %d.', self::UNIX_TIMESTAMP, self::EXIF_STRING, self::JULIAN_DAY_COUNT, $type);
  156. }
  157. }
  158. /**
  159. * Update the timestamp held by this entry.
  160. *
  161. * @param integer|float|string $timestamp
  162. * the timestamp held by this entry in the correct form
  163. * as indicated by the third argument. For {@link UNIX_TIMESTAMP}
  164. * this is an integer counting the number of seconds since January
  165. * 1st 1970, for {@link EXIF_STRING} this is a string of the form
  166. * 'YYYY:MM:DD hh:mm:ss', and for {@link JULIAN_DAY_COUNT} this is a
  167. * floating point number where the integer part denotes the day
  168. * count and the fractional part denotes the time of day (0.25 means
  169. * 6:00, 0.75 means 18:00).
  170. * @param integer $type
  171. * the type of the timestamp. This must be one of
  172. * {@link UNIX_TIMESTAMP}, {@link EXIF_STRING}, or
  173. * {@link JULIAN_DAY_COUNT}.
  174. * @throws PelInvalidArgumentException
  175. */
  176. public function setValue($timestamp, $type = self::UNIX_TIMESTAMP)
  177. {
  178. if ($type === self::UNIX_TIMESTAMP) {
  179. if (is_int($timestamp) || is_float($timestamp)) {
  180. $this->day_count = $this->convertUnixToJd($timestamp);
  181. $this->seconds = $timestamp % 86400;
  182. } else {
  183. throw new PelInvalidArgumentException('Expected integer value for $type, got %s', gettype($timestamp));
  184. }
  185. } elseif ($type === self::EXIF_STRING) {
  186. /*
  187. * Clean the timestamp: some timestamps are broken other
  188. * separators than ':' and ' '.
  189. */
  190. $d = preg_split('/[^0-9]+/', $timestamp);
  191. for ($i = 0; $i < 6; $i ++) {
  192. if (empty($d[$i])) {
  193. $d[$i] = 0;
  194. }
  195. }
  196. $this->day_count = $this->convertGregorianToJd($d[0], $d[1], $d[2]);
  197. $this->seconds = $d[3] * 3600 + $d[4] * 60 + $d[5];
  198. } elseif ($type === self::JULIAN_DAY_COUNT) {
  199. if (is_int($timestamp) || is_float($timestamp)) {
  200. $this->day_count = (int) floor($timestamp);
  201. $this->seconds = (int) (86400 * ($timestamp - floor($timestamp)));
  202. } else {
  203. throw new PelInvalidArgumentException('Expected integer value for $type, got %s', gettype($timestamp));
  204. }
  205. } else {
  206. throw new PelInvalidArgumentException('Expected UNIX_TIMESTAMP (%d), ' . 'EXIF_STRING (%d), or ' . 'JULIAN_DAY_COUNT (%d) for $type, got %d.', self::UNIX_TIMESTAMP, self::EXIF_STRING, self::JULIAN_DAY_COUNT, $type);
  207. }
  208. // finally update the string which will be used when this is turned into bytes.
  209. parent::setValue($this->getValue(self::EXIF_STRING));
  210. }
  211. // The following four functions are used for converting back and
  212. // forth between the date formats. They are used in preference to
  213. // the ones from the PHP calendar extension to avoid having to
  214. // fiddle with timezones and to avoid depending on the extension.
  215. //
  216. // See http://www.hermetic.ch/cal_stud/jdn.htm#comp for a reference.
  217. /**
  218. * Converts a date in year/month/day format to a Julian Day count.
  219. *
  220. * @param integer $year
  221. * the year.
  222. * @param integer $month
  223. * the month, 1 to 12.
  224. * @param integer $day
  225. * the day in the month.
  226. * @return integer the Julian Day count.
  227. */
  228. public function convertGregorianToJd($year, $month, $day)
  229. {
  230. // Special case mapping 0/0/0 -> 0
  231. if ($year == 0 || $month == 0 || $day == 0) {
  232. return 0;
  233. }
  234. $m1412 = ($month <= 2) ? - 1 : 0;
  235. return floor((1461 * ($year + 4800 + $m1412)) / 4) + floor((367 * ($month - 2 - 12 * $m1412)) / 12) - floor((3 * floor(($year + 4900 + $m1412) / 100)) / 4) + $day - 32075;
  236. }
  237. /**
  238. * Converts a Julian Day count to a year/month/day triple.
  239. *
  240. * @param int $jd
  241. * the Julian Day count.
  242. * @return array an array with three entries: year, month, day.
  243. */
  244. public function convertJdToGregorian($jd)
  245. {
  246. // Special case mapping 0 -> 0/0/0
  247. if ($jd == 0) {
  248. return [
  249. 0,
  250. 0,
  251. 0
  252. ];
  253. }
  254. $l = $jd + 68569;
  255. $n = floor((4 * $l) / 146097);
  256. $l = $l - floor((146097 * $n + 3) / 4);
  257. $i = floor((4000 * ($l + 1)) / 1461001);
  258. $l = $l - floor((1461 * $i) / 4) + 31;
  259. $j = floor((80 * $l) / 2447);
  260. $d = $l - floor((2447 * $j) / 80);
  261. $l = floor($j / 11);
  262. $m = $j + 2 - (12 * $l);
  263. $y = 100 * ($n - 49) + $i + $l;
  264. return [
  265. $y,
  266. $m,
  267. $d
  268. ];
  269. }
  270. /**
  271. * Converts a UNIX timestamp to a Julian Day count.
  272. *
  273. * @param integer|float $timestamp
  274. * the timestamp.
  275. * @return float the Julian Day count.
  276. */
  277. public function convertUnixToJd($timestamp)
  278. {
  279. return floor($timestamp / 86400) + 2440588;
  280. }
  281. /**
  282. * Converts a Julian Day count to a UNIX timestamp.
  283. *
  284. * @param integer|float $jd
  285. * the Julian Day count.
  286. * @return mixed $timestamp the integer timestamp or false if the
  287. * day count cannot be represented as a UNIX timestamp.
  288. */
  289. public function convertJdToUnix($jd)
  290. {
  291. if ($jd > 0) {
  292. $timestamp = ($jd - 2440588) * 86400;
  293. if ($timestamp >= 0) {
  294. return $timestamp;
  295. }
  296. }
  297. return false;
  298. }
  299. }