Juan Ramos
Juan Ramos

Reputation: 189

How to access values from object

I don't know ho to even ask but essentially I want to use the values in this output from a datetime object.

time.struct_time(tm_year=2019, tm_mon=5, tm_mday=12, tm_hour=13, tm_min=30, tm_sec=19, tm_wday=6, tm_yday=132, tm_isdst=1)

and compare them with an if statement using (y,d,h).

I lack the vocabulary to look for it. I don't know for certain what the first output is called is an object tuple etc. Some clarification or a link to the documentation might be nice.

Upvotes: 1

Views: 283

Answers (2)

Allan
Allan

Reputation: 515

You can represent the parts you want as a tuple, and then compare these tuples:

import time

t1 = time.localtime()
time.sleep(1)
t2 = time.localtime()

a = t1.tm_year, t1.tm_mday, t1.tm_hour
b = t2.tm_year, t2.tm_mday, t2.tm_hour

print("a={}, b={}".format(a, b))
if a == b:
    print("They are the same.")
else:
    print("Different year, day or hour.")

Obviously, the example above will only print that they are different if you time that time.sleep(1) to run exactly across a boundary between hours, days or years. I just did that for demontration purposes.

Upvotes: 0

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

     import time
     seconds = time.time();
     result = time.ctime(seconds);

     if(result.tm_year==y &&result.tm_mday==d &&result.tm_hour==h)
     {
     }

Something like this?

Upvotes: 2

Related Questions