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

file_name_format: Unix? #5

Open
INPoppoRTUNE opened this issue Jul 19, 2024 · 6 comments
Open

file_name_format: Unix? #5

INPoppoRTUNE opened this issue Jul 19, 2024 · 6 comments

Comments

@INPoppoRTUNE
Copy link

My files come in the following format: 1721398175-1721398267.mp4, where the two numbers are Unix formatted dates and times, which seems not to be supported by the card.

I can cut the “start” date using file_name_date_begins: 11, but I would need a “Unix” option implemented. I noticed in the docs some datetime difference functionality already implemented with the AGO function, so it seems like the necessary libraries are already in place.

Thanks for your time and the beautiful card.

@lukelalo
Copy link
Owner

lukelalo commented Oct 4, 2024

You need to configure a file sensor, like this

@INPoppoRTUNE
Copy link
Author

You need to configure a file sensor, like this

I may be missing something, but in the attached Git I can't find no reference about the Unix to Human readable format conversion, only sorting(?)

@lukelalo
Copy link
Owner

lukelalo commented Oct 5, 2024

Yes, you are right. But currently I don’t have enough time to develop this feature. Feel free to do a pull request if you know how to develop it.
Regards

@INPoppoRTUNE
Copy link
Author

INPoppoRTUNE commented Oct 5, 2024

Yes, you are right. But currently I don’t have enough time to develop this feature. Feel free to do a pull request if you know how to develop it. Regards

Thanks anyway.
I'm not really familiar with JS, but I'll try to give it a spin.
I'd guess the entire datetime conversion is handled inside of the _createFileResource(fileRawUrl, fileNameFormat, fileNameDateBegins, captionFormat) function in gallery-card.js, is this correct?

@lukelalo
Copy link
Owner

lukelalo commented Oct 5, 2024

Yes, that's the right place

@INPoppoRTUNE
Copy link
Author

Yes, that's the right place

Here's a snippet that convert Unix timestamps to YYYYMMDD-HHmmss (as in your example) through external function:

function processUnixFileName(fileName) {
  const unixPattern = /^[0-9]+-[0-9]+\.mp4$/;
  if (unixPattern.test(fileName)) {
    const timestamp = fileName.split("-")[0];
    const date = new Date(parseInt(timestamp, 10) * 1000); // Convert Unix timestamp to Date object
    const formattedDate = formatDateToYYYYMMDDHHmmss(date);
    return formattedDate;
  } else {
    return fileName; // If not Unix format, return the original fileName
  }
}

function formatDateToYYYYMMDDHHmmss(date) {
  const yyyy = date.getFullYear();
  const mm = String(date.getMonth() + 1).padStart(2, '0');
  const dd = String(date.getDate()).padStart(2, '0');
  const hh = String(date.getHours()).padStart(2, '0');
  const min = String(date.getMinutes()).padStart(2, '0');
  const ss = String(date.getSeconds()).padStart(2, '0');

  return `${yyyy}${mm}${dd}-${hh}${min}${ss}`;
}

// Example usage
const fileName = "1721398175-1721398267.mp4";
const result = processUnixFileName(fileName);
console.log(result); // Outputs in the YYYYMMDD-HHmmss format: 20240719-140935

If I'm correctly reading your code it should be sufficient to add those two functions and call processUnixFileName(fileName) inside _createFileResource(fileRawUrl, fileNameFormat, fileNameDateBegins, captionFormat), as it would run only if the Regex pattern for Unix is detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants