<commit_before>/*
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var tz = require('timezone-js');
var fs = require('fs');
var railsTimezone = require('rails-timezone');

var RailsMappings = require('oae-util/timezones-rails');

tz.timezone.loadingScheme = tz.timezone.loadingSchemes.MANUAL_LOAD;
tz.timezone.transport = function(opts) {
    return fs.readFileSync(opts.url, 'utf8');
};
tz.timezone.loadZoneJSONData(__dirname + '/../timezones.json', true);

module.exports = tz;

/**
 * Given a ruby-on-rails supported timezone name, map it to a TZInfo identifier supported by OAE.
 *
 * @param   {String}    zone   The rails timezone to map
 * @return  {String}           The TZInfo identifier that represents the associated rails timezone
 */
var getTimezoneFromRails = module.exports.getTimezoneFromRails = function(zone) {
    return railsTimezone.from(zone);
};

/**
 * Given a olson timezone name, map it to a ruby-on-rails timezone.
 *
 * @param   {String}    zone   The olson timezone to map
 * @return  {String}           The associated rails timezone
 */
var getRailsTimezoneFromTZInfo = module.exports.getRailsTimezoneFromTZInfo = function(zone) {
    // If there's a direct mapping just return it
    if (railsTimezone.to(zone)) {
        return railsTimezone.to(zone);
    }
    // Otherwise we'll guess a zone based on the offset

    // Get the current offsets for all rails zones
    var railsOffsets = {};
    for (var zoneMapping in RailsMappings) {
        var mappingDate = new tz.Date('now', RailsMappings[zoneMapping]);
        railsOffsets[mappingDate.getTimezoneOffset()] = zoneMapping;
    }
    var userOffset = new tz.Date('now', zone).getTimezoneOffset();
    // if there's a zone with a matching offset return it
    if (railsOffsets[userOffset]) {
        return railsOffsets[userOffset];
    }
    // no matching offset, return the zone with the closest offset
    var availableOffsets = Object.keys(railsOffsets);
    var closest = availableOffsets[0];
    var deltaClosest = Math.abs(closest - userOffset);
    for (var i = 1; i < availableOffsets.length; i++) {
        var deltaChecking = Math.abs(availableOffsets[i] - userOffset);
        if (deltaChecking < deltaClosest) {
            closest = availableOffsets[i];
            deltaClosest = Math.abs(closest - userOffset);
        }
    }
    return railsOffsets[closest];
};

/**
 * Gets a list of available timezones with their current offset from UTC
 *
 * @return  {Object}        A map of all the available rails timezones and their offsets from UTC
 */
var getZones = module.exports.getZones = function() {
    var railsZones = {};
    for (var zoneMapping in RailsMappings) {
        var mappingDate = new tz.Date('now', RailsMappings[zoneMapping]);
        railsZones[RailsMappings[zoneMapping]] = {
            'displayName': zoneMapping,
            'offset': mappingDate.getTimezoneOffset()
        };
    }
    return railsZones;
};
<commit_msg>code review issues<commit_after>/*
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var tz = require('timezone-js');
var fs = require('fs');
var railsTimezone = require('rails-timezone');
var _ = require('underscore');

var RailsMappings = require('oae-util/timezones-rails');

tz.timezone.loadingScheme = tz.timezone.loadingSchemes.MANUAL_LOAD;
tz.timezone.transport = function(opts) {
    return fs.readFileSync(opts.url, 'utf8');
};
tz.timezone.loadZoneJSONData(__dirname + '/../timezones.json', true);

module.exports = tz;

/**
 * Given a ruby-on-rails supported timezone name, map it to a TZInfo identifier supported by OAE.
 *
 * @param  {String}    zone   The rails timezone to map
 * @return {String}           The TZInfo identifier that represents the associated rails timezone
 */
var getTimezoneFromRails = module.exports.getTimezoneFromRails = function(zone) {
    return railsTimezone.from(zone);
};

/**
 * Given a olson timezone name, map it to a ruby-on-rails timezone.
 *
 * @param  {String}    zone   The olson timezone to map
 * @return {String}           The associated rails timezone
 */
var getRailsTimezoneFromTZInfo = module.exports.getRailsTimezoneFromTZInfo = function(zone) {
    // If there's a direct mapping just return it
    if (railsTimezone.to(zone)) {
        return railsTimezone.to(zone);
    }
    // Otherwise we'll guess a zone based on the offset

    // Get the current offsets for all rails zones
    var railsOffsets = {};
    _.each(RailsMappings, function(value, key) {
        var mappingDate = new tz.Date('now', value);
        railsOffsets[mappingDate.getTimezoneOffset()] = key;
    });
    var userOffset = new tz.Date('now', zone).getTimezoneOffset();
    // if there's a zone with a matching offset return it
    if (railsOffsets[userOffset]) {
        return railsOffsets[userOffset];
    }
    // no matching offset, return the zone with the closest offset
    var availableOffsets = Object.keys(railsOffsets);
    // offsets are in minutes, 1440 is 24 hours
    var closest = {'delta': 1440, 'zone': null};
    _.each(railsOffsets, function(zone, offset) {
        var delta = Math.abs(offset - userOffset);
        if (delta < closest.delta) {
            closest = {
                'delta': delta,
                'zone': zone
            };
        }
    });
    return closest.zone;
};

/**
 * Gets a list of available timezones with their current offset from UTC
 *
 * @return {Object}        A map of all the available rails timezones and their offsets from UTC
 */
var getZones = module.exports.getZones = function() {
    var railsZones = {};
    _.each(RailsMappings, function(value, key) {
        var mappingDate = new tz.Date('now', value);
        railsZones[value] = {
            'displayName': key,
            'offset': mappingDate.getTimezoneOffset()
        };
    });
    return railsZones;
};

{'fraction_non_alphanumeric': 0.10368663594470046, 'fraction_numerical': 0.004320276497695853, 'mean_word_length': 3.17870036101083, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4417017', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1969, 'n_words': 665}