github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/client/src/time.ts (about)

     1  // Content managed by Project Forge, see [projectforge.md] for details.
     2  import {els} from "./dom";
     3  
     4  export function utc(date: Date) {
     5    const u = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
     6    return new Date(u).toISOString().substring(0, 19).replace("T", " ");
     7  }
     8  
     9  export function wireTime(el: HTMLElement) {
    10    const tsStr = el.dataset.timestamp;
    11    if (!tsStr) {
    12      console.log("invalid timestamp [" + tsStr + "]", el);
    13      return;
    14    }
    15    const ts = new Date(tsStr);
    16  
    17    const tsEl = document.createElement("span");
    18    tsEl.title = ts.toISOString();
    19    if (el.classList.contains("millis")) {
    20      tsEl.innerText = ts.toISOString();
    21    } else {
    22      tsEl.innerText = ts.toLocaleString();
    23    }
    24    el.replaceChildren(tsEl);
    25  }
    26  
    27  export function relativeTime(el: HTMLElement): string {
    28    const timestamp = el.dataset.timestamp;
    29    const str = (timestamp || "").replace(/-/gu, "/").replace(/[TZ]/gu, " ") + " UTC";
    30    const date = new Date(str);
    31    const diff = (new Date().getTime() - date.getTime()) / 1000;
    32    const dayDiff = Math.floor(diff / 86400);
    33    const year = date.getFullYear();
    34    const month = date.getMonth() + 1;
    35    const day = date.getDate();
    36  
    37    if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
    38      return year.toString() + "-" + (month < 10 ? "0" + month.toString() : month.toString()) + "-" + (day < 10 ? "0" + day.toString() : day.toString());
    39    }
    40  
    41    let ret: string;
    42    let timeoutSeconds: number;
    43    if (dayDiff === 0) {
    44      if (diff < 5) {
    45        timeoutSeconds = 1;
    46        ret = "just now";
    47      } else if (diff < 60) {
    48        timeoutSeconds = 1;
    49        ret = Math.floor(diff) + " seconds ago";
    50      } else if (diff < 120) {
    51        timeoutSeconds = 10;
    52        ret = "1 minute ago";
    53      } else if (diff < 3600) {
    54        timeoutSeconds = 30;
    55        ret = Math.floor(diff / 60) + " minutes ago";
    56      } else if (diff < 7200) {
    57        timeoutSeconds = 60;
    58        ret = "1 hour ago";
    59      } else {
    60        timeoutSeconds = 60;
    61        ret = Math.floor(diff / 3600) + " hours ago";
    62      }
    63    } else if (dayDiff === 1) {
    64      timeoutSeconds = 600;
    65      ret = "yesterday";
    66    } else if (dayDiff < 7) {
    67      timeoutSeconds = 600;
    68      ret = dayDiff + " days ago";
    69    } else {
    70      timeoutSeconds = 6000;
    71      ret = Math.ceil(dayDiff / 7) + " weeks ago";
    72    }
    73    if (el) {
    74      el.innerText = ret;
    75      setTimeout(() => relativeTime(el), timeoutSeconds * 1000);
    76    }
    77    return ret;
    78  }
    79  
    80  export function timeInit() {
    81    els(".timestamp").forEach((el) => {
    82      wireTime(el);
    83    });
    84    els(".reltime").forEach((el) => {
    85      relativeTime(el);
    86    });
    87    return [wireTime, relativeTime];
    88  }