Created
December 23, 2020 10:38
-
-
Save sticks-stuff/b972275fe72541bffd123c8a9cc6c159 to your computer and use it in GitHub Desktop.
Get timestamps out of Jerma !sus strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const date = require('date-and-time'); | |
const day_of_week = require('date-and-time/plugin/day-of-week'); | |
const meridiem = require('date-and-time/plugin/meridiem'); | |
date.plugin(meridiem); | |
date.plugin(day_of_week); | |
var { DateTime } = require('luxon'); | |
var stringToParse = 'You cast SUS! You sense the next stream starting on Friday the 11th around 3pm PST'; | |
var splitString = stringToParse.split(" "); | |
var currentDate = DateTime.local().setZone('America/Los_Angeles'); //you can comment out .setZone to use your local time | |
var streamDates = new Array; | |
var streamTimes = new Array; | |
for (i = 0; i < splitString.length; i++) { | |
//get the time for formats like '4pm' | |
if (date.isValid(splitString[i], 'hA...')) { | |
streamTimes.push(date.preparse(splitString[i], 'hA...').H); | |
} | |
else { | |
//if date is given as a number, we want this to be prioritised | |
if (date.isValid(splitString[i], 'D...')) { | |
streamDates.push(date.preparse(splitString[i], 'D...').D); | |
} | |
//otherwise lets look for a written day of the week, like "Saturday" | |
else if (date.isValid(splitString[i], 'dd...')) { | |
var streamDay = date.preparse(splitString[i], 'dd...').d; | |
var difference = ((streamDay - currentDate.weekday) + 7) % 7 //weird mod stuff to make asking for a day next week work correctly | |
streamDates.push(currentDate.day + difference); | |
} | |
} | |
} | |
console.log("Possible stream times " + streamTimes); | |
console.log("Possible stream dates " + streamDates); | |
var latestTime = (parseInt(Math.max.apply(Math, streamTimes))) || 16; //default stream time is 4pm | |
var latestDay = (parseInt(Math.max.apply(Math, streamDates))) || currentDate.day; //default stream day is today | |
evaulatedStreamTime = DateTime.fromObject({day: latestDay, hour: latestTime, zone: 'America/Los_Angeles', numberingSystem: 'beng'}); | |
console.log("evaulated stream day " + evaulatedStreamTime.day); | |
console.log("evaulated stream hour " + evaulatedStreamTime.hour); | |
console.log("evaulated stream unix time " + (evaulatedStreamTime / 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment