novoid/date2name

--ctime uses current date on macOS

garethrees opened this issue · 6 comments

Here's an example of me trying to add the date to a standard text file:

$ stat -f "%SB" pipeline.sh
Oct 19 20:51:22 2018

$ date2name --ctime pipeline.sh
/pipeline.sh                              →  2019-04-01_pipeline.sh

It looks like date2name can't read the "birth time" correctly on macOS, as it's just prepending today's date to the file.

It does pick up the mtime

$ mv 2019-04-01_pipeline.sh pipeline.sh

$ date2name --mtime pipeline.sh
/pipeline.sh                              →  2018-10-19_pipeline.sh

…but this isn't great, as I want to rename lots of old files that I've modified after the creation date.

I did a little digging, and it looks like you can call os.stat(path).st_birthtime here to get the real created time. I'm not sure whether there's any cross-platform compatibility to worry about with st_birthtime though.

Thanks for the nice tool 😃.

Just made this change locally and seemed to work:

- return time.strftime(formatstring, time.localtime(os.path.getctime(item))) + delimiter_char + item
+ return time.strftime(formatstring, time.localtime(os.stat(item).st_birthtime)) + delimiter_char + item
$ date2name --ctime pipeline.sh
/pipeline.sh                              →  2018-10-19_pipeline.sh

# Revert the change
$ mv 2018-10-19_pipeline.sh pipeline.sh

# Check --mtime still works correctly
$ date2name --mtime pipeline.sh
/pipeline.sh                              →  2019-04-01_pipeline.sh

Hi,
Thanks for reporting.
However, this breaks things in GNU/Linux as st_birthtime is not available here. Therefore, I have to add the exception just for macOS. As I don't have a mac any more and I'm not sure what the platform string is, could you please tell me what is the output of platform.system()? Thanks!

I think this is what you're after?

$ python3
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'

$ python
Python 2.7.16 (default, Mar  4 2019, 09:01:38)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'

Thanks 🙏!

Hi @garethrees,
I pushed the fix to this repo. Could you please test it and confirm that it resolves your issue?

Yep, applied d13b80b locally works great. Thanks!

Perfect!
Have a nice day.