chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
export const run: ActionFunction = async function (params) {
|
||||
const currentDate = new Date()
|
||||
await leon.answer({
|
||||
key: 'current_date',
|
||||
data: {
|
||||
weekday: currentDate.toLocaleString(params.lang, { weekday: 'long' }),
|
||||
month: currentDate.toLocaleString(params.lang, { month: 'long' }),
|
||||
day: currentDate.getDate(),
|
||||
year: currentDate.getFullYear()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { zeroPad } from '../lib/zeroPad'
|
||||
|
||||
export const run: ActionFunction = async function (params) {
|
||||
const currentDate = new Date()
|
||||
await leon.answer({
|
||||
key: 'current_date_time',
|
||||
data: {
|
||||
weekday: currentDate.toLocaleString(params.lang, { weekday: 'long' }),
|
||||
month: currentDate.toLocaleString(params.lang, { month: 'long' }),
|
||||
day: currentDate.getDate(),
|
||||
year: currentDate.getFullYear(),
|
||||
hours: zeroPad(currentDate.getHours()),
|
||||
minutes: zeroPad(currentDate.getMinutes()),
|
||||
seconds: zeroPad(currentDate.getSeconds())
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { zeroPad } from '../lib/zeroPad'
|
||||
|
||||
export const run: ActionFunction = async function (params) {
|
||||
const timeZone = params.action_arguments['time_zone']
|
||||
const locationName =
|
||||
typeof params.action_arguments['location_name'] === 'string'
|
||||
? params.action_arguments['location_name']
|
||||
: null
|
||||
|
||||
if (typeof timeZone !== 'string') {
|
||||
await leon.answer({
|
||||
key: 'time_zone_not_found'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
Intl.DateTimeFormat('en', { timeZone })
|
||||
} catch {
|
||||
await leon.answer({
|
||||
key: 'time_zone_not_found'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const currentDate = new Date(new Date().toLocaleString('en', { timeZone }))
|
||||
await leon.answer({
|
||||
key: 'current_date_time_with_time_zone',
|
||||
data: {
|
||||
weekday: currentDate.toLocaleString(params.lang, { weekday: 'long' }),
|
||||
month: currentDate.toLocaleString(params.lang, { month: 'long' }),
|
||||
day: currentDate.getDate(),
|
||||
year: currentDate.getFullYear(),
|
||||
hours: zeroPad(currentDate.getHours()),
|
||||
minutes: zeroPad(currentDate.getMinutes()),
|
||||
seconds: zeroPad(currentDate.getSeconds()),
|
||||
location_name: locationName || timeZone,
|
||||
time_zone: timeZone
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { zeroPad } from '../lib/zeroPad'
|
||||
|
||||
export const run: ActionFunction = async function () {
|
||||
const currentDate = new Date()
|
||||
await leon.answer({
|
||||
key: 'current_time',
|
||||
data: {
|
||||
hours: zeroPad(currentDate.getHours()),
|
||||
minutes: zeroPad(currentDate.getMinutes()),
|
||||
seconds: zeroPad(currentDate.getSeconds())
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { format } from 'numerable'
|
||||
|
||||
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
|
||||
|
||||
/**
|
||||
* Get the week number (1-52) for a given date.
|
||||
* @link https://stackoverflow.com/a/6117889/11571888
|
||||
* @example getWeekNumber(new Date(2020, 0, 1)) // 1
|
||||
* @example getWeekNumber(new Date(2020, 0, 8)) // 2
|
||||
*/
|
||||
const getWeekNumber = (date: Date): number => {
|
||||
const dateCopy = new Date(date.getTime())
|
||||
dateCopy.setHours(0, 0, 0, 0)
|
||||
dateCopy.setDate(dateCopy.getDate() + 3 - ((dateCopy.getDay() + 6) % 7))
|
||||
const week1 = new Date(dateCopy.getFullYear(), 0, 4)
|
||||
return (
|
||||
1 +
|
||||
Math.round(
|
||||
((dateCopy.getTime() - week1.getTime()) / ONE_DAY_MILLISECONDS -
|
||||
3 +
|
||||
((week1.getDay() + 6) % 7)) /
|
||||
7
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const run: ActionFunction = async function () {
|
||||
const currentDate = new Date()
|
||||
const currentWeekNumber = getWeekNumber(currentDate)
|
||||
await leon.answer({
|
||||
key: 'current_week_number',
|
||||
data: {
|
||||
week_number: format(currentWeekNumber, '0o')
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
|
||||
|
||||
/**
|
||||
* 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 targetDateValue = params.action_arguments['target_date']
|
||||
if (typeof targetDateValue !== 'string') {
|
||||
await leon.answer({
|
||||
key: 'days_countdown_error'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const currentDate = new Date()
|
||||
const futureDate = new Date(targetDateValue)
|
||||
if (Number.isNaN(futureDate.getTime())) {
|
||||
await leon.answer({
|
||||
key: 'days_countdown_error'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Pads a number with zeros.
|
||||
*
|
||||
* @example zeroPad(1, 2) // '01'
|
||||
* @example zeroPad(10, 2) // '10'
|
||||
*/
|
||||
export const zeroPad = (number: number, places = 2): string => {
|
||||
return number.toString().padStart(places, '0')
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"numerable": "0.3.15"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
Reference in New Issue
Block a user