Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: days are 0 when duration >1 month #3

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/commands/time-event/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const TIME_EVENTS_CHANNEL_ID = '1210366431707930675';
export const TIME_EVENTS_PUBLIC_CATEGORY_ID = '1211037531181686805';
export const TIME_EVENT_DURATION_FORMAT = 'Dд. Hч. mм.';
export const COURT_DUELIST_ROLE_ID = '1206749097126535268';
17 changes: 9 additions & 8 deletions src/time-event/time-event.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Prisma, TimeEventChannelType } from '@prisma/client';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import dayjsDurationPlugin from 'dayjs/plugin/duration';
import { ChannelType, Guild, GuildChannelCreateOptions } from 'discord.js';

import { Injectable } from '@nestjs/common';

import {
COURT_DUELIST_ROLE_ID,
TIME_EVENTS_PUBLIC_CATEGORY_ID,
TIME_EVENT_DURATION_FORMAT,
} from '../commands/time-event/constants';
import { COURT_DUELIST_ROLE_ID, TIME_EVENTS_PUBLIC_CATEGORY_ID } from '../commands/time-event/constants';
import 'dayjs/locale/ru';
import { PrismaService } from '../prisma/prisma.service';

dayjs.extend(duration);
dayjs.extend(dayjsDurationPlugin);

@Injectable()
export class TimeEventService {
constructor(private readonly prisma: PrismaService) {}

getEventTimeLeftInMinutes(date: dayjs.Dayjs): string {
return dayjs.duration(dayjs(date).diff(dayjs(), 'minutes'), 'minutes').format(TIME_EVENT_DURATION_FORMAT);
const duration = dayjs.duration(dayjs(date).diff(dayjs(), 'minutes'), 'minutes');
const days = Math.floor(duration.asDays());
const hoursWithFraction = (duration.asDays() % 1) * 24;
const minutesWithFraction = (duration.asDays() % 1) * 60;

return `${days}д. ${Math.floor(hoursWithFraction)}ч. ${Math.floor(minutesWithFraction)}м.`;
}

getTimerChannelTitle(date: dayjs.Dayjs): string {
Expand Down
Loading