MCPcopy Create free account
hub / github.com/RubyLouvre/anu / serializeDate

Function serializeDate

test/json3.js:321–386  ·  view source on GitHub ↗
(value)

Source from the content-addressed store, hash-verified

319
320 // Internal: Serializes a date object.
321 var serializeDate = function (value) {
322 var getData, year, month, date, time, hours, minutes, seconds, milliseconds;
323 // Define additional utility methods if the `Date` methods are buggy.
324 if (!isExtended) {
325 var floor = Math.floor;
326 // A mapping between the months of the year and the number of days between
327 // January 1st and the first of the respective month.
328 var Months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
329 // Internal: Calculates the number of days between the Unix epoch and the
330 // first day of the given month.
331 var getDay = function (year, month) {
332 return Months[month] + 365 * (year - 1970) + floor((year - 1969 + (month = +(month > 1))) / 4) - floor((year - 1901 + month) / 100) + floor((year - 1601 + month) / 400);
333 };
334 getData = function (value) {
335 // Manually compute the year, month, date, hours, minutes,
336 // seconds, and milliseconds if the `getUTC*` methods are
337 // buggy. Adapted from @Yaffle's `date-shim` project.
338 date = floor(value / 864e5);
339 for (year = floor(date / 365.2425) + 1970 - 1; getDay(year + 1, 0) <= date; year++);
340 for (month = floor((date - getDay(year, 0)) / 30.42); getDay(year, month + 1) <= date; month++);
341 date = 1 + date - getDay(year, month);
342 // The `time` value specifies the time within the day (see ES
343 // 5.1 section 15.9.1.2). The formula `(A % B + B) % B` is used
344 // to compute `A modulo B`, as the `%` operator does not
345 // correspond to the `modulo` operation for negative numbers.
346 time = (value % 864e5 + 864e5) % 864e5;
347 // The hours, minutes, seconds, and milliseconds are obtained by
348 // decomposing the time within the day. See section 15.9.1.10.
349 hours = floor(time / 36e5) % 24;
350 minutes = floor(time / 6e4) % 60;
351 seconds = floor(time / 1e3) % 60;
352 milliseconds = time % 1e3;
353 };
354 } else {
355 getData = function (value) {
356 year = value.getUTCFullYear();
357 month = value.getUTCMonth();
358 date = value.getUTCDate();
359 hours = value.getUTCHours();
360 minutes = value.getUTCMinutes();
361 seconds = value.getUTCSeconds();
362 milliseconds = value.getUTCMilliseconds();
363 };
364 }
365 serializeDate = function (value) {
366 if (value > -1 / 0 && value < 1 / 0) {
367 // Dates are serialized according to the `Date#toJSON` method
368 // specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15
369 // for the ISO 8601 date time string format.
370 getData(value);
371 // Serialize extended years correctly.
372 value = (year <= 0 || year >= 1e4 ? (year < 0 ? "-" : "+") + toPaddedString(6, year < 0 ? -year : year) : toPaddedString(4, year)) +
373 "-" + toPaddedString(2, month + 1) + "-" + toPaddedString(2, date) +
374 // Months, dates, hours, minutes, and seconds should have two
375 // digits; milliseconds should have three.
376 "T" + toPaddedString(2, hours) + ":" + toPaddedString(2, minutes) + ":" + toPaddedString(2, seconds) +
377 // Milliseconds are optional in ES 5.0, but required in 5.1.
378 "." + toPaddedString(3, milliseconds) + "Z";

Callers 2

dateToJSONFunction · 0.85
serializeFunction · 0.85

Calls 3

getDayFunction · 0.85
getDataFunction · 0.85
toPaddedStringFunction · 0.85

Tested by

no test coverage detected