|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Represents a date label on the timeline ruler at a specific day boundary. |
| 19 | + * |
| 20 | + * This label indicates the transition between two days. For example, exactly at the |
| 21 | + * midnight boundary between March 11 and March 12, `labelLeft` will be 'YYYY/MM/11' |
| 22 | + * and `labelRight` will be 'YYYY/MM/12'. |
| 23 | + */ |
| 24 | +export interface DateLabel { |
| 25 | + /** The horizontal offset from the left edge of the timeline in pixels representing the boundary time. */ |
| 26 | + offsetX: number; |
| 27 | + /** The date string (YYYY/MM/DD) representing the day immediately before the boundary. */ |
| 28 | + labelLeft: string; |
| 29 | + /** The date string (YYYY/MM/DD) representing the day starting exactly at the boundary. */ |
| 30 | + labelRight: string; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Calculates the positions and formatted text for date labels (e.g., "YYYY/MM/DD") based on the current |
| 35 | + * visible time range on the timeline ruler. This ensures that day boundaries crossing the view |
| 36 | + * are correctly labeled. |
| 37 | + * |
| 38 | + * The calculation mathematically aligns to the exact local time midnight boundary by factoring in |
| 39 | + * the given timezone shift, ensuring that the visual representation maps exactly to the viewer's local day. |
| 40 | + * |
| 41 | + * @param leftEdgeTime The timestamp (in UTC milliseconds) corresponding to the visible left edge of the timeline. |
| 42 | + * @param visibleDuration The total duration (in milliseconds) visible on the screen. |
| 43 | + * @param pixelsPerMs The current zoom level, expressed as pixels per millisecond. |
| 44 | + * @param timezoneShiftHours The timezone offset in hours to apply. Positive values move ahead of UTC, negative values move behind. |
| 45 | + * @returns An array of `DateLabel` objects representing the day boundaries within the visible range. |
| 46 | + */ |
| 47 | +export function calculateDateLabels( |
| 48 | + leftEdgeTime: number, |
| 49 | + visibleDuration: number, |
| 50 | + pixelsPerMs: number, |
| 51 | + timezoneShiftHours: number, |
| 52 | +): DateLabel[] { |
| 53 | + const labels: DateLabel[] = []; |
| 54 | + const DAY = 60 * 60 * 24 * 1000; |
| 55 | + const timezoneShiftInMs = timezoneShiftHours * 60 * 60 * 1000; |
| 56 | + |
| 57 | + const localLeftEdgeTime = leftEdgeTime + timezoneShiftInMs; |
| 58 | + const localPrevMidnight = Math.floor(localLeftEdgeTime / DAY) * DAY - DAY; |
| 59 | + let prevDayTime = localPrevMidnight - timezoneShiftInMs; |
| 60 | + |
| 61 | + while (true) { |
| 62 | + const currentDayTime = prevDayTime + DAY; |
| 63 | + if (currentDayTime > leftEdgeTime + visibleDuration) { |
| 64 | + break; |
| 65 | + } |
| 66 | + labels.push({ |
| 67 | + offsetX: (currentDayTime - leftEdgeTime) * pixelsPerMs, |
| 68 | + labelLeft: toDateLabel(currentDayTime - 1, timezoneShiftHours), |
| 69 | + labelRight: toDateLabel(currentDayTime, timezoneShiftHours), |
| 70 | + }); |
| 71 | + prevDayTime = currentDayTime; |
| 72 | + } |
| 73 | + return labels; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Formats a given timestamp into a date string (YYYY/MM/DD) according to the configured timezone shift. |
| 78 | + * |
| 79 | + * @param time The timestamp in milliseconds to format. |
| 80 | + * @param timezoneShiftHours The timezone offset in hours applied to the formatting. |
| 81 | + * @returns The formatted date string. |
| 82 | + */ |
| 83 | +function toDateLabel(time: number, timezoneShiftHours: number): string { |
| 84 | + const date = new Date(time + timezoneShiftHours * 60 * 60 * 1000); |
| 85 | + const year = date.getUTCFullYear(); |
| 86 | + const month = ('' + (date.getUTCMonth() + 1)).padStart(2, '0'); |
| 87 | + const day = ('' + date.getUTCDate()).padStart(2, '0'); |
| 88 | + return `${year}/${month}/${day}`; |
| 89 | +} |
0 commit comments