<commit_before>var moment = require('moment-timezone')
var sherlock = require('./other_modules/sherlock/sherlock')

function processDateAndTitleFromText (inputText, zoneName, callback) {
  var rightNow = new Date()
  var nowLocalDate = convertDateToLiteralTimezoneEquivalent(rightNow, zoneName, true)
  console.log(' ----> nowLocalDate:' + nowLocalDate)

  // Process Local Date as GMT to ensure "tomorrow" is tomorrow locally
  sherlock._setNow(nowLocalDate)
  var sherlocked = sherlock.parse(inputText)

  if (sherlocked.startDate == null) {
    callback("Couldn't work out that time sorry. 😕")
  } else {
    var startDateLocal = sherlocked.startDate
    console.log(' ----> sherlocked.startDate:' + startDateLocal)
    var startDateGMT = convertDateToLiteralTimezoneEquivalent(startDateLocal, zoneName, false)
    console.log(' ----> startDateGMT:' + startDateGMT)
    var userDateText = timezonedDateText(startDateLocal)
    console.log(' ----> userDateText:' + userDateText)
    callback(null, startDateGMT, userDateText, sherlocked.eventTitle)
  }
}

function convertDateToLiteralTimezoneEquivalent (date, timezoneText, reverse) {
  console.log('date: ' + date)
  var nowTimezone = moment.tz(date, timezoneText)
  var timezoneOffsetString = nowTimezone.format('Z')
  if (reverse === true) {
    timezoneOffsetString = reverseTimezoneOffset(timezoneOffsetString)
  }
  console.log('timezoneOffsetString: ' + timezoneOffsetString)
  var now = moment(date)
  console.log('now: ' + now.toDate())
  now.utcOffset(timezoneOffsetString, true)
  console.log('now: ' + now.toDate())
  return now.toDate()
}

function reverseTimezoneOffset (timezoneOffsetString) {
  var firstCharacter = timezoneOffsetString.substr(0, 1)
  if (firstCharacter === '-') {
    firstCharacter = '+'
  } else if (firstCharacter === '+') {
    firstCharacter = '-'
  }
  return firstCharacter + timezoneOffsetString.substr(1)
}

function timezonedDateText (date) {
  return moment(date).format('MMM DD YYYY ddd h:mma')
}

module.exports = {
  processDateAndTitleFromText
}<commit_msg>scheduler<commit_after>var moment = require('moment-timezone')
var sherlock = require('./other_modules/sherlock/sherlock')

function processDateAndTitleFromText (inputText, zoneName, callback) {
  var rightNow = new Date()
  var nowLocalDate = convertDateToLiteralTimezoneEquivalent(rightNow, zoneName, true)
  console.log(' ----> nowLocalDate:' + nowLocalDate)

  // Process Local Date as GMT to ensure "tomorrow" is tomorrow locally
  sherlock._setNow(nowLocalDate)
  var sherlocked = sherlock.parse(inputText)

  if (sherlocked.startDate == null) {
    callback("Couldn't work out that time sorry. 😕")
  } else {
    var startDateLocal = sherlocked.startDate
    console.log(' ----> sherlocked.startDate:' + startDateLocal)
    var startDateGMT = convertDateToLiteralTimezoneEquivalent(startDateLocal, zoneName, false)
    console.log(' ----> startDateGMT:' + startDateGMT)
    var userDateText = timezonedDateText(startDateLocal)
    console.log(' ----> userDateText:' + userDateText)
    callback(null, startDateGMT, userDateText, sherlocked.eventTitle)
  }
}

function convertDateToLiteralTimezoneEquivalent (date, timezoneText, reverse) {
  console.log('date: ' + date)
  var nowTimezone = moment.tz(date, timezoneText)
  var timezoneOffsetString = nowTimezone.format('Z')
  if (reverse === true) {
    timezoneOffsetString = reverseTimezoneOffset(timezoneOffsetString)
  }
  console.log('timezoneOffsetString: ' + timezoneOffsetString)
  var now = moment(date)
  console.log('now: ' + now.toDate())
  now.utcOffset(timezoneOffsetString, true)
  console.log('now: ' + now.toDate())
  return now.toDate()
}

function reverseTimezoneOffset (timezoneOffsetString) {
  var firstCharacter = timezoneOffsetString.substr(0, 1)
  if (firstCharacter === '-') {
    firstCharacter = '+'
  } else if (firstCharacter === '+') {
    firstCharacter = '-'
  }
  return firstCharacter + timezoneOffsetString.substr(1)
}

function timezonedDateText (date) {
  var dateFormat = 'ddd h:mma MMM Do'
  if (date.getHours === 0 && date.getMinutes === 0) dateFormat = 'ddd MMM Do'
  var now = new Date()
  if (now.getFullYear() !== date.getFullYear()) {
    dateFormat += ', YYYY'
  }
  return moment(date).format(dateFormat)
}

module.exports = {
  processDateAndTitleFromText
}
{'fraction_non_alphanumeric': 0.12520249942143022, 'fraction_numerical': 0.0018514232816477668, 'mean_word_length': 4.778074866310161, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '578127', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1350, 'n_words': 286}