DateFormatter

The Inline Google Spreadsheet Viewer DateFormatter class.

Defined (1)

The class is defined in the following location(s).

/lib/visformat.php  
  1. class DateFormatter { 
  2. private $format; 
  3. private $locale; 
  4. private $timezone; 
  5. private $gmt_offset; 
  6. private $abbrev; 
  7.  
  8. public function __construct($locale, $tz, $format) { 
  9. $this->locale = $locale; 
  10. if (preg_match('/^%([A-Z]+):(.*)$/', $format, $matches)) { 
  11. $tz = $matches[1]; 
  12. $format = $matches[2]; 
  13. $this->format = $format; 
  14. $this->timezone = new DateTimeZone($tz); 
  15. $date = new DateTime("", $this->timezone); 
  16. $this->gmt_offset = $this->timezone->getOffset($date); 
  17. $abbrev = timezone_abbreviations_list(); 
  18. $this->abbrev = $tz; 
  19. if (!isset($abbrev[strtolower($tz)])) { 
  20. $id = $this->timezone->getName(); 
  21. foreach ($abbrev as $key => $value) { 
  22. if ($value['timezone_id'] == $id) { 
  23. $this->abbrev = $key; 
  24. break; 
  25.  
  26. public function format($date) 
  27. $date += $this->gmt_offset; 
  28.  
  29. $result = ""; 
  30. $prev = -1; 
  31. $cur_count = 0; 
  32. $in_quote = 0; 
  33. $format = $this->format; 
  34. for ($i = 0; ($ch = substr($format, $i, 1)) !== FALSE; $i++) { 
  35. if ($ch == "'") { 
  36. if (substr($format, $i+1, 1) === "'") { 
  37. $prev = -1; 
  38. $result .= $ch; 
  39. } else { 
  40. $in_quote = !$in_quote; 
  41. } else if (!$in_quote && ctype_alpha($ch)) { 
  42. if ($prev == $ch) { 
  43. $cur_count++; 
  44. } else { 
  45. $result .= $this->convert($date, $prev, $cur_count); 
  46. $prev = $ch; 
  47. $cur_count = 1; 
  48. } else { 
  49. if ($prev !== -1) { 
  50. $result .= $this->convert($date, $prev, $cur_count); 
  51. $prev = -1; 
  52. $result .= $ch; 
  53. $result .= $this->convert($date, $prev, $cur_count); 
  54. return $result; 
  55.  
  56. private function convert($date, $char, $count) 
  57. $pat = ""; 
  58. if ($char === -1) return; 
  59.  
  60. switch ($char) { 
  61. case 'G': // era designator (Text) AD 
  62. return "AD"; 
  63. case 'y': // year (Number) 1996 
  64. $pat = $count == 2 ? "%y" : "%Y";  
  65. break; 
  66. case 'M': // month in year (Text & Number) July & 07 
  67. $pat = $count >= 3 ? $count >= 4 ? "%B" : "%b" : "%m"; 
  68. break; 
  69. case 'd': // day in month (Number) 10 
  70. $pat = "%d"; 
  71. break; 
  72. case 'h': // hour in am/pm (1~12) (Number) 12 
  73. $pat = "%I"; 
  74. break; 
  75. case 'H': // hour in day (0~23) (Number) 0 
  76. $pat = "%H"; 
  77. if ($count > 2) { 
  78. $pat = (string)(int)floor($date/3600); 
  79. $c = strlen($pat); 
  80. if ($c > $count) $c = $count; 
  81. $pat = substr($pat, -$c); 
  82. $pat = str_pad($pat, $count, " ", STR_PAD_LEFT); 
  83. break; 
  84. case 'm': // minute in hour (Number) 30 
  85. $pat = "%M"; 
  86. break; 
  87. case 's': // second in minute (Number) 55 
  88. $pat = "%S"; 
  89. break; 
  90. case 'S': // fractional second (Number) 978 
  91. return str_pad("", $count, "0"); 
  92. case 'E': // day of week (Text) Tuesday 
  93. $pat = $count >= 4 ? "%A" : "%a"; 
  94. break; 
  95. case 'e': //* day of week (local 1~7) (Text & Number) Tuesday & 2 
  96. $pat = $count >= 3 ? $count >= 4 ? "%A" : "%a" : "%d"; 
  97. break; 
  98. case 'D': // day in year (Number) 189 
  99. $pat = "%j"; 
  100. break; 
  101. case 'F': // day of week in month (Number) 2 (2nd Wed in July) 
  102. $mday = intval(gmstrftime("%d", $date)); 
  103. $pat = (string)intval(($mday + 6) / 7); 
  104. break; 
  105. case 'w': // week in year (Number) 27 
  106. $pat = "%V"; 
  107. break; 
  108. case 'W': // week in month (Number) 2 
  109. $mday = intval(gmstrftime("%d", $date)); 
  110. $wday = intval(gmstrftime("%w", $date)); 
  111. $pat = (string)intval(($mday - $wday) / 7); 
  112. break; 
  113. case 'a': // am/pm marker (Text) PM 
  114. $pat = "%p"; 
  115. break; 
  116. case 'k': // hour in day (1~24) (Number) 24 
  117. $hour = intval(gmstrftime("%H", $date)); 
  118. if (!$hour) $hour = 24; 
  119. $pat = (string)$hour; 
  120. break; 
  121. case 'K': // hour in am/pm (0~11) (Number) 0 
  122. $hour = intval(gmstrftime("%I", $date)); 
  123. if ($hour == 12) $hour = 0; 
  124. $pat = (string)$hour; 
  125. break; 
  126. case 'z': // time zone (Text) Pacific Standard Time 
  127. $pat = $this->abbrev; 
  128. break; 
  129. case 'Z': // time zone (RFC 822) (Number) -0800 
  130. $pat = sprintf("%+03d00", (int)(round($this->gmt_offset / 3600))); 
  131. break; 
  132. case 'v': // time zone (generic) (Text) Pacific Time 
  133. $pat = $this->timezone->getName(); 
  134. break; 
  135. case 'V': // time zone (location) (Text) United States (Los Angeles) 
  136. $pat = $this->timezone->getName(); 
  137. $pat = preg_replace("%^(.*)/(.*)$%", "$1 ($2)", $pat); 
  138. break; 
  139. if (substr($pat, 0, 1) == "%") { 
  140. $lsave = setlocale(LC_ALL, "0"); 
  141. setlocale(LC_ALL, $this->locale); 
  142. $pat = gmstrftime($pat, $date); 
  143. setlocale(LC_ALL, $lsave); 
  144. $len = strlen($pat); 
  145. if ($len < $count) { 
  146. if (strpos("0123456789", substr($pat, 0, 1)) !== FALSE) { 
  147. $pat = str_pad($pat, $count, "0", STR_PAD_LEFT); 
  148. } else { 
  149. $pat = str_pad($pat, $count, " "); 
  150. return $pat; 
  151.  
  152.  
  153. };