File size: 3,013 Bytes
17cf14d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Timezone-aware datetime utilities (similar to your Python datetime_utils.py)

/**

 * Get current time in configured timezone

 */
export function nowTz(): Date {
  return new Date()
}

/**

 * Calculate days until a target date

 */
export function daysUntil(target: Date | string): number {
  const targetDate = typeof target === 'string' ? new Date(target) : target
  const now = new Date()
  const diffTime = targetDate.getTime() - now.getTime()
  return Math.floor(diffTime / (1000 * 60 * 60 * 24))
}

/**

 * Calculate hours until a target date

 */
export function hoursUntil(target: Date | string): number {
  const targetDate = typeof target === 'string' ? new Date(target) : target
  const now = new Date()
  const diffTime = targetDate.getTime() - now.getTime()
  return diffTime / (1000 * 60 * 60)
}

/**

 * Check if current time is within a window around target time

 * @param target Target datetime

 * @param windowMinutes Window size in minutes (default: 60)

 * @returns True if now is between [target - window, target + window]

 */
export function isWithinWindow(target: Date | string, windowMinutes: number = 60): boolean {
  const targetDate = typeof target === 'string' ? new Date(target) : target
  const now = new Date()
  const diffMs = Math.abs(now.getTime() - targetDate.getTime())
  return diffMs <= windowMinutes * 60 * 1000
}

/**

 * Add hours to a date

 */
export function addHours(date: Date | string, hours: number): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date)
  d.setHours(d.getHours() + hours)
  return d
}

/**

 * Add days to a date

 */
export function addDays(date: Date | string, days: number): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date)
  d.setDate(d.getDate() + days)
  return d
}

/**

 * Format date for display with timezone

 */
export function formatDateTz(date: Date | string | null | undefined, options?: Intl.DateTimeFormatOptions): string {
  if (!date) return 'N/A'
  
  const d = typeof date === 'string' ? new Date(date) : date
  
  return d.toLocaleString('en-US', {
    timeZone: process.env.NEXT_PUBLIC_TIMEZONE || 'America/New_York',
    ...options,
  })
}

/**

 * Check if a date is in the past

 */
export function isPast(date: Date | string): boolean {
  const d = typeof date === 'string' ? new Date(date) : date
  return d.getTime() < Date.now()
}

/**

 * Check if a date is in the future

 */
export function isFuture(date: Date | string): boolean {
  return !isPast(date)
}

/**

 * Get start of day for a date

 */
export function startOfDay(date: Date | string): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date)
  d.setHours(0, 0, 0, 0)
  return d
}

/**

 * Get end of day for a date

 */
export function endOfDay(date: Date | string): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date)
  d.setHours(23, 59, 59, 999)
  return d
}