Python: Print structure time from given number of seconds

Python Operating System Services: Exercise-25 with Solution

Write a Python program that takes a given number of seconds and pass since epoch as an argument. Print structure time in local time.

Sample Solution:

Python Code :

import time
result = time.localtime(414538698)
print("\nResult:", result)
print("\nYear:", result.tm_year)

Sample Output:

Result: time.struct_time(tm_year=1983, tm_mon=2, tm_mday=19, tm_hour=21, tm_min=38, tm_sec=18, tm_wday=5, tm_yday=50, tm_isdst=0)

Year: 1983

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print simple format of time, full names and the representation format and preferred date time format.
Next: Write a Python program that takes a tuple containing 9 elements corresponding to structure time as an argument and returns a string representing it.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Python: Cache results with decorators

There is a great way to cache functions with decorators in Python. Caching will help save time and precious resources when there is an expensive function at hand.

Implementation is easy, just import lru_cache from functools library and decorate your function using @lru_cache.

from functools import lru_cache

def fibo(a):
    if a <= 1:
        return a
        return fibo(a-1) + fibo(a-2)

for i in range(20):
    print(fibo(i), end="|")

print("\n\n", fibo.cache_info())



 CacheInfo(hits=36, misses=20, maxsize=None, currsize=20)