PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/files/phpjs/0.1/datetime/date.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 272 lines | 162 code | 6 blank | 104 comment | 10 complexity | cb1c0297bf2a1d04092f8a5bb3ed1afb MD5 | raw file
  1. function date (format, timestamp) {
  2. // http://kevin.vanzonneveld.net
  3. // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
  4. // + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
  5. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  6. // + improved by: MeEtc (http://yass.meetcweb.com)
  7. // + improved by: Brad Touesnard
  8. // + improved by: Tim Wiel
  9. // + improved by: Bryan Elliott
  10. // + improved by: David Randall
  11. // + input by: Brett Zamir (http://brett-zamir.me)
  12. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  13. // + improved by: Theriault
  14. // + derived from: gettimeofday
  15. // + input by: majak
  16. // + bugfixed by: majak
  17. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  18. // + input by: Alex
  19. // + bugfixed by: Brett Zamir (http://brett-zamir.me)
  20. // + improved by: Theriault
  21. // + improved by: Brett Zamir (http://brett-zamir.me)
  22. // + improved by: Theriault
  23. // + improved by: Thomas Beaucourt (http://www.webapp.fr)
  24. // + improved by: JT
  25. // + improved by: Theriault
  26. // + improved by: RafaƂ Kukawski (http://blog.kukawski.pl)
  27. // + bugfixed by: omid (http://phpjs.org/functions/380:380#comment_137122)
  28. // + input by: Martin
  29. // + input by: Alex Wilson
  30. // + input by: Haravikk
  31. // + improved by: Theriault
  32. // + bugfixed by: Chris (http://www.devotis.nl/)
  33. // % note 1: Uses global: php_js to store the default timezone
  34. // % note 2: Although the function potentially allows timezone info (see notes), it currently does not set
  35. // % note 2: per a timezone specified by date_default_timezone_set(). Implementers might use
  36. // % note 2: this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST set by that function
  37. // % note 2: in order to adjust the dates in this function (or our other date functions!) accordingly
  38. // * example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
  39. // * returns 1: '09:09:40 m is month'
  40. // * example 2: date('F j, Y, g:i a', 1062462400);
  41. // * returns 2: 'September 2, 2003, 2:26 am'
  42. // * example 3: date('Y W o', 1062462400);
  43. // * returns 3: '2003 36 2003'
  44. // * example 4: x = date('Y m d', (new Date()).getTime()/1000);
  45. // * example 4: (x+'').length == 10 // 2009 01 09
  46. // * returns 4: true
  47. // * example 5: date('W', 1104534000);
  48. // * returns 5: '53'
  49. // * example 6: date('B t', 1104534000);
  50. // * returns 6: '999 31'
  51. // * example 7: date('W U', 1293750000.82); // 2010-12-31
  52. // * returns 7: '52 1293750000'
  53. // * example 8: date('W', 1293836400); // 2011-01-01
  54. // * returns 8: '52'
  55. // * example 9: date('W Y-m-d', 1293974054); // 2011-01-02
  56. // * returns 9: '52 2011-01-02'
  57. var that = this,
  58. jsdate,
  59. f,
  60. // Keep this here (works, but for code commented-out
  61. // below for file size reasons)
  62. //, tal= [],
  63. txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  64. // trailing backslash -> (dropped)
  65. // a backslash followed by any character (including backslash) -> the character
  66. // empty string -> empty string
  67. formatChr = /\\?(.?)/gi,
  68. formatChrCb = function (t, s) {
  69. return f[t] ? f[t]() : s;
  70. },
  71. _pad = function (n, c) {
  72. n = String(n);
  73. while (n.length < c) {
  74. n = '0' + n;
  75. }
  76. return n;
  77. };
  78. f = {
  79. // Day
  80. d: function () { // Day of month w/leading 0; 01..31
  81. return _pad(f.j(), 2);
  82. },
  83. D: function () { // Shorthand day name; Mon...Sun
  84. return f.l().slice(0, 3);
  85. },
  86. j: function () { // Day of month; 1..31
  87. return jsdate.getDate();
  88. },
  89. l: function () { // Full day name; Monday...Sunday
  90. return txt_words[f.w()] + 'day';
  91. },
  92. N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun]
  93. return f.w() || 7;
  94. },
  95. S: function(){ // Ordinal suffix for day of month; st, nd, rd, th
  96. var j = f.j(),
  97. i = j%10;
  98. if (i <= 3 && parseInt((j%100)/10, 10) == 1) {
  99. i = 0;
  100. }
  101. return ['st', 'nd', 'rd'][i - 1] || 'th';
  102. },
  103. w: function () { // Day of week; 0[Sun]..6[Sat]
  104. return jsdate.getDay();
  105. },
  106. z: function () { // Day of year; 0..365
  107. var a = new Date(f.Y(), f.n() - 1, f.j()),
  108. b = new Date(f.Y(), 0, 1);
  109. return Math.round((a - b) / 864e5);
  110. },
  111. // Week
  112. W: function () { // ISO-8601 week number
  113. var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3),
  114. b = new Date(a.getFullYear(), 0, 4);
  115. return _pad(1 + Math.round((a - b) / 864e5 / 7), 2);
  116. },
  117. // Month
  118. F: function () { // Full month name; January...December
  119. return txt_words[6 + f.n()];
  120. },
  121. m: function () { // Month w/leading 0; 01...12
  122. return _pad(f.n(), 2);
  123. },
  124. M: function () { // Shorthand month name; Jan...Dec
  125. return f.F().slice(0, 3);
  126. },
  127. n: function () { // Month; 1...12
  128. return jsdate.getMonth() + 1;
  129. },
  130. t: function () { // Days in month; 28...31
  131. return (new Date(f.Y(), f.n(), 0)).getDate();
  132. },
  133. // Year
  134. L: function () { // Is leap year?; 0 or 1
  135. var j = f.Y();
  136. return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0;
  137. },
  138. o: function () { // ISO-8601 year
  139. var n = f.n(),
  140. W = f.W(),
  141. Y = f.Y();
  142. return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0);
  143. },
  144. Y: function () { // Full year; e.g. 1980...2010
  145. return jsdate.getFullYear();
  146. },
  147. y: function () { // Last two digits of year; 00...99
  148. return f.Y().toString().slice(-2);
  149. },
  150. // Time
  151. a: function () { // am or pm
  152. return jsdate.getHours() > 11 ? "pm" : "am";
  153. },
  154. A: function () { // AM or PM
  155. return f.a().toUpperCase();
  156. },
  157. B: function () { // Swatch Internet time; 000..999
  158. var H = jsdate.getUTCHours() * 36e2,
  159. // Hours
  160. i = jsdate.getUTCMinutes() * 60,
  161. // Minutes
  162. s = jsdate.getUTCSeconds(); // Seconds
  163. return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
  164. },
  165. g: function () { // 12-Hours; 1..12
  166. return f.G() % 12 || 12;
  167. },
  168. G: function () { // 24-Hours; 0..23
  169. return jsdate.getHours();
  170. },
  171. h: function () { // 12-Hours w/leading 0; 01..12
  172. return _pad(f.g(), 2);
  173. },
  174. H: function () { // 24-Hours w/leading 0; 00..23
  175. return _pad(f.G(), 2);
  176. },
  177. i: function () { // Minutes w/leading 0; 00..59
  178. return _pad(jsdate.getMinutes(), 2);
  179. },
  180. s: function () { // Seconds w/leading 0; 00..59
  181. return _pad(jsdate.getSeconds(), 2);
  182. },
  183. u: function () { // Microseconds; 000000-999000
  184. return _pad(jsdate.getMilliseconds() * 1000, 6);
  185. },
  186. // Timezone
  187. e: function () { // Timezone identifier; e.g. Atlantic/Azores, ...
  188. // The following works, but requires inclusion of the very large
  189. // timezone_abbreviations_list() function.
  190. /* return that.date_default_timezone_get();
  191. */
  192. throw 'Not supported (see source code of date() for timezone on how to add support)';
  193. },
  194. I: function () { // DST observed?; 0 or 1
  195. // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
  196. // If they are not equal, then DST is observed.
  197. var a = new Date(f.Y(), 0),
  198. // Jan 1
  199. c = Date.UTC(f.Y(), 0),
  200. // Jan 1 UTC
  201. b = new Date(f.Y(), 6),
  202. // Jul 1
  203. d = Date.UTC(f.Y(), 6); // Jul 1 UTC
  204. return ((a - c) !== (b - d)) ? 1 : 0;
  205. },
  206. O: function () { // Difference to GMT in hour format; e.g. +0200
  207. var tzo = jsdate.getTimezoneOffset(),
  208. a = Math.abs(tzo);
  209. return (tzo > 0 ? "-" : "+") + _pad(Math.floor(a / 60) * 100 + a % 60, 4);
  210. },
  211. P: function () { // Difference to GMT w/colon; e.g. +02:00
  212. var O = f.O();
  213. return (O.substr(0, 3) + ":" + O.substr(3, 2));
  214. },
  215. T: function () { // Timezone abbreviation; e.g. EST, MDT, ...
  216. // The following works, but requires inclusion of the very
  217. // large timezone_abbreviations_list() function.
  218. /* var abbr = '', i = 0, os = 0, default = 0;
  219. if (!tal.length) {
  220. tal = that.timezone_abbreviations_list();
  221. }
  222. if (that.php_js && that.php_js.default_timezone) {
  223. default = that.php_js.default_timezone;
  224. for (abbr in tal) {
  225. for (i=0; i < tal[abbr].length; i++) {
  226. if (tal[abbr][i].timezone_id === default) {
  227. return abbr.toUpperCase();
  228. }
  229. }
  230. }
  231. }
  232. for (abbr in tal) {
  233. for (i = 0; i < tal[abbr].length; i++) {
  234. os = -jsdate.getTimezoneOffset() * 60;
  235. if (tal[abbr][i].offset === os) {
  236. return abbr.toUpperCase();
  237. }
  238. }
  239. }
  240. */
  241. return 'UTC';
  242. },
  243. Z: function () { // Timezone offset in seconds (-43200...50400)
  244. return -jsdate.getTimezoneOffset() * 60;
  245. },
  246. // Full Date/Time
  247. c: function () { // ISO-8601 date.
  248. return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb);
  249. },
  250. r: function () { // RFC 2822
  251. return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
  252. },
  253. U: function () { // Seconds since UNIX epoch
  254. return jsdate / 1000 | 0;
  255. }
  256. };
  257. this.date = function (format, timestamp) {
  258. that = this;
  259. jsdate = (timestamp === undefined ? new Date() : // Not provided
  260. (timestamp instanceof Date) ? new Date(timestamp) : // JS Date()
  261. new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
  262. );
  263. return format.replace(formatChr, formatChrCb);
  264. };
  265. return this.date(format, timestamp);
  266. }