You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <script lang="ts">
  2. import EveryMinute from "./EveryMinute.svelte";
  3. export let startTime: Date | string = new Date();
  4. export let endTime: Date | string = new Date();
  5. export let compact: boolean = false;
  6. let started = false;
  7. let overdue = false;
  8. let danger = false;
  9. let amount = 0;
  10. let amountStr = "0";
  11. let unit = "days";
  12. let titleTimeStr = "";
  13. let now = new Date();
  14. function formatTime(time: Date): string {
  15. const pad = (n:number) => n < 9 ? '0'+n : n.toString();
  16. return `${time.getFullYear()}-${pad(time.getMonth()+1)}-${pad(time.getDate())}`
  17. }
  18. $: {
  19. overdue = false;
  20. unit = "days";
  21. const st = (startTime instanceof Date) ? startTime : new Date(startTime);
  22. const et = (endTime instanceof Date) ? endTime : new Date(endTime);
  23. if (now < st) {
  24. started = false;
  25. amount = (st.getTime() - now.getTime()) / 86400000
  26. } else {
  27. started = true;
  28. amount = (et.getTime() - now.getTime()) / 86400000
  29. }
  30. if (amount < 0) {
  31. overdue = true;
  32. amount = -amount;
  33. }
  34. danger = (!overdue && started && amount <= 3);
  35. if (amount < 2) {
  36. amount *= 24;
  37. unit = "hours"
  38. }
  39. if (amount < 2) {
  40. amount *= 60;
  41. unit = "minutes";
  42. }
  43. amount = Math.floor(amount);
  44. if (amount <= 1) {
  45. unit = unit.slice(0, -1);
  46. }
  47. if (amount < 1) {
  48. amountStr = "< 1"
  49. } else {
  50. amountStr = amount.toString()
  51. }
  52. if (compact) {
  53. unit = unit.slice(0, 1);
  54. }
  55. titleTimeStr = `${formatTime(new Date(startTime))} – ${formatTime(new Date(endTime))}`
  56. }
  57. </script>
  58. <EveryMinute bind:now={now} />
  59. <span title={titleTimeStr}>
  60. {#if (overdue)}
  61. <span class="overdue">{amountStr}{compact ? "" : " "}{unit} {!compact ? "ago" : ""}</span>
  62. {:else if (started)}
  63. <span class:danger class="started">{amountStr}{compact ? "" : " "}{unit} {!compact ? "left" : ""}</span>
  64. {:else}
  65. <span class="pending">In {amountStr}{compact ? "" : " "}{unit}</span>
  66. {/if}
  67. </span>
  68. <style>
  69. span {
  70. color: #ccc;
  71. }
  72. span.pending {
  73. color: #2797e2;
  74. }
  75. span.danger {
  76. color: #e28127;
  77. }
  78. span.overdue {
  79. color: #666666;
  80. }
  81. </style>