Skip to content

Commit 6a12713

Browse files
committed
clean date
1 parent 2a794ed commit 6a12713

File tree

4 files changed

+45
-62
lines changed

4 files changed

+45
-62
lines changed

workers/src/handlers/commands.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { sendTelegramMessage, sendAdmin } from '../services/telegram'
22
import { addFlightTracking, clearUserTracking, untrackFlight } from '../services/tracking'
33
import { getFlightIdByNumber, getNotTrackedFlights, generateFakeFlights, storeFlights } from '../services/flightData'
4-
import { getCurrentIdtTime, formatTimeAgo, formatTimestampForDisplay } from '../utils/dateTime'
5-
import { formatTrackingListOptimized, formatFlightSuggestions, escapeMarkdown } from '../utils/formatting'
4+
import {
5+
formatTrackingListOptimized,
6+
formatFlightSuggestions,
7+
escapeMarkdown,
8+
formatTimestampForDisplay,
9+
formatTimeAgo,
10+
} from '../utils/formatting'
611
import { isValidFlightCode } from '../utils/validation'
712
import { CRON_PERIOD_SECONDS, getTelegramUrl } from '../utils/constants'
813
import type { Env } from '../env'

workers/src/services/flightData.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { getCurrentIdtTime, getIdtTimeString } from '../utils/dateTime'
1+
import { getCurrentIdtTime } from '../utils/dateTime'
22
import { VERCEL_FLIGHTS_API_URL } from '../utils/constants'
33
import { ofetch } from 'ofetch'
44
import type { Env } from '../env'
55
import type { Flight, VercelApiResponse, VercelFlightResponse, DOProps } from '../types'
66
import { sendTelegramMessage, sendAdmin } from '../services/telegram'
7+
import { formatTimeFromTimestamp } from '../utils/formatting'
78
export const generateFakeFlights = (ctx: DurableObjectState<DOProps>): Flight[] => {
89
const now = getCurrentIdtTime(ctx)
910
const futureTime1 = new Date(now.getTime() + 2 * 60 * 60 * 1000) // 2 hours from now
@@ -93,14 +94,13 @@ export const detectChanges = (
9394
ctx: DurableObjectState<DOProps>
9495
) => {
9596
const changes: string[] = []
96-
const changeId = Math.random().toString(36).substring(7)
9797

9898
if (prevFlight.status !== currentFlight.status) {
9999
changes.push(`📍 Status: ${currentFlight.status}`)
100100
}
101101
if (prevFlight.eta !== currentFlight.eta) {
102-
const prevDt = getIdtTimeString(prevFlight.eta)
103-
const currentDt = getIdtTimeString(currentFlight.eta)
102+
const prevDt = formatTimeFromTimestamp(prevFlight.eta)
103+
const currentDt = formatTimeFromTimestamp(currentFlight.eta)
104104
const prevTime = prevDt ?? 'TBA'
105105
const currentTime = currentDt ?? 'TBA'
106106
changes.push(`🕒 Arrival Time: ${currentTime} (was ${prevTime})`)

workers/src/utils/dateTime.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,6 @@ export const getCurrentIdtTimeString = (ctx: DurableObjectState<DOProps>) => {
4848
return cache.idtTimeString
4949
}
5050

51-
export const getIdtDateString = (n: number) => {
52-
return new Date(n).toLocaleString('en-US', {
53-
timeZone: 'Asia/Jerusalem',
54-
hour12: false,
55-
})
56-
}
57-
58-
export const getIdtTimeString = (n: number) => {
59-
return new Date(n).toLocaleTimeString('en-US', {
60-
timeZone: 'Asia/Jerusalem',
61-
hour12: false,
62-
hour: '2-digit',
63-
minute: '2-digit',
64-
})
65-
}
66-
6751
// Non-cached versions for use outside Durable Object context
6852
export const getCurrentIdtTimeNoCache = () => {
6953
const now = new Date()
@@ -77,34 +61,3 @@ export const getCurrentIdtDateStringNoCache = () => {
7761
hour12: false,
7862
})
7963
}
80-
81-
export const formatTimeAgo = (timestamp: number, ctx: DurableObjectState<DOProps>): string => {
82-
if (!timestamp || timestamp === 0) {
83-
return '- not updated'
84-
}
85-
86-
const now = getCurrentIdtTime(ctx).getTime()
87-
const diffMs = now - timestamp
88-
const diffMinutes = Math.floor(diffMs / 60000)
89-
90-
if (diffMinutes < 1) {
91-
return 'just now'
92-
} else if (diffMinutes < 60) {
93-
return `${diffMinutes} minute${diffMinutes > 1 ? 's' : ''} ago`
94-
} else if (diffMinutes < 1440) {
95-
const hours = Math.floor(diffMinutes / 60)
96-
return `${hours} hour${hours > 1 ? 's' : ''} ago`
97-
} else {
98-
const days = Math.floor(diffMinutes / 1440)
99-
return `${days} day${days > 1 ? 's' : ''} ago`
100-
}
101-
}
102-
103-
export const formatTimestampForDisplay = (timestamp: number): string => {
104-
if (!timestamp || timestamp === 0) {
105-
return '- not updated'
106-
}
107-
108-
const date = new Date(timestamp)
109-
return date.toISOString().split('T')[0]
110-
}

workers/src/utils/formatting.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { getCurrentIdtTime } from './dateTime'
33
import type { Env } from '../env'
44
import type { Flight, InlineKeyboardButton, InlineKeyboardMarkup, DOProps } from '../types'
55

6-
// Helper function to format time from timestamp
76
export const formatTimeFromTimestamp = (timestamp: number) => {
87
const date = new Date(timestamp)
98
return date.toLocaleTimeString('en-GB', {
@@ -12,8 +11,13 @@ export const formatTimeFromTimestamp = (timestamp: number) => {
1211
hour12: false,
1312
})
1413
}
14+
export const formatDateFromTimestamp = (timestamp: number) => {
15+
const date = new Date(timestamp)
16+
return date.toLocaleString('en-GB', {
17+
hour12: false,
18+
})
19+
}
1520

16-
// Helper function to get day label from timestamp
1721
export const getDayLabelFromTimestamp = (timestamp: number, ctx: DurableObjectState<DOProps>) => {
1822
const date = new Date(timestamp)
1923
const todayIdt = getCurrentIdtTime(ctx)
@@ -34,16 +38,37 @@ export const getDayLabelFromTimestamp = (timestamp: number, ctx: DurableObjectSt
3438
}
3539
}
3640

37-
export const formatTrackingList = async (userFlights: string[], env: Env) => {
38-
if (userFlights.length === 0) return "You're not tracking any flights.\nUse /track LY086 to start!"
41+
export const formatTimeAgo = (timestamp: number, ctx: DurableObjectState<DOProps>): string => {
42+
if (!timestamp || timestamp === 0) {
43+
return '- not updated'
44+
}
45+
46+
const now = getCurrentIdtTime(ctx).getTime()
47+
const diffMs = now - timestamp
48+
const diffMinutes = Math.floor(diffMs / 60000)
49+
50+
if (diffMinutes < 1) {
51+
return 'just now'
52+
} else if (diffMinutes < 60) {
53+
return `${diffMinutes} minute${diffMinutes > 1 ? 's' : ''} ago`
54+
} else if (diffMinutes < 1440) {
55+
const hours = Math.floor(diffMinutes / 60)
56+
return `${hours} hour${hours > 1 ? 's' : ''} ago`
57+
} else {
58+
const days = Math.floor(diffMinutes / 1440)
59+
return `${days} day${days > 1 ? 's' : ''} ago`
60+
}
61+
}
62+
63+
export const formatTimestampForDisplay = (timestamp: number): string => {
64+
if (!timestamp || timestamp === 0) {
65+
return '- not updated'
66+
}
3967

40-
// Use the optimized function to get flight data in a single query
41-
const chatId = parseInt(userFlights[0].split('_')[0]) // Extract chat ID from first flight ID (this is a hack, need to pass chatId properly)
42-
// Actually, let's modify the function signature to accept chatId instead of userFlights array
43-
return 'Function needs to be called with chatId parameter for optimization'
68+
const date = new Date(timestamp)
69+
return date.toISOString().split('T')[0]
4470
}
4571

46-
// New optimized version that takes chatId directly
4772
export const formatTrackingListOptimized = (
4873
chatId: number,
4974
env: Env,

0 commit comments

Comments
 (0)