All files / src/features/Events/EventDetail/reviews reviewDate.ts

92.85% Statements 13/14
92.85% Branches 13/14
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        23x                     23x 118x   107x 107x   107x 107x       107x 102x 101x       5x        
import moment from "moment-timezone";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
 
/** Below this many days a review reads as relative ("2 days ago"). */
const RELATIVE_DAYS = 7;
 
/**
 * How a review or reply date is written on the card (frames 7878-57940 /
 * 7878-56665 show both forms: "2 days ago" and "24 Jun").
 *
 * The API serves epoch SECONDS and uses 0/null for "unset". Neither is ever fed
 * to a Date directly — an unset value returns "" so the card renders nothing,
 * rather than the 1970 the platform has been bitten by before. Formatting goes
 * through the platform IANA zone, never a hardcoded offset.
 */
export const formatReviewDate = (timestamp?: number | null): string => {
  if (!timestamp) return "";
 
  const when = moment.unix(timestamp).tz(PLATFORM_TIMEZONE);
  Iif (!when.isValid()) return "";
 
  const now = moment().tz(PLATFORM_TIMEZONE);
  const days = now.diff(when, "days");
 
  // A future timestamp is not a real review date; fall through to the absolute
  // form rather than printing "in 3 days".
  if (days >= 0 && days < RELATIVE_DAYS) {
    if (days === 0) return "Today";
    return days === 1 ? "1 day ago" : `${days} days ago`;
  }
 
  // The year is only worth the space once it is not the current one.
  return when.year() === now.year()
    ? when.format("DD MMM")
    : when.format("DD MMM YYYY");
};