Transform syscall.Filetime to human-readable date

I’m writing a gallery page for my site. I store the images in static/photography, in my partial for the page I have the following

{{ define "main" }}
{{ $folder := "static/photography"}}
{{ $files := readDir $folder }}
{{ range $idx, $img := $files }}
<div class="gallery">
    <div class="gallery-image">
      <img src="/photography/{{ .Name }}" alt="{{ .Name }}">
{{ end }}
{{ end }}

I’m trying to display the creation date of the file, which is available as $img.CreationTime which returns the time as two 32 bit parts of one 64 bit integer - how do I combine these in order to display them as a proper date? I’ve searched around and can’t find any solutions that work, the ones that I’ve seen use a binary reader but I can’t seem to get one working properly.

1 Like

for Go time

If you have EXIF data … go here

I have looked and played around with the time package but could not figure out how to solve the problem since CreationTime is the 64 bit timestamp split into two 32 bit values which have to ‘glued’ before they can be parsed. Could you give me a tip on how that is supposed to be done?

The way I’ve seen this done is through the use of binary.Reader which is, to my knowledge and some testing, unavailable in Hugo.

What operating system?

Looks like Windows since it’s the only OS with syscall.Filetime defined. Try using Filetime.Nanoseconds():

{{ $img.CreationTime.Nanoseconds | time }}
1 Like

Your OS assumption is correct I am on Windows.
Your solution also works, almost, using {{ $img.CreationTime.Nanoseconds }} &rarr; {{ $img.CreationTime.Nanoseconds | time }} results in 1610919232422978000 → 51048000281-11-05 03:20:00 +0100 CET.

Using to convert 1610919232422978000 to local time gives the correct creation date of the file, but piping it into time seems to not work correctly. I’ve looked around in the time package and can’t find a function which takes nanoseconds and returns a readable date.

I tried to use time.Unix 0 <nanoseconds> but I get ... can't evaluate field Unix in type interface {}

Ideally I’d want to get the ‘Date taken’ property (see image below), since that is somewhat closer to the actual date of when the photo was taken (I take analogue photographs so in this case, Date taken is actually the day the image was developed and scanned, not when it was taken), but I don’t think that’s possible given that I currently store my images inside the static folder.

Here’s a work-around:

{{ $zerotime := time "1970-01-01" }}
{{ duration "nanosecond" $img.CreationTime.Nanoseconds | $zerotime.Add }}

I cleaner solution would be to expose a time.Unix implementation so that you could do:

{{ time.Unix 0 $img.CreationTime.Nanoseconds }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.