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

BUG: duration filter produces wrong result #251

Open
jldiaz opened this issue Dec 11, 2024 · 1 comment
Open

BUG: duration filter produces wrong result #251

jldiaz opened this issue Dec 11, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jldiaz
Copy link

jldiaz commented Dec 11, 2024

Version:

  • OS: Any? (tested on Windows)
  • Browser: Any? (tested on Firefox)
  • Web Clipper version: 0.10.5
  • Obsidian version: Not relevant

Describe the bug

The recently added duration filter produces wrong results. For example, if the input is "PT9099S", instead of giving the result "2:31:39", it produces "2:00:9099"

In all my tests, the "minutes" part is 00, and the "seconds" part contains the total number of seconds.

URLs where the bug occurs

Tested with youtube videos

To reproduce

Steps to reproduce the behavior:

Use the test template provided below, and:

  1. Go to https://www.youtube.com/watch?v=MyR6R55lGR (a video with a duration of 9099 seconds)
  2. Click on Obsidian Web Clipper
  3. See error

image

Test template

{
  "schemaVersion": "0.1.0",
  "name": "Test template",
  "behavior": "create",
  "noteContentFormat": "Origina valuel: {{schema:@VideoObject:duration}}\nFiltered result:  {{schema:@VideoObject:duration|duration}}\nOther variant:  {{schema:@VideoObject:duration|duration:\"mm:ss\"}}\n",
  "properties": [],
  "triggers": [
    "https://www.youtube.com"
  ],
  "noteNameFormat": "{{title}}",
  "path": "Clippings"
}
@jldiaz jldiaz added the bug Something isn't working label Dec 11, 2024
@jldiaz
Copy link
Author

jldiaz commented Dec 11, 2024

I think the bug is in dayjs.duration. If not a bug, at least a confusing and poorly documented behaviour.

See https://day.js.org/docs/en/durations/durations

Given the string "PT9099S", which represents 9099 seconds (i.e.: 2 hours, 31 minutes and 39 seconds), we could think that we can pass the string to dayjs.duration() (it understands ISO format) and then ask for the hours, minutes and seconds parts. However this produces puzzling results

>> let test = dayjs.duration("PT9099S")
>> test.hours()
0
>> test.minutes()
0
>> test.seconds()
9099
>> test.format("HH:mm:ss")
"00:00:9099" 

The same happens if we initialize the duration using a json with fiedls for years, months, days, hours, minutes and seconds, and put 9099 in seconds and 0 in the others.

If, however, we create the duration object passing it an integer that will be interpreted as a number of miliseconds, then it works as expected:

>> let test = dayjs.duration(9099000)
>> test.hours()
2
>> test.minutes()
31
>> test.seconds()
39
>> test.format("HH:mm:ss")
"02:31:39" 

This suggests the following workaround:

>> let test = dayjs.duration(dayjs.duration("PT9099S").asMilliseconds())
>> test.format("HH:mm:ss")
"02:31:39" 

Another surprising workaround is this:

>> let test = dayjs.duration("PT9099S").add(0)
>> test.format("HH:mm:ss")
"02:31:39" 

Apparently, .add() forces a recomputation of the internal representation of the duration in separate fields (year, month, ..., minutes, seconds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant