watching-time.ts 770 B

1234567891011121314151617181920212223
  1. import type { Nullable } from '../../../../../types/utils'
  2. export function calculateWatchingTimeInSecond(
  3. startTimeWatchedFirstVideo: number,
  4. startTimeWatchedSecondVideo: Nullable<number>
  5. ) {
  6. let firstVideoWatchingTimeInSecond = 0
  7. let secondVideoWatchingTimeInSecond = 0
  8. if (startTimeWatchedSecondVideo === null) {
  9. firstVideoWatchingTimeInSecond = Math.floor(
  10. (Date.now() - startTimeWatchedFirstVideo) / 1000
  11. )
  12. } else {
  13. firstVideoWatchingTimeInSecond = Math.floor(
  14. (startTimeWatchedSecondVideo - startTimeWatchedFirstVideo) / 1000
  15. )
  16. secondVideoWatchingTimeInSecond = Math.floor(
  17. (Date.now() - startTimeWatchedSecondVideo) / 1000
  18. )
  19. }
  20. return { firstVideoWatchingTimeInSecond, secondVideoWatchingTimeInSecond }
  21. }