<commit_before>var EXPORTED_SYMBOLS = ["sendlater3DateParse", "sendlater3DateToSugarDate", "sendlater3SugarLocale"];

// Using mozIJSSubScriptLoader instead of Cu.import because I want to
// preserve SugarJS unmodified from the official
// distribution, and it doesn't define EXPORTED_SYMBOLS, which is
// required by CU.import.
var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                       .getService(Components.interfaces.mozIJSSubScriptLoader); 
loader.loadSubScript("resource://sendlater3/sugar.js"); 

// https://github.com/andrewplummer/Sugar/issues/605
Sugar.Date.extend();

var didLocale = false;
var locale;

function getLocale() {
    if (! didLocale) {
	didLocale = true;
        var locales;
        try {
	    var localeService = Components.classes[
                "@mozilla.org/intl/nslocaleservice;1"]
                .getService(Components.interfaces.nsILocaleService);
	    var localeObj = localeService.getApplicationLocale();
	    locales = [localeObj.getCategory("NSILOCALE_TIME")];
        }
        catch (ex) {
            try {
                locales = Components.classes[
                    "@mozilla.org/intl/localeservice;1"]
                    .getService(Components.interfaces.mozILocaleService)
                    .getRegionalPrefsLocales();
            }
            catch (ex) {
                locales = [];
            }
        }
        for (var l of locales) {
	    // Sugar may not recognize the locale. It throws an error when
	    // an unrecognized locale is passed in and the locale is
	    // needed.
	    try {
	        Sugar.Date.create().format(null, l);
                locale = l;
                return locale;
	    }
	    catch (ex) {}
        }
    }
    return locale;
}

function sendlater3DateParse(date) {
    var locale = getLocale();
    var obj = Sugar.Date.create(date, {locale: locale, future: true});
    if (! (obj.isValid() || locale.substr(0, 2) == "en")) {
	// Fall back on English date rules
	obj = Sugar.Date.create(date, {future: true});
    }
    return obj;
}

function sendlater3DateToSugarDate(date) {
    try {
	return Sugar.Date.create(date);
    }
    catch (e) {
	var d = new Sugar.Date();
	d.setTime(date.getTime());
	return d.raw;
    }
}
<commit_msg>Fix bug in SugarJS upgrade<commit_after>var EXPORTED_SYMBOLS = ["sendlater3DateParse", "sendlater3DateToSugarDate", "sendlater3SugarLocale"];

// Using mozIJSSubScriptLoader instead of Cu.import because I want to
// preserve SugarJS unmodified from the official
// distribution, and it doesn't define EXPORTED_SYMBOLS, which is
// required by CU.import.
var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                       .getService(Components.interfaces.mozIJSSubScriptLoader); 
loader.loadSubScript("resource://sendlater3/sugar.js"); 

// https://github.com/andrewplummer/Sugar/issues/605
Sugar.Date.extend();

var didLocale = false;
var locale;

function getLocale() {
    if (! didLocale) {
	didLocale = true;
        var locales;
        try {
	    var localeService = Components.classes[
                "@mozilla.org/intl/nslocaleservice;1"]
                .getService(Components.interfaces.nsILocaleService);
	    var localeObj = localeService.getApplicationLocale();
	    locales = [localeObj.getCategory("NSILOCALE_TIME")];
        }
        catch (ex) {
            try {
                locales = Components.classes[
                    "@mozilla.org/intl/localeservice;1"]
                    .getService(Components.interfaces.mozILocaleService)
                    .getRegionalPrefsLocales();
            }
            catch (ex) {
                locales = [];
            }
        }
        for (var l of locales) {
	    // Sugar may not recognize the locale. It throws an error when
	    // an unrecognized locale is passed in and the locale is
	    // needed.
	    try {
	        (new Date()).format(null, l);
                locale = l;
                return locale;
	    }
	    catch (ex) {}
        }
    }
    return locale;
}

function sendlater3DateParse(date) {
    // Sugar.Date.create is actually Sugar.Date.parse. It returns an unextended
    // Date object. Since we call Date.extend above, the core Date constructor
    // now returns an extended object, so we use that after parsing to create
    // an extended object.
    var locale = getLocale();
    var obj = new Date(
        Sugar.Date.create(date, {locale: locale, future: true}));
    if (! (obj.isValid() || locale.substr(0, 2) == "en")) {
	// Fall back on English date rules
	obj = new Date(Sugar.Date.create(date, {future: true}));
    }
    return obj;
}

function sendlater3DateToSugarDate(date) {
    return new Date(date);
}

{'fraction_non_alphanumeric': 0.11348719034511963, 'fraction_numerical': 0.0059284353165360995, 'mean_word_length': 2.3220815752461323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490387', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1382, 'n_words': 342}