Created
December 9, 2015 00:04
Star
You must be signed in to star a gist
A possible method for implementing bugzil.la/902573 from the torbirdy extension. A rounded UTC Date header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** RFC 822 labels for days of the week. */ | |
var kDaysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; | |
/** A list of month names for Date parsing. */ | |
var kMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | |
"Sep", "Oct", "Nov", "Dec"]; | |
/* | |
* Formatting helper to output numbers between 0-9 as 00-09 instead. | |
*/ | |
function padTo2Digits(num) { | |
return num < 10 ? "0" + num : num.toString(); | |
} | |
/** | |
* Add a date/time field to the output, rounded down to the nearest | |
* minute, using the JS date object as the time representation. | |
* The value will be output using the UTC timezone offset. | |
* | |
* Note that if the date is an invalid date (its internal date parameter is a | |
* NaN value), this method throws an error instead of generating an invalid | |
* string. | |
* | |
* @public | |
* @param {Date} date The date to be added to the output string. | |
*/ | |
addRoundedUTCDate = function (date) { | |
console.log("addDate:", date); | |
// Rather than make a header plastered with NaN values, throw an error on | |
// specific invalid dates. | |
if (isNaN(date.getTime())) | |
throw new Error("Cannot encode an invalid date"); | |
// RFC 5322 says years can't be before 1900. The after 9999 is a bit that | |
// derives from the specification saying that years have 4 digits. | |
if (date.getUTCFullYear() < 1900 || date.getUTCFullYear() > 9999) | |
throw new Error("Date year is out of encodable range"); | |
// Always use UTC: | |
let tzOffsetStr = "+0000"; | |
// Convert the day-time figure into a single value to avoid unwanted line | |
// breaks in the middle. | |
let dayTime = [ | |
kDaysOfWeek[date.getUTCDay()] + ",", | |
date.getUTCDate(), | |
kMonthNames[date.getUTCMonth()], | |
date.getUTCFullYear(), | |
padTo2Digits(date.getUTCHours()) + ":" + | |
padTo2Digits(date.getUTCMinutes()) + ":" + | |
// Rounding down to the nearest minute: | |
padTo2Digits(0), | |
tzOffsetStr | |
].join(" "); | |
this.addText(dayTime, false); | |
}; | |
// Import the jsmime module that is used to generate mail headers. | |
let { jsmime } = Components.utils.import("resource:///modules/jsmime.jsm"); | |
// Inject our own structured encoder to the default headeremitter, | |
// to override the default Date encoder with a UTC-only version. | |
jsmime.headeremitter.addStructuredEncoder("Date", addRoundedUTCDate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment