FEAT: before/since history flag supports durations up to hours (13h23m31s etc)

This commit is contained in:
Drn
2024-06-11 20:08:29 -07:00
parent 7e78587586
commit 297c10d0fb
2 changed files with 26 additions and 6 deletions

View File

@ -307,8 +307,13 @@ func handleCommands() *exrouter.Route {
beforeID = discordTimestampToSnowflake("2006-01-02", before)
} else if isNumeric(before) {
beforeID = before
} else { // try to parse duration
dur, err := time.ParseDuration(before)
if err == nil {
beforeID = discordTimestampToSnowflake("2006-01-02 15:04:05.999999999 -0700 MST", time.Now().Add(-dur).Format("2006-01-02 15:04:05.999999999 -0700 MST"))
}
}
if config.Debug {
if config.Debug && beforeID != "" {
log.Println(lg("Command", "History", color.CyanString, "Date before range applied, snowflake %s, converts back to %s",
beforeID, discordSnowflakeToTimestamp(beforeID, "2006-01-02T15:04:05.000Z07:00")))
}
@ -318,8 +323,13 @@ func handleCommands() *exrouter.Route {
sinceID = discordTimestampToSnowflake("2006-01-02", since)
} else if isNumeric(since) {
sinceID = since
} else { // try to parse duration
dur, err := time.ParseDuration(since)
if err == nil {
sinceID = discordTimestampToSnowflake("2006-01-02 15:04:05.999999999 -0700 MST", time.Now().Add(-dur).Format("2006-01-02 15:04:05.999999999 -0700 MST"))
}
}
if config.Debug {
if config.Debug && sinceID != "" {
log.Println(lg("Command", "History", color.CyanString, "Date since range applied, snowflake %s, converts back to %s",
sinceID, discordSnowflakeToTimestamp(sinceID, "2006-01-02T15:04:05.000Z07:00")))
}

View File

@ -328,10 +328,15 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string
var beforeRange = before
if beforeRange != "" {
if isDate(beforeRange) {
if isDate(beforeRange) { // user has input YYYY-MM-DD
beforeRange = discordTimestampToSnowflake("2006-01-02", beforeID)
} else { // try to parse duration
dur, err := time.ParseDuration(beforeRange)
if err == nil {
beforeRange = discordTimestampToSnowflake("2006-01-02 15:04:05.999999999 -0700 MST", time.Now().Add(-dur).Format("2006-01-02 15:04:05.999999999 -0700 MST"))
}
}
if isNumeric(beforeRange) {
if isNumeric(beforeRange) { // did we convert something (or was it always a number)?
rangeContent += fmt.Sprintf("**Before:** `%s`\n", beforeRange)
}
before = beforeRange
@ -339,10 +344,15 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string
var sinceRange = since
if sinceRange != "" {
if isDate(sinceRange) {
if isDate(sinceRange) { // user has input YYYY-MM-DD
sinceRange = discordTimestampToSnowflake("2006-01-02", sinceRange)
} else { // try to parse duration
dur, err := time.ParseDuration(sinceRange)
if err == nil {
sinceRange = discordTimestampToSnowflake("2006-01-02 15:04:05.999999999 -0700 MST", time.Now().Add(-dur).Format("2006-01-02 15:04:05.999999999 -0700 MST"))
}
}
if isNumeric(sinceRange) {
if isNumeric(sinceRange) { // did we convert something (or was it always a number)?
rangeContent += fmt.Sprintf("**Since:** `%s`\n", sinceRange)
}
since = sinceRange