Show same cake day date independent of timezone (#2455)

* Show same cake day date independent of timezone

* Remove commented out assertions
This commit is contained in:
matc-pub 2024-05-12 17:53:29 +02:00 committed by GitHub
parent 0d4a583c9f
commit c80101fde9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 29 deletions

View file

@ -24,7 +24,7 @@ import { canMod } from "@utils/roles";
import type { QueryParams } from "@utils/types"; import type { QueryParams } from "@utils/types";
import { RouteDataResponse } from "@utils/types"; import { RouteDataResponse } from "@utils/types";
import classNames from "classnames"; import classNames from "classnames";
import { format, parseISO } from "date-fns"; import { format } from "date-fns";
import { NoOptionI18nKeys } from "i18next"; import { NoOptionI18nKeys } from "i18next";
import { Component, linkEvent } from "inferno"; import { Component, linkEvent } from "inferno";
import { Link } from "inferno-router"; import { Link } from "inferno-router";
@ -99,6 +99,7 @@ import { PersonListing } from "./person-listing";
import { getHttpBaseInternal } from "../../utils/env"; import { getHttpBaseInternal } from "../../utils/env";
import { IRoutePropsWithFetch } from "../../routes"; import { IRoutePropsWithFetch } from "../../routes";
import { MediaUploads } from "../common/media-uploads"; import { MediaUploads } from "../common/media-uploads";
import { cakeDate } from "@utils/helpers";
type ProfileData = RouteDataResponse<{ type ProfileData = RouteDataResponse<{
personRes: GetPersonDetailsResponse; personRes: GetPersonDetailsResponse;
@ -695,7 +696,7 @@ export class Profile extends Component<ProfileRouteProps, ProfileState> {
<Icon icon="cake" /> <Icon icon="cake" />
<span className="ms-2"> <span className="ms-2">
{I18NextService.i18n.t("cake_day_title")}{" "} {I18NextService.i18n.t("cake_day_title")}{" "}
{format(parseISO(pv.person.published), "PPP")} {format(cakeDate(pv.person.published), "PPP")}
</span> </span>
</div> </div>
{!UserService.Instance.myUserInfo && ( {!UserService.Instance.myUserInfo && (

View file

@ -13,7 +13,7 @@ import getUnixTime from "./get-unix-time";
import { groupBy } from "./group-by"; import { groupBy } from "./group-by";
import hostname from "./hostname"; import hostname from "./hostname";
import hsl from "./hsl"; import hsl from "./hsl";
import isCakeDay from "./is-cake-day"; import isCakeDay, { cakeDate } from "./is-cake-day";
import numToSI from "./num-to-si"; import numToSI from "./num-to-si";
import poll from "./poll"; import poll from "./poll";
import randomStr from "./random-str"; import randomStr from "./random-str";
@ -27,6 +27,7 @@ import dedupByProperty from "./dedup-by-property";
import getApubName from "./apub-name"; import getApubName from "./apub-name";
export { export {
cakeDate,
capitalizeFirstLetter, capitalizeFirstLetter,
debounce, debounce,
editListImmutable, editListImmutable,

View file

@ -1,34 +1,18 @@
import { parseISO, getYear, getDayOfYear, isLeapYear } from "date-fns"; import { getYear, isSameDay, isSameYear, parse, setYear } from "date-fns";
const leapDay = getDayOfYear(new Date(2024, 1, 29)); // Returns a date in local time with the same year, month and day. Ignores the
// source timezone. The goal is to show the same date in all timezones.
export function cakeDate(published: string): Date {
return parse(published.substring(0, 10), "yyyy-MM-dd", new Date(0));
}
export default function isCakeDay(published: string): boolean { export default function isCakeDay(published: string): boolean {
const createDate = parseISO(published); const createDate = cakeDate(published);
const createDateDayOfYear = getDayOfYear(createDate);
const isCreateDateLeapYear = isLeapYear(createDate);
const currentDate = new Date(); const currentDate = new Date();
let currentDateDayOfYear = getDayOfYear(currentDate);
const isCurrentDateLeapYear = isLeapYear(currentDate);
if (
isCreateDateLeapYear &&
!isCurrentDateLeapYear &&
currentDateDayOfYear >= leapDay
) {
++currentDateDayOfYear;
}
if (
!isCreateDateLeapYear &&
isCurrentDateLeapYear &&
createDateDayOfYear >= leapDay
) {
--currentDateDayOfYear;
}
// The day-overflow of Date makes leap days become 03-01 in non leap years.
return ( return (
createDateDayOfYear === currentDateDayOfYear && isSameDay(currentDate, setYear(createDate, getYear(currentDate))) &&
getYear(createDate) !== getYear(currentDate) !isSameYear(currentDate, createDate)
); );
} }