Skip to content

Instantly share code, notes, and snippets.

@adamfortuno
Created October 17, 2019 17:34
Show Gist options
  • Save adamfortuno/a178355e0999e677e22cd3037391882a to your computer and use it in GitHub Desktop.
Save adamfortuno/a178355e0999e677e22cd3037391882a to your computer and use it in GitHub Desktop.
Converts a JSON Snippet with an EST Date Time to its UTC Equivilant
const mtz = require('moment-timezone');
const moment = require('moment');
const original_date_string = "2019/10/15 14:21:14";
const original_date_object = new Date(original_date_string);
const original_date_object_est = moment.tz(original_date_object, 'America/New_York');
console.log("Original: %s", original_date_string);
console.log("UTC: %s", original_date_object_est.utc().toISOString());
exports.handler = async (event) => {
// Stub out the response object
const response = {
statusCode: 400,
body: ""
};
try {
// Make sure you were passed a string...
if ( typeof event.body !== "string" ) {
throw new Error("Please pass a string of log entries delimited by pipe.");
}
// Specify the delimiter and clean the log entries submitted
const delimiter = "|";
const defaultTimeZone = 'America/New_York';
const defaultDateTimeFormat = 'YYYY/MM/DD HH:mm:ss';
let logEntries;
if ( event.body.trim().endsWith(delimiter) ) {
logEntries = event.body.trim().substring(0, event.body.length - 1);
} else {
logEntries = event.body.trim();
}
// Enumerate the log entries
const values = logEntries.split(delimiter);
// Update entry's date property if it exists
values.forEach( (logEntry, idx, array) => {
const logEntryObject = JSON.parse(logEntry);
if ( logEntryObject.date ) {
const targetDate = moment.tz(logEntryObject.date, defaultDateTimeFormat, defaultTimeZone);
logEntryObject.date = targetDate.utc().toISOString();
array[idx] = JSON.stringify(logEntryObject);
}
});
// Stage the successful results for transmission to the requester
response.statusCode = 200;
response.body = JSON.stringify( values.join(delimiter) );
} catch (error) {
response.body = error.toString();
}
// Reply with results of operation
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment