Skip to content

Instantly share code, notes, and snippets.

@zkxs
Created April 23, 2023 20:12
Show Gist options
  • Save zkxs/215f660ba108a2e95f2017557cba5d50 to your computer and use it in GitHub Desktop.
Save zkxs/215f660ba108a2e95f2017557cba5d50 to your computer and use it in GitHub Desktop.
Imagine if Discord could do time math for you
I'd rather avoid Monday and Saturday if possible because I have other things I like to do during those days. My typical availability looks like:
Monday 18:00 - 23:00
Tuesday 18:00 - 23:00
Wednesday 18:00 - 23:00
Thursday 18:00 - 23:00
Friday 18:00 - 25:00
Saturday 11:00 - 25:00
Sunday 11:00 - 23:00
*Valid through next daylight savings shift (2023-11-05T01:59-05:00), after which it will be wrong*
I'd rather avoid Monday and Saturday if possible because I have other things I like to do during those days. My typical availability looks like:
Monday <t:1682204400:t> - <t:1682222400:t>
Tuesday <t:1682204400:t> - <t:1682222400:t>
Wednesday <t:1682204400:t> - <t:1682222400:t>
Thursday <t:1682204400:t> - <t:1682222400:t>
Friday <t:1682204400:t> - <t:1682143200:t>
Saturday <t:1682179200:t> - <t:1682143200:t>
Sunday <t:1682179200:t> - <t:1682222400:t>
*Valid through next daylight savings shift (<t:1699167540:f>), after which it will be wrong*
import java.io.FileOutputStream
import java.io.PrintWriter
import java.nio.file.Files
import java.time._
import java.time.format._
import scala.io.Source
import scala.util.matching.Regex
object Schedule {
// read from this
val inputFile: String = "schedule-input.txt"
// write to this
val outputFile: String = "schedule-output.txt"
val dateTimePattern: Regex = """\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?:Z|[-+]\d{2}:\d{2})""".r
val timePattern: Regex = """\d{2}:\d{2}""".r
def main(args: Array[String]) {
val source = Source.fromFile(inputFile)
val writer = new PrintWriter(new FileOutputStream(outputFile))
val timeFormat = DateTimeFormatter.ISO_LOCAL_TIME.withResolverStyle(ResolverStyle.LENIENT)
val nowDate = LocalDate.now()
val zoneId = ZoneId.systemDefault
for (line <- source.getLines()) {
// replace full dates
val replacement1 = dateTimePattern.replaceAllIn(line, matched => {
val unixTime = ZonedDateTime.parse(matched.toString).toInstant.toEpochMilli / 1000
s"<t:$unixTime:f>"
})
// replace timestamps
val replacement2 = timePattern.replaceAllIn(replacement1, matched => {
val unixTime = ZonedDateTime.of(nowDate, LocalTime.parse(matched.toString, timeFormat), zoneId).toInstant.toEpochMilli / 1000
s"<t:$unixTime:t>"
})
writer.println(replacement2)
}
source.close()
writer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment