Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Take into account year to isThisMonth

Chocobozzzd47fefe3767b
diff --git a/packages/core-utils/src/common/date.ts b/packages/core-utils/src/common/date.ts
index cd74885676..be1e36a9c8 100644
--- a/packages/core-utils/src/common/date.ts
+++ b/packages/core-utils/src/common/date.ts
@@ -13,7 +13,7 @@ export function isYesterday (d: Date) {
export function isThisWeek (d: Date) {
const minDateOfThisWeek = new Date()
- minDateOfThisWeek.setHours(0, 0, 0)
+ minDateOfThisWeek.setHours(0, 0, 0, 0)
// getDay() -> Sunday - Saturday : 0 - 6
// We want to start our week on Monday
@@ -26,9 +26,9 @@ export function isThisWeek (d: Date) {
}
export function isThisMonth (d: Date) {
- const thisMonth = new Date().getMonth()
+ const now = new Date()
- return d.getMonth() === thisMonth
+ return d.getMonth() === now.getMonth() && d.getFullYear() === now.getFullYear()
}
export function isLastMonth (d: Date) {
diff --git a/packages/tests/src/core-utils/date.ts b/packages/tests/src/core-utils/date.ts
index eba8cdd103..a0ccda7f8b 100644
--- a/packages/tests/src/core-utils/date.ts
+++ b/packages/tests/src/core-utils/date.ts
@@ -1,6 +1,116 @@
-import { millisecondsToTime, millisecondsToVttTime, secondsToTime } from '@peertube/peertube-core-utils'
+/* oxlint-disable @typescript-eslint/no-unused-expressions */
+
+import {
+ isLastMonth,
+ isLastWeek,
+ isThisMonth,
+ isThisWeek,
+ isToday,
+ isYesterday,
+ millisecondsToTime,
+ millisecondsToVttTime,
+ secondsToTime
+} from '@peertube/peertube-core-utils'
import { expect } from 'chai'
+describe('Is today', function () {
+ it('Returns true for the current date', function () {
+ expect(isToday(new Date())).to.be.true
+ })
+
+ it('Returns false for yesterday', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 1)
+
+ expect(isToday(d)).to.be.false
+ })
+})
+
+describe('Is yesterday', function () {
+ it('Returns true for yesterday', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 1)
+
+ expect(isYesterday(d)).to.be.true
+ })
+
+ it('Returns false for today', function () {
+ expect(isYesterday(new Date())).to.be.false
+ })
+})
+
+describe('Is this week', function () {
+ it('Returns true for today', function () {
+ expect(isThisWeek(new Date())).to.be.true
+ })
+
+ it('Returns false for 8 days ago', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 8)
+
+ expect(isThisWeek(d)).to.be.false
+ })
+
+ it('Returns true for the very start of today, ignoring milliseconds', function () {
+ const d = new Date()
+ d.setHours(0, 0, 0, 0)
+
+ expect(isThisWeek(d)).to.be.true
+ })
+})
+
+describe('Is this month', function () {
+ it('Returns true for the current date', function () {
+ expect(isThisMonth(new Date())).to.be.true
+ })
+
+ it('Returns false for the same month last year', function () {
+ const d = new Date()
+ d.setFullYear(d.getFullYear() - 1)
+
+ expect(isThisMonth(d)).to.be.false
+ })
+
+ it('Returns false for last month', function () {
+ const d = new Date()
+ d.setMonth(d.getMonth() - 1)
+
+ expect(isThisMonth(d)).to.be.false
+ })
+})
+
+describe('Is last month', function () {
+ it('Returns true for 15 days ago', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 15)
+
+ expect(isLastMonth(d)).to.be.true
+ })
+
+ it('Returns false for 40 days ago', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 40)
+
+ expect(isLastMonth(d)).to.be.false
+ })
+})
+
+describe('Is last week', function () {
+ it('Returns true for 3 days ago', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 3)
+
+ expect(isLastWeek(d)).to.be.true
+ })
+
+ it('Returns false for 10 days ago', function () {
+ const d = new Date()
+ d.setDate(d.getDate() - 10)
+
+ expect(isLastWeek(d)).to.be.false
+ })
+})
+
describe('Seconds to time', function () {
it('Outputs a human readable time', function () {
expect(secondsToTime(61.1335)).to.equals('1m1s')
@@ -29,7 +139,6 @@ describe('Milliseconds to time', function () {
})
describe('Milliseconds to WebVTT time', function () {
-
it('Should have a valid WebVTT time', function () {
expect(millisecondsToVttTime(1000)).to.equal('00:00:01.000')
expect(millisecondsToVttTime(1001)).to.equal('00:00:01.001')