<commit_before>/**
 * Module dependencies.
 */

var debug = require('debug')('time')
  , fs = require('fs')
  , path = require('path')
  , bindings = require('bindings')('time.node')
  , MILLIS_PER_SECOND = 1000
  , DAYS_OF_WEEK = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
  , MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']
  , TZ_BLACKLIST = [ 'SystemV', 'Etc' ];

/**
 * Extends a "Date" constructor with node-time's extensions.
 * By default, `time.Date` is extended with this function.
 * If you want the global your your module-specific Date to be extended,
 * then invoke this function on the Date constructor.
 */

exports = module.exports = function (Date) {
  debug('extending Date constructor');
  var p = Date.prototype;
  p.getTimezone = getTimezone;
  p.setTimezone = setTimezone;
  p.getTimezoneAbbr = getTimezoneAbbr;
  return exports;
}

/**
 * The initial timezone of the process. This env var may initially be undefined,
 * in which case node-time will attempt to resolve and set the variable.
 */

exports.currentTimezone = process.env.TZ;

/**
 * Export the raw functions from the bindings.
 */

exports.time = bindings.time;
exports.localtime = bindings.localtime;
exports.mktime = bindings.mktime;

/**
 * A "hack" of sorts to force getting our own Date instance.
 * Otherwise, in normal cases, the global Natives are shared between
 * contexts (not what we want)...
 */

var _Date = process.env.NODE_MODULE_CONTEXTS
  ? Date
  : require('vm').runInNewContext("Date");

/**
 * Add the node-time extensions (setTimezone(), etc.)
 */

exports(_Date);

/**
 * During startup, we synchronously attempt to determine the location of the
 * timezone dir, or TZDIR on some systems. This isn't necessary for the
 * C bindings, however it's needed for the `listTimezones()` function and for
 * resolving the 'initial' timezone to use.
 */

debug('attempting to resolve timezone directory.');
var possibleTzdirs = [
    '/usr/share/zoneinfo'
  , '/usr/lib/zoneinfo'
  , '/usr/share/lib/zoneinfo'
];
var TZDIR = process.env.TZDIR;
if (TZDIR) {
  debug('got env-defined TZDIR:', TZDIR);
  possibleTzdirs.unshift(TZDIR);
}
while (possibleTzdirs.length > 0) {
  var d = possibleTzdirs.shift();
  debug('checking if directory exists:', d);
  try {
    if (fs.statSync(d).isDirectory()) {
      TZDIR = d;
      break;
    }
  } catch (e) {
    debug(e);
  }
}
possibleTzdirs = null; // garbage collect
if (TZDIR) {
  debug('found timezone directory at:', TZDIR);
} else {
  debug('WARN: Could not find timezone directory. listTimezones() won\'t work');
}

/**
 * Older versions of node-time would require the user to have the TZ
 * environment variable set, otherwise undesirable results would happen. Now
 * node-time tries to automatically determine the current timezone for you.
 */

if (!exports.currentTimezone) {
  debug('`process.env.TZ` not initially set, attempting to resolve');
  try {
    var currentTimezonePath = fs.readlinkSync('/etc/localtime');
    if (currentTimezonePath.substring(0, TZDIR.length) === TZDIR) {
      // Got It!
      var zone = currentTimezonePath.substring(TZDIR.length + 1);
      exports.currentTimezone = process.env.TZ = zone;
      debug('resolved initial timezone:', zone);
    }
  } catch (e) {
    debug(e);
  }
}

if (!exports.currentTimezone) {
  debug('"currentTimezone" still not set. Checking "/etc/timezone"');
  try {
    var zone = fs.readFileSync('/etc/timezone', 'utf8').trim();
    exports.currentTimezone = process.env.TZ = zone;
    debug('resolved initial timezone:', zone);
  } catch (e) {
    debug(e);
  }
}

/**
 * The user-facing 'tzset' function is a thin wrapper around the native binding to
 * 'tzset()'. This function accepts a timezone String to set the process' timezone
 * to. Returns an object with the zoneinfo for the timezone.
 *
 * Throws (on *some* platforms) when the desired timezone could not be loaded.
 *
 * Sets the `currentTimezone` property on the exports.
 */

function tzset (tz) {
  if (tz) {<commit_msg>check `/etc/TZ` for initial timezone information<commit_after>/**
 * Module dependencies.
 */

var debug = require('debug')('time')
  , fs = require('fs')
  , path = require('path')
  , bindings = require('bindings')('time.node')
  , MILLIS_PER_SECOND = 1000
  , DAYS_OF_WEEK = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
  , MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']
  , TZ_BLACKLIST = [ 'SystemV', 'Etc' ];

/**
 * Extends a "Date" constructor with node-time's extensions.
 * By default, `time.Date` is extended with this function.
 * If you want the global your your module-specific Date to be extended,
 * then invoke this function on the Date constructor.
 */

exports = module.exports = function (Date) {
  debug('extending Date constructor');
  var p = Date.prototype;
  p.getTimezone = getTimezone;
  p.setTimezone = setTimezone;
  p.getTimezoneAbbr = getTimezoneAbbr;
  return exports;
}

/**
 * The initial timezone of the process. This env var may initially be undefined,
 * in which case node-time will attempt to resolve and set the variable.
 */

exports.currentTimezone = process.env.TZ;

/**
 * Export the raw functions from the bindings.
 */

exports.time = bindings.time;
exports.localtime = bindings.localtime;
exports.mktime = bindings.mktime;

/**
 * A "hack" of sorts to force getting our own Date instance.
 * Otherwise, in normal cases, the global Natives are shared between
 * contexts (not what we want)...
 */

var _Date = process.env.NODE_MODULE_CONTEXTS
  ? Date
  : require('vm').runInNewContext("Date");

/**
 * Add the node-time extensions (setTimezone(), etc.)
 */

exports(_Date);

/**
 * During startup, we synchronously attempt to determine the location of the
 * timezone dir, or TZDIR on some systems. This isn't necessary for the
 * C bindings, however it's needed for the `listTimezones()` function and for
 * resolving the 'initial' timezone to use.
 */

debug('attempting to resolve timezone directory.');
var possibleTzdirs = [
    '/usr/share/zoneinfo'
  , '/usr/lib/zoneinfo'
  , '/usr/share/lib/zoneinfo'
];
var TZDIR = process.env.TZDIR;
if (TZDIR) {
  debug('got env-defined TZDIR:', TZDIR);
  possibleTzdirs.unshift(TZDIR);
}
while (possibleTzdirs.length > 0) {
  var d = possibleTzdirs.shift();
  debug('checking if directory exists:', d);
  try {
    if (fs.statSync(d).isDirectory()) {
      TZDIR = d;
      break;
    }
  } catch (e) {
    debug(e);
  }
}
possibleTzdirs = null; // garbage collect
if (TZDIR) {
  debug('found timezone directory at:', TZDIR);
} else {
  debug('WARN: Could not find timezone directory. listTimezones() won\'t work');
}

/**
 * Older versions of node-time would require the user to have the TZ
 * environment variable set, otherwise undesirable results would happen. Now
 * node-time tries to automatically determine the current timezone for you.
 */

if (!exports.currentTimezone) {
  debug('`process.env.TZ` not initially set, attempting to resolve');
  try {
    var currentTimezonePath = fs.readlinkSync('/etc/localtime');
    if (currentTimezonePath.substring(0, TZDIR.length) === TZDIR) {
      // Got It!
      var zone = currentTimezonePath.substring(TZDIR.length + 1);
      exports.currentTimezone = process.env.TZ = zone;
      debug('resolved initial timezone:', zone);
    }
  } catch (e) {
    debug(e);
  }
}

if (!exports.currentTimezone) {
  debug('"currentTimezone" still not set. Checking "/etc/timezone"');
  try {
    var zone = fs.readFileSync('/etc/timezone', 'utf8').trim();
    exports.currentTimezone = process.env.TZ = zone;
    debug('resolved initial timezone:', zone);
  } catch (e) {
    debug(e);
  }
}

if (!exports.currentTimezone) {
  debug('"currentTimezone" still not set. Checking "/etc/TZ"');
  try {
    var zone = fs.readFileSync('/etc/TZ', 'utf8').trim();
    exports.currentTimezone = process.env.TZ = zone;
    debug('resolved initial timezone:', zone);
  } catch (e) {
    debug(e);
  }
}

/**
 * The user-facing 'tzset' function is a thin wrapper around the native binding to
 * 'tzset()'. This function accepts a timezone String to set the process' timezone
 * to. Returns an object with the zoneinfo for the timezone.
 *
 * Throws (on *some* platforms) when the desired timezone could not be loaded.
 *
 * Sets the `currentTimezone` property on the exports.
 */

function tzset (tz) {
  if (tz) {
{'fraction_non_alphanumeric': 0.13805620608899297, 'fraction_numerical': 0.001990632318501171, 'mean_word_length': 3.836353340883352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1703820', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2653, 'n_words': 826}