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

  • SEARCH
  • Home
  • 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 365205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:30:53+00:00 2026-05-12T13:30:53+00:00

I’ve been writing a time converter to take the systems time_t and convert it

  • 0

I’ve been writing a time converter to take the systems time_t and convert it into human readable date/time. Oh, and this is my second python script ever. We’ll leave that fact aside and move on.

The full script is hosted here.

Writing converters for the year and month were fairly easy, but I’ve hit a serious brick wall trying to get the day working. As you can see, I’ve been trying to brute-force my way all the way from 1970 to today. Unfortunately, the day comes out as -105.

Does anyone know of a better way to do it, or a way to fix up what I have attempted here? It’s currently 3:30 AM, so it’s quite possible I’m missing something obvious.

Sorry, I forgot to note that I’m doing this manually in order to learn python. Doing it via date functions defeats the purpose, unfortunately.

  • 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-12T13:30:54+00:00Added an answer on May 12, 2026 at 1:30 pm

    (I’m assuming you do this to learn Python, so I’ll point out the errors in your code).

    >>> years = SecondsSinceEpoch / 31540000
    

    Nonononono. You can’t do that. Some years have 31536000 seconds, others have 31622400 seconds.

    >>> if calendar.isleap(yeariterator) == True:
    

    You don’t need to test if a true value is true. 🙂 Do:

    >>> if calendar.isleap(yeariterator):
    

    Instead.

    Also change:

    >>> yeariterator = 1969
    >>> iterator = 0
    >>> while yeariterator < yearsfordayfunction:
    >>>     yeariterator = yeariterator + 1
    

    To:

    for yeariterator in range(1970, yearsfordayfunction):

    That will also fix your error: You don’t stop until AFTER 2009, so you get the answer -105, because there is 105 days left of the year.

    And also, there’s not much point in calculating month by month. Year by year works fine.

        for yeariterator in range(1970, yearsfordayfunction):
                if calendar.isleap(yeariterator) == True:
                        days = days - 366
                else:
                        days = days - 365
    

    And an indent of 8 spaces is a lot. 4 is more common.

    Also, I’d calculate year and day of year in one method, instead of doing it twice.

    def YearDay():
        SecondsSinceEpoch = int(time.time())
        days = SecondsSinceEpoch // 86400 # Double slash means floored int.
        year = 1970
        while True:
                if calendar.isleap(year):
                        days -= 366
                else:
                        days -= 365
                year += 1
                if calendar.isleap(year):
                        if days <= 366:
                                return year, days
                else:
                        if days <= 365:
                                return year, days
    
    
    def MonthDay(year, day):
        if calendar.isleap(year):
                monthstarts = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
        else:
                monthstarts = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
    
        month = 0
        for start in monthstarts:
                if start > day:
                        return month, day - monthstarts[month-1] + 1
                month += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.