Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Templater tp.file.creation date producing "1 January 1970" when creation date is not available

Open Xallt opened this issue 1 year ago • 8 comments

Steps to reproduce

  • Create a file in the vault with an unaccesible creation date. I got such a file whlie creating a text file on my HDD https://gitlab.gnome.org/GNOME/nautilus/-/issues/1734. Don't know how to intentionally create such a file. In a different environment
  • Create a simple template
<% tp.file.creation_date("D MMMM YYYY") %>
  • In the file without a creation date, insert this template

Expected result

  • Due to the creation date of the file not being available, the tp.file.creation_date should be replaced with an "ERROR" of some sort
  • A warning should pop up informing that the creation date is not available for some reason

Actual result

The date is replaced with "1 January 1970"

Environment

  • Operating system: Linux Mint

Xallt avatar Oct 22 '22 12:10 Xallt

To fix this we would check for ctime before trying to use it here:

https://github.com/SilentVoid13/Templater/blob/b7781a7ab7757f07cfdfc2c6fcabcc9b94e44a2a/src/core/functions/internal_functions/file/InternalModuleFile.ts#L98

shabegom avatar Oct 29 '22 15:10 shabegom

I have encountered a similar problem. I use this function under the ubuntu system. Created: <% tp.file.creation_date() %> in the template, the actual created file shows created: 1970-01-01 08:00 . It's the first time I use ob through appimage on ubuntu, and I don't know why this phenomenon doesn't happen.

GraceZhuuu avatar Aug 16 '23 09:08 GraceZhuuu

@GraceZhuuu That's a bug with your OS, not with Obsidian or Templater. Obsidian (and therefore, Templater) rely on the OS to provide accurate created/modified dates. Not every OS does this accurately, and when moving files between devices, that metadata is often lost.

I would not recommend relying on the OS for these dates. I would add frontmatter to your notes to set the created/modified dates, so that the data cannot be lost in transit. The Linter plugin can do this for you automatically.

Zachatoo avatar Aug 20 '23 20:08 Zachatoo

I am inclined to call the current behavior correct. If asked to insert a create date when the OS does not provide a valid date, then the expected result should be what we see here for this OS. I wouldn’t want to create our date, and I’d rather we not throw an error or fill it with some placeholder text.

philoserf avatar Oct 05 '23 04:10 philoserf

Yeah, it can also be worked around with a bit of code and conditional logic. I think we should close this as it's not really fixable within the scope of the plugin.

AB1908 avatar Oct 05 '23 14:10 AB1908

It would be a great help if at least something happened when the date isn't available, because it took me quite some time to figure out why my notes were suddenly from 1970, and I'm confident I am not the only one who was confused about it Pretty sure Obsidian is capable of some kind of warnings

Xallt avatar Oct 05 '23 16:10 Xallt

True, Obsidian is capable of warning users but it chooses not to, it only exposes the information returned by the OS. I think we can try looking into some additional logic to notify the users if the creation date is not found/looks erroneous so that users realise they may need to fill in the date by hand/add additional logic to their script to handle this edge case.

AB1908 avatar Oct 05 '23 20:10 AB1908

@GraceZhuuu That's a bug with your OS, not with Obsidian or Templater. Obsidian (and therefore, Templater) rely on the OS to provide accurate created/modified dates. Not every OS does this accurately, and when moving files between devices, that metadata is often lost.

I would not recommend relying on the OS for these dates. I would add frontmatter to your notes to set the created/modified dates, so that the data cannot be lost in transit. The Linter plugin can do this for you automatically.

I didn't want to duplicate the linter functionality but did want to trigger the same actions when my template was executed. You can trigger Linter by placing this in your templater code:

app.commands.executeCommandById("obsidian-linter:lint-file");

danteali avatar Dec 18 '23 23:12 danteali