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 8971395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:56:38+00:00 2026-06-15T17:56:38+00:00

I just started programming with Python, and have some simple questions (probably). What I

  • 0

I just started programming with Python, and have some simple questions (probably). What I would like to do is compare some timestamps to find the closest that isn’t later then now.

Basically what Iam trying to do is getting the current track played on the radio, and they have a feed that show the next 20 or so with time for when the track starts. I want to get whats playing right now!

Here is an example array of strings:

examples = ['2012-12-10 02:06:45', '2012-12-10 02:02:43', '2012-12-10 01:58:53']

Now what I would like to do is compare the time closest to now (but not later) to see whats currently playing.

This is my script so far:

import datetime, itertools, time
currentTimeMachine = datetime.datetime.now()
now = currentTimeMachine.strftime("%Y-%m-%d %H:%M:%S")

examples = ['2012-12-10 02:06:45', '2012-12-10 02:02:43', '2012-12-10 01:58:53']
tmsg = examples.strftime('%d%b%Y')


 print [x for x in itertools.takewhile( lambda t: now > datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S"), examples )][-1]

The last bit there I picked up somwhere else, but I cant seem to get it to work.

Any help would be greatly appreciated!

  • 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-06-15T17:56:39+00:00Added an answer on June 15, 2026 at 5:56 pm

    The other answers have fixed your errors, so your algorithm now runs properly.

    But the algorithm itself is wrong. You want to get the closest to the present without going over. But what you’ve written is:

     [x for x in itertools.takewhile(pred, examples)][-1]
    

    Think about what this means. First, takewhile will return examples until one of them fails the predicate. Then you’re taking the last one that succeeded. So, if your examples looked like this:

    [now-3, now-10, now-5, now+3, now-9, now-1, now+9]
    

    First, takewhile will yield now-3, now-10, now-5 and then stop because pred(now+3) returns False. Then, you take the last one, now-5.

    This would work if you sorted the examples in ascending order:

    [now-10, now-9, now-5, now-3, now-1, now+3, now+9]
    

    Now takewhile will yield everything up to now-1, so the last thing it yields is the one you want.

    But the example in your initial question were in descending order, and in the comment to Anthony Kong’s answer, you added some more that aren’t in any order at all. So, you obviously can’t rely on them being in sorted order. So, one possible fix is to sort them:

    >>> import datetime, itertools, time
    >>> currentTimeMachine = datetime.datetime.now()
    >>> print [x for x in itertools.takewhile(lambda t: currentTimeMachine  > datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S"), sorted(examples))][-1]
    2012-12-10 02:06:45
    

    Or, to make things a bit more readable, break up that last line, and get rid of the extraneous list comprehension:

    >>> exampleDates = [datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S") for t in examples]
    >>> def beforeNow(t):
    ...     return currentTimeMachine > t
    >>> print list(itertools.takewhile(beforeNow, sorted(exampleDates))[-1]
    

    However, this is kind of a silly way to do things. What you really want is the maximum value in examples that isn’t after the present. So just translate that English sentence into code:

    >>> print max(x for x in exampleDates if x <= currentTimeMachine)
    

    Let’s put it all together:

    >>> examples = ['2012-12-10 02:06:45', '2012-12-10 02:02:43', '2012-12-10 01:58:53']
    >>> exampleDates = (datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S") for t in examples)
    >>> currentTimeMachine = datetime.datetime.now()
    >>> print max(t for t in exampleDates if t <= currentTimeMachine)
    2012-12-10 02:06:45
    

    I used a generator expression rather than a list for exampleDates because you don’t actually need the list for anything you just need to iterate over it once. If you want to keep it around for inspection or repeated use, change the parens to square brackets.

    Also, I changed the < to <=, because you said “isn’t later then now” rather than “is earlier than now” (in other words, now should count).

    As a side note, because you happen to have ISO-esque timestamps, you actually can sort them as strings:

    >>> now = datetime.datetime.now()
    >>> currentTimeMachine = datetime.datetime.strftime(now, "%Y-%m-%d %H:%M:%S")
    >>> print max(t for t in examples if t <= currentTimeMachine)
    2012-12-10 02:06:45
    

    There’s no good reason to do things this way, and it will invariably lead you to bugs when you get timestamps in slightly different formats (e.g., '2012-12-10 02:06:45' compares before '2012-12-10Z01:06:45'), but it isn’t actually a problem with your original code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing python programming for my project and I have just started.
Just started learning python (3.2) and have a question. I have created a some
I just started learning my first real programming language, Python. I'd like to know
I've just started programming, therefore I'm kinda noob. I'm trying to use python to
I just started programming in Basic4android. I have created 2 layouts. splash and search
I have just started programming and have made a few small applications in C
I've just started programming in Qt framework. Following is a very simple program: #include
How would you explain to someone who has just started programming in Java, what
So I'm really new to programming, I just started learning Python yesterday and I'm
I've just started learning D Programming. I'd like to deploy my programs on an

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.