Skip to content

Commit

Permalink
chore(date): clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
johnleider committed Apr 10, 2023
1 parent ea590b9 commit db55023
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 95 deletions.
6 changes: 4 additions & 2 deletions packages/vuetify/src/adapters/vuetify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Types
// Utilities
import { createRange } from '@/util'

// Types
import type { DateAdapter } from './date-adapter'

function getWeekArray (date: Date) {
const weeks = []
let currentWeek = []
const weeks = []
const firstDayOfMonth = startOfMonth(date)
const lastDayOfMonth = endOfMonth(date)

Expand Down
4 changes: 4 additions & 0 deletions packages/vuetify/src/composables/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { propsFactory } from '@/util'
import type { InjectionKey, PropType } from 'vue'
import type { DateAdapter } from '@/adapters/date-adapter'

export type DateOptions = {
adapter: { new(locale: string): DateAdapter<any> }
}

export const DateAdapterSymbol: InjectionKey<{ adapter: { new(locale: string): DateAdapter<any> } }> = Symbol.for('vuetify:date-adapter')

export interface DateProps {
Expand Down
6 changes: 2 additions & 4 deletions packages/vuetify/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ import { nextTick, reactive } from 'vue'

// Types
import type { App, ComponentPublicInstance, InjectionKey } from 'vue'
import type { DateOptions } from '@/composables/date'
import type { DefaultsOptions } from '@/composables/defaults'
import type { DisplayOptions } from '@/composables/display'
import type { IconOptions } from '@/composables/icons'
import type { LocaleOptions, RtlOptions } from '@/composables/locale'
import type { ThemeOptions } from '@/composables/theme'
import type { DateAdapter } from './adapters/date-adapter'

export * from './composables'

export interface VuetifyOptions {
aliases?: Record<string, any>
blueprint?: Blueprint
components?: Record<string, any>
date?: {
adapter: { new(locale: string): DateAdapter<any> }
}
date?: DateOptions
directives?: Record<string, any>
defaults?: DefaultsOptions
display?: DisplayOptions
Expand Down
89 changes: 0 additions & 89 deletions packages/vuetify/src/util/dateTimeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,3 @@
import { createRange, padStart } from '@/util'

export function getFirstDayOfMonth (year: number, month: number) {
return new Date(year, month, 1)
}

export function getLastDayOfMonth (year: number, month: number) {
return new Date(year, month + 1, 0)
}

export function getNumberOfDaysInMonth (year: number, month: number) {
return new Date(year, month, 0).getDate()
}

export function getMonthName (month: number, locale = 'en-US') {
const date = new Date(`2022-${month}-01`)
return date.toLocaleString(locale, { month: 'long' })
}

export function changeMonth (year: number, month: number, change: number) {
if (change < 0 && month + change < 1) {
return [year - 1, 12 + (month + change)]
} else if (change > 0 && month + change > 12) {
return [year + 1, (month + change) % 12]
} else {
return [year, month + change]
}
}

export function getDate (year: number, month: number, day: number) {
return `${year}-${padStart(String(month), 2, '0')}-${padStart(String(day), 2, '0')}`
}

export function addDays (date: string, days: number) {
const d = new Date(date)

d.setDate(d.getDate() + days)

return getDate(d.getFullYear(), d.getMonth() + 1, d.getDate())
}

export function parseDate (date: string): [number, number, number] {
const [year, month, day] = date.split('-').map(Number)

return [year, month, day]
}

function getMondayOfFirstWeekOfYear (year: number) {
const januaryFirst = new Date(year, 0, 1)
const mondayOfFirstWeek = new Date(januaryFirst)

mondayOfFirstWeek.setDate(mondayOfFirstWeek.getDate() + ((11 - januaryFirst.getDay()) % 7) - 3)

return mondayOfFirstWeek
}

// https://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date/275024#275024
export function getWeek (date: Date) {
let year = date.getFullYear()
let d1w1 = getMondayOfFirstWeekOfYear(year)

if (date < d1w1) {
year = year - 1
d1w1 = getMondayOfFirstWeekOfYear(year)
} else {
const tv = getMondayOfFirstWeekOfYear(year + 1)
if (date >= tv) {
year = year + 1
d1w1 = tv
}
}

const diffTime = Math.abs(date.getTime() - d1w1.getTime())
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))

return Math.floor(diffDays / 7) + 1
}

export function getDaysInMonth (year: number, month: number) {
return createRange(getNumberOfDaysInMonth(year, month), 1).map(day => ({
year,
month,
day,
date: getDate(year, month, day),
}))
}

// old

function createUTCDate (year: number, month = 0, day = 1) {
let date
if (year < 100 && year >= 0) {
Expand Down

0 comments on commit db55023

Please sign in to comment.