Skip to content

Commit

Permalink
Fixes AuHau#16 - time entry with start time ignored
Browse files Browse the repository at this point in the history
1) Switched from the start endoing time_entries/start to the create end point time_entries. Start always used the current time and the create endpoint needs a start time specified.
2) This meant that the create_time_entry code needed to create a current time in the localized timezone as well as,
3) The parsed time argument to be put into a localized time string as well.

Much thanks to Margus at Toggl for pointing out the differences between the end points to me.
  • Loading branch information
beauraines committed Oct 14, 2014
1 parent 94ac3bc commit b6e9da0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions toggl.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ def create_time_entry_json(description, project_name=None, duration=0):
# epoch.
if duration == 0:
duration = 0-time.time()

tz = pytz.timezone(toggl_cfg.get('options', 'timezone'))

# Create JSON object to send to toggl.
data = { 'time_entry' : \
{ 'duration' : duration,
{ 'duration' : duration,
'billable' : False,
'start' : datetime.datetime.utcnow().isoformat(),
'start' : tz.localize(datetime.datetime.now()).isoformat(),
'description' : description,
'created_with' : 'toggl-cli',
'ignore_start_and_stop' : options.ignore_start_and_stop
}
}
if project_id != None:
Expand Down Expand Up @@ -397,14 +398,16 @@ def start_time_entry(args):

if len(args) == 1:
tz = pytz.timezone(toggl_cfg.get('options', 'timezone'))
st = tz.localize(parse(args[0]))
data['time_entry']['start'] = st.astimezone(pytz.utc).isoformat()
dt = parse(args[0])
st = tz.localize(dt)
data['time_entry']['start'] = st.isoformat()
data['time_entry']['duration'] = 0 - int(dt.strftime('%s'))

if options.verbose:
print json.dumps(data)

headers = {'content-type': 'application/json'}
r = requests.post("%s/time_entries/start" % TOGGL_URL, auth=AUTH,
r = requests.post("%s/time_entries" % TOGGL_URL, auth=AUTH,
data=json.dumps(data), headers=headers)
r.raise_for_status() # raise exception on error

Expand Down

0 comments on commit b6e9da0

Please sign in to comment.