0

I am using a Python library gitlab to get last commits for the current day. The main purpose is to find out time diff between last and previous commits for my time tracker.

It works in this way however when the very first commit does not exist in this day I am getting, for example, 10:00 hour as a base (starting work) And here is my problem, Gitlab time and my pc time is not the same and it gives me incorrect diff.

I would like to know whether there is a way to get current DateTime from Gitlab to compare my pc time and find out a real diff.

Here is an example:


def oneDayCommitsBy(user, project, cDate = datetime.datetime.now()):
    untilDt = cDate + datetime.timedelta(days=1)
    strcDate = "%s-%s-%s" % (cDate.year, cDate.month, cDate.day)
    strUntilDt = "%s-%s-%s" % (untilDt.year, untilDt.month, untilDt.day)
    cDayCommits = project.commits.list(ref_name="master", since=strcDate, until=strUntilDt)
    print 'commmits [' + strcDate + ', ' + strUntilDt + ']' 
    for c in cDayCommits:
        if c.author_name == user:
            print c
            print '\n'
            ctime = datetime.datetime.now()
            commitTm = datetime.datetime.strptime(str(c.created_at),"%Y-%m-%dT%H:%M:%S.%fZ")
            diff = ctime - commitTm
            print diff.total_seconds()/3600
            print '\r'


# private token or personal token authentication
gl = gitlab.Gitlab(HOST, private_token=TOKEN, api_version=4)
gl.auth()
project = gl.projects.get(REPO)
oneDayCommitsBy('harry', project)

What it does is getting commits and comparing created_at time to local time and calculates diff in an hour but the difference is an incorrect cause in my pc time is different and in gitlab is different too.

n1md7
  • 2,854
  • 1
  • 12
  • 27
  • Can you paste a copy of your output? – Yaakov Bressler Jul 26 '19 at 12:37
  • @YaakovBressler Hi, I have updated my question – n1md7 Jul 26 '19 at 13:26
  • 1
    Most webservers provide a timestamp in the response headers for the current request (Date header). You can use that to check your server's time against your current PC time. Or get NTP working so everyone is synchronized properly. – patthoyts Jul 26 '19 at 13:30
  • @patthoyts Sounds logical to me but I am not sending request myself, Gitlab library does it for me. Do you know how can I get header data? I checked [gitlab-docs](https://python-gitlab.readthedocs.io/en/stable/api-objects.html) cannot find anything similar :/ If I send separate request using Urllib then I can get header data but I would like to find out solution from Gitlab lib. – n1md7 Jul 26 '19 at 13:53
  • I believe you are using local time, while you should use UTC. See https://stackoverflow.com/questions/15940280/how-to-get-utc-time-in-python – Iron Bishop Jul 26 '19 at 14:38
  • 1
    Thanks for your response but that's, not an issue. The only solution I have right now is to send one extra request and get server time, separated from Gitlab req, parse it and compare diff to use later. I did in this way and it worked. The reason why I asked the question was to find out whether there was an option from the Gitlab library to solve this problem without extra interventions. – n1md7 Jul 26 '19 at 14:57

0 Answers0