My simple question being where is hour, minute and second saved?


#1

from datetime import datetime
now = datetime.now()

print '%s:%s:%s' % (now.hour, now.minute, now.second)


#2

Hello, @anusan1293.
What do you mean by "saved"?
Is something like how does the program knows the exact value of hour,minute and second at the time we call it?


#3

yeah kind of. What I meant was how does the program know that I asked for the hour minute and so by just typing now.hour


#4

@anusan1293: Got it!
Well, at the first line of your code you're asking to get something from the module datetime which in this case is datetime.
We're doing this because such module has classes for manipulating dates and times in both simple and complex ways

Q: Okay but how does Python know the exact hour,minute and second?
A: "Objects of the datetime type represent a date and a time in some timezone. Unless otherwise specified, datetime objects use “naive time”, which means that it’s up to the application to keep track of what time zone they’re in." - source

In other words, it's up for the code, that was created to get each value correctly, to store the values into:

hour, minute, second, year, month and day

So you can simply "call" them by using datetime.hour, for example.
You'll see in later lessons about OOP and you'll understand it better!

Here are some good links for you to check:
1. 8.1. datetime — Basic date and time types,
2. Python: Get Today’s Current Date and Time,
3. The datetime Module (got the explanation from here),
4. 13 useful tips about Python datetime objects.

Questions like this are really good! It makes you think how it actually works, instead of just knowing such feature exists!

If I wasn't clear, let me know and I'll find another way to explain it.


#5

thanks man. It was really helpful


#6

No problem! Glad to help!