djherbis / times

#golang file times (atime, mtime, ctime, btime)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any consideration to adding time updates to times package?

jeffaco opened this issue · comments

I have a need to update times on a file when I run and produce a log file. That is:

  • Look up a time,
  • Modify it in some way (compress it like logrotate might do), and
  • Change compressed file time to show original time

Basically, I want to be able to rotate old logs but preserve their time. While times allows me to look up the times, it does not allow me to update the times. Has this been considered?

Which times do you want to change? Which OS?
os.Chtimes already handles atime, mtime fairly nicely cross-platform.

I have started working on another lib (djherbis/utimes, unpublished), which supports updating each time individually, and adds birthtime support (where possible). But I haven't worked out support for many OS yet.

It also seems to me like most OS prevent ChangeTime modification.

I didn't realize that os.Chtimes existed. Let me take a look at that, thanks!

If I call that, and the platform doesn't support that, is it ignored, or should I squash the error?

Probably always best to check the error, but I believe it's supported across all the major platforms.

I'm sorry, I missed that os.Chtimes was in os (I thought it was in your package when I was rushing out this morning). Since this is a system-supplied function, I fully expect that it's portable across all platforms, and I should of course check for errors.

I find it rather surprising that go offers os.Chtimes without offering a mechanism to read times of a file in a portable manner. That seems like an obvious oversight.

In any case, using times to read the times and os.Chtimes to set the times worked perfectly for me. Thanks so much for your suggestion!

No problem! Happy to help.

This issue seems to refer to that very problem:
golang/go#3952 (comment)