Search code examples
gitdategithubcygwin

How to grab the last 12 months from cygwin "date" with a static time value


I've got this script

"$(date +%Y-%m-01) -$i months" +%Y-%m)-01"

that works great for getting the last 12 months from date, month by month. I want to have a static value for the time being used, so that instead of going back 12 months from the CURRENT TIME it is going back from a static time.

I'm using this with Github and I want to be consistent with the commits I sync to when I want to go back x months. How to specify time down to the second and use it with this script?


Solution

  • You could just use what git proves you: git log --since='X seconds ago', e.g. to list all commits in the last year (approximately 365*24*60*60=31536000 seconds)

    $ git log --since='31536000 seconds ago'
    

    You can give an upper bound with --until.

    If you want to list all commits one year before SOME_DATE you can use date.

    # the current time (NOW)
    date +%s
    
    # the time a SOME_DATE (e.g. 2011-01-01)
    date +%s --date=2011-01-01
    
    # one year before SOME_DATE is
    #
    #   31536000 + NOW - SOME_DATE
    #
    # (use bc for calculations)
    SECONDS=$(echo 31536000 -  $(date +%s) + $(date +%s --date=2011-01-01) | bc)
    
    # putting it all together:
    # display all commits since one year before 2011-01-01 until 2011-01-01
    git log --since="$(echo 31536000 + $(date +%s) - $(date +%s --date=2011-01-01)| bc) seconds ago" --before="$(echo $(date +%s) - $(date +%s --date=2011-01-01) | bc -l) seconds ago"