4 Replies - 666 Views - Last Post: 03 December 2013 - 06:50 AM Rate Topic: -----

#1 Inuinu172   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

[problem] Compare datetimes and print seconds and minutes serperate

Posted 03 December 2013 - 01:12 AM

What im trying to do:

Compare two datetime objects and print the results in minutes and seconds separately.

My code:
  def onJoin(self, room, user): #called when user joins the room
    user.entryTime = datetime.datetime.now()
    print("[i] %s joined the room!" % user.name)
  
  def onLeave(self, room, user):  #called when the user leaves the room
    try:
      user.exitTime = datetime.datetime.now()
      user.spentTime = user.exitTime - user.entryTime
      time = divmod(user.spentTime.days * 86400 + user.spentTime.seconds, 60)
      print('[i] %s spent %s in total.' % (user.name, time))
    except:
      print('[i] Where the fuck did %s come from? O.o' % user.name)  #if the user leaves without joining first.



And this is the result:

[i] tryhardhusky joined the room!
[i] tryhardhusky spent (0, 3) in total.



The first number is minutes, and the second is seconds.
This works fine, But would like the minutes and seconds to print separate.
Like this:
[i] tryhardhusky joined the room!
[i] tryhardhusky spent 0 minutes and 3 seconds in total.



I'm rather new to the datetime module, and really dont know how I would get this result :/
If anyone could help, It would be much appreciated :)

Thanks in advance ^^/>

This post has been edited by Inuinu172: 03 December 2013 - 01:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [problem] Compare datetimes and print seconds and minutes serperate

#2 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: [problem] Compare datetimes and print seconds and minutes serperate

Posted 03 December 2013 - 04:10 AM

This seems like more of a string formatting question than a datetime question, but this perhaps:
minutes,seconds = divmod(user.spentTime.total_seconds(), 60.0)
template = "[i] {} spent {:.0f} minutes and {:.2f} seconds in total."
print(template.format(user.name,minutes,seconds))

-Mek
Was This Post Helpful? 1
  • +
  • -

#3 Inuinu172   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [problem] Compare datetimes and print seconds and minutes serperate

Posted 03 December 2013 - 04:36 AM

View PostMekire, on 03 December 2013 - 04:10 AM, said:

This seems like more of a string formatting question than a datetime question, but this perhaps:
minutes,seconds = divmod(user.spentTime.total_seconds(), 60.0)
template = "[i] {} spent {:.0f} minutes and {:.2f} seconds in total."
print(template.format(user.name,minutes,seconds))

-Mek


Thanks Mekire!
But I dont exactly understand how it works, Could you please explain a little detail?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: [problem] Compare datetimes and print seconds and minutes serperate

Posted 03 December 2013 - 06:48 AM

Which part?

The divmod returns a 2-tuple so since what we really want is minutes and seconds, there's no need to put it in a single variable.

The str.format method finds "{}" in your string and replaces them with provided arguments. Various things can be placed within the brackets to control how the result is displayed.

See here for details:
format-specification-mini-language

And here for examples:
format-examples

-Mek
Was This Post Helpful? 1
  • +
  • -

#5 Inuinu172   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [problem] Compare datetimes and print seconds and minutes serperate

Posted 03 December 2013 - 06:50 AM

View PostMekire, on 03 December 2013 - 06:48 AM, said:

Which part?

The divmod returns a 2-tuple so since what we really want is minutes and seconds, there's no need to put it in a single variable.

The str.format method finds "{}" in your string and replaces them with provided arguments. Various things can be placed within the brackets to control how the result is displayed.

See here for details:
format-specification-mini-language

And here for examples:
format-examples

-Mek



Ooh, That makes alot more sense :)
Thanks again Mek ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1