1
1
mirror of https://github.com/leon-ai/leon.git synced 2025-01-08 10:47:45 +03:00

feat(skill/date_time): days countdown

This commit is contained in:
Théo LUDWIG 2023-06-25 12:27:30 +02:00
parent 8645ef01fd
commit 636eb0f139
No known key found for this signature in database
GPG Key ID: ADFE5A563D718F3B
4 changed files with 77 additions and 2 deletions

View File

@ -23,12 +23,26 @@
"What is this week's number?",
"What week of the year are we?"
]
},
"days_countdown": {
"type": "logic",
"utterance_samples": [
"How many days left until 1st July?",
"What is the number of days remaining until December 25th?",
"In how many days will it be February 10th?"
]
}
},
"answers": {
"current_date_time": [
"It is %weekday%, %month% %day%, %year%, and it is %hours%:%minutes%:%seconds%."
],
"current_week_number": ["It is the %week_number% week of the year."]
"current_week_number": ["It is the %week_number% week of the year."],
"days_countdown": [
"There are %days% days between %month1% %day1%, %year1% and %month2% %day2%, %year2%."
],
"days_countdown_error": [
"I'm sorry, I didn't understand the date you said."
]
}
}

View File

@ -3,7 +3,7 @@ import { leon } from '@sdk/leon'
import { format } from 'numerable'
const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
/**
* Get the week number (1-52) for a given date.

View File

@ -0,0 +1,60 @@
import type {
ActionFunction,
ActionParams,
BuiltInDateRangeEntity
} from '@sdk/types'
import { leon } from '@sdk/leon'
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
const isBuiltInDateRangeEntity = (
entity: ActionParams['current_entities'][number]
): entity is BuiltInDateRangeEntity => {
return entity.entity === 'daterange'
}
/**
* Calculate the number of days between two dates.
* @example daysBetween(new Date(2020, 0, 1), new Date(2020, 0, 1)) // 0
* @example daysBetween(new Date(2020, 0, 1), new Date(2020, 0, 2)) // 1
*/
const daysBetween = (date1: Date, date2: Date): number => {
const differenceMilliseconds = Math.abs(date1.getTime() - date2.getTime())
return Math.round(differenceMilliseconds / ONE_DAY_MILLISECONDS)
}
export const run: ActionFunction = async function (params) {
const { current_entities } = params
let futureDateString: string | null = null
for (const entity of current_entities) {
if (isBuiltInDateRangeEntity(entity)) {
const { futureEndDate } = entity.resolution
futureDateString = futureEndDate
break
}
}
if (futureDateString == null) {
return await leon.answer({
key: 'days_countdown_error'
})
}
const currentDate = new Date()
const futureDate = new Date(futureDateString)
const daysCountdown = daysBetween(currentDate, futureDate)
await leon.answer({
key: 'days_countdown',
data: {
days: daysCountdown,
month1: currentDate.toLocaleString(params.lang, { month: 'long' }),
day1: currentDate.getDate(),
year1: currentDate.getFullYear(),
month2: futureDate.toLocaleString(params.lang, { month: 'long' }),
day2: futureDate.getDate(),
year2: futureDate.getFullYear()
}
})
}

View File

@ -0,0 +1 @@
export const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24