开发者

How to find information from the last 24 hours

I'm a beginner in programming and I want to find the easiest way of doing this. I have information which has a line in it that shows when it was updated in UTC time, now i want to write my program to ask if info is within the last 24 hours than print the info. I'm working in Python 3, Thanks

import datetime
import urllib.request
def findEarthquake(entry):
    start= entry.find("<titile>") +7
    end= entry.find("</title>")
    eq= entry[start:end]
    return eq
def findQuakeTime(entry):
    start=开发者_StackOverflow社区 entry.find("<p>") +3
    end= entry.find("<br>") -3
    time=entry[start:end]
    return time 
page= urllib.request.urlopen("http://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml")
text= page.read().decode("utf8")
start= text.find("<entry>") +7
earthquakeList=[]

while start >= 0:
    end= text.find("</entry>", start)
    entry= text[start:end]
    quake= findEarthquake(entry)
    quakeTime= findQuakeTime(entry)


Looking at the data being provided by USGS, it seems they already provide this info to you. For example, in the first entry I see <category label="Age" term="Past day"/>, while the last is <category label="Age" term="Past week"/>.

Assuming you trust this information, you can extract it just as you do the title and time:

def findQuakeAge(entry):
    start = entry.find('<category label="Age" term="') + 28
    end = entry.find('"/>', start)
    age = entry[start:end]
    return age

Then you can filter your data based on whether the age is "Past day" or not.

Update: And here's how to check the dates manually.

from datetime import datetime, timedelta

quakeTime = datetime.strptime(quakeTime, "%A, %B %d, %Y %H:%M:%S %Z")

if datetime.now() - quakeTime < timedelta(days=1):
    # quake was less than a day ago

Note that strptime() wants the time zone, so you should remove the -3 from your findQuakeTime() code.


There are two possible approaches:

  • Convert each line in your information to your local time and compare with the current time.

  • Get the current time in UTC and compare with the metadata in your information. In my opinion this solutions is cleaner and it will perform better when showing multiple items since only one conversion is necessary.

Unless you supply more information on your programming language, platform and any frameworks that you are using, there is no way to provide more detailed help.

EDIT:

Now that we know that your are using Python (and you should also mention that it is Python-3, rather than the current stable version) and since your quakeTime variable seems to be in the form Tuesday, March 7, 2011 15:52:08, here's a small script that tests if a given date is within the last 24 hours or not:

import calendar
import sys
import time

def withinLast24Hours(d):
        current = time.time();

        limit = current - 24 * 3600;

        argument = calendar.timegm(time.strptime(d, '%A, %B %d, %Y %H:%M:%S'))

        if (argument > limit):
                return True
        else:
                return False

print(withinLast24Hours(sys.argv[1]))

And a couple use cases:

$ date -u
Tue Mar  8 17:56:42 UTC 2011
$ python3 timetest.py 'Tuesday, March  7, 2011 17:56:08'
False
$ python3 timetest.py 'Tuesday, March  7, 2011 17:57:08'
True
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜