config_and_utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Config & utilidades auditoría (sin cambios funcionales)
  2. (function(){
  3. function toggleDetails(header) {
  4. const details = header.nextElementSibling;
  5. const icon = header.querySelector('.toggle-icon');
  6. if (details.style.display === 'block') {
  7. details.style.display = 'none';
  8. icon && icon.classList.remove('rotate-180');
  9. } else {
  10. details.style.display = 'block';
  11. icon && icon.classList.add('rotate-180');
  12. }
  13. }
  14. function getLocalDateString(){
  15. const today = new Date();
  16. const year = today.getFullYear();
  17. const month = String(today.getMonth() + 1).padStart(2, '0');
  18. const day = String(today.getDate()).padStart(2, '0');
  19. return `${year}-${month}-${day}`;
  20. }
  21. function formatBytes(bytes, decimals = 2) {
  22. if (!+bytes) return '0 Bytes';
  23. const k = 1024;
  24. const dm = decimals < 0 ? 0 : decimals;
  25. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  26. const i = Math.floor(Math.log(bytes) / Math.log(k));
  27. return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
  28. }
  29. function formatTime(datetimeString){
  30. if (!datetimeString || typeof datetimeString !== 'string') return '--';
  31. try {
  32. const parts = datetimeString.split(' ');
  33. if (parts.length < 5) return '--';
  34. const timePart = parts[4];
  35. const [hoursStr, minutesStr, secondsStr] = timePart.split(':');
  36. let hour = parseInt(hoursStr, 10);
  37. if (isNaN(hour)) return '--';
  38. const ampm = hour >= 12 ? 'PM' : 'AM';
  39. hour = hour % 12;
  40. hour = hour || 12;
  41. const formattedHour = String(hour).padStart(2, '0');
  42. return `${formattedHour}:${minutesStr}:${secondsStr} ${ampm}`;
  43. } catch(e){
  44. console.error('Error crítico al formatear la hora:', e, 'Input was:', datetimeString);
  45. return '--';
  46. }
  47. }
  48. // Exponer
  49. window.toggleDetails = toggleDetails;
  50. window.getLocalDateString = getLocalDateString;
  51. window.formatBytes = formatBytes;
  52. window.formatTime = formatTime;
  53. })();