Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:04:26+00:00 2026-05-13T15:04:26+00:00

How does time.localtime() work exactly? I can call up the array (tupple, I think

  • 0

How does time.localtime() work exactly? I can call up the “array” (tupple, I think it is called – because it is immutable?) and reference/index components of it. For example:

>>> time.localtime()[0]
2010

But if I do:

print time.localtime()
time.struct_time(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46, tm_sec=58, tm_wday=6, tm_yday=38, tm_isdst=0)

First: How does time.localtime() know to return time.struct_time()?

Second: If I type print time.struct_time() it wants additional values to be passed to it (thus not giving me the same values it returned from time.localtime()) – how do I know what those values could possibly be? I checked the python documentation and I couldn’t make ‘heads nor tails’ of it. Really just looking for an example here…

Third: When I index the tupple/array for time.localtime() it returns the proper associated value, “2010” for example, rather than “tm_year=2010” – how does it know to do this (generally speaking not asking for a lot here).

Forth: If I wanted to “call” tm_year from time.localtime() how can I do this? What I am doing (and don’t ‘feel’ right about) is the following:

tm_year = str(time.localtime()[0])
tm_mon = str(time.localtime()[1])
tm_mday = str(time.localtime()[2])
tm_hour = str(time.localtime()[3])
tm_min = str(time.localtime()[4])

NOTE: I am saving them as strings for other reasons not explained in this question, just wanted to point out that I am creating my own variables (named exactly the same as they are in the tupple) and then just referencing the index value associated with the value I want

Is there a way to to just call time.localtime(tm_year) (I know that doesn’t work as is, but just brainstorming…)

Thanks in advance… (and I have read “http://docs.python.org/library/time.html” but I am sure I missed some important information… any advice?)

-J

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:04:26+00:00Added an answer on May 13, 2026 at 3:04 pm

    time.localtime()[0] calls __gettiem__() on the time.struct_time instance. This is how it gets this list/tuple like behaviour from. Here is a really simple example

    >>> class MyTime(object):
    ...     def __init__(self, year, month, mday, hour, minute):
    ...         self.data =  year, month, mday, hour, minute
    ...     def __getitem__(self, idx):
    ...         return self.data[idx]
    ...     def __str__(self):
    ...         return "MyTime(tm_year=%s, tm_mon=%s, tm_mday=%s, tm_hour=%s, tm_min=%s)"%self.data
    ... 
    >>> x=MyTime(2010,2,7,14,46)
    >>> x[0]
    2010
    >>> x[1]
    2
    >>> print x
    MyTime(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46)
    

    time.struct_time is a class. time.localtime() returns an instance of the class, which explains the difference when you try to print them

    You can load up your 5 variables like this

    >>> tm_year,tm_mon,tm_mday,tm_hour,tm_min=map(str,time.localtime()[:5])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 287k
  • Answers 287k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There isn't a built-in class just like HashSet<T> except for… May 13, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer In your view, do the following : <%= select_tag 'countries',… May 13, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer You have to run the development server such that it… May 13, 2026 at 5:05 pm

Related Questions

SQLAlchemy's DateTime type allows for a timezone=True argument to save a non-naive datetime object
We are working with large amounts of data, all tagged in UTC (in Java).
I need to add timezone information to a db table with user maintained locations.
How might I get the current time in milliseconds in C? I am doing
I have just, in my groggy morning state, reversed & confused the arguments to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.