如何查找过去 24 小时的信息

发布于 2024-10-20 15:40:38 字数 807 浏览 0 评论 0原文

我是编程初学者,我想找到最简单的方法来做到这一点。我有一条信息,其中有一行显示它何时以 UTC 时间更新,现在我想编写程序来询问信息是否在过去 24 小时内,而不是打印信息。我正在使用 Python 3,谢谢

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= 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)

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= 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2024-10-27 15:40:38

查看美国地质调查局提供的数据,他们似乎已经向您提供了此信息。例如,在第一个条目中,我看到 ,而最后一个条目是

假设您信任此信息,您可以像提取标题和时间一样提取它:

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

然后您可以根据年龄是否为“过去一天” 来过滤数据。

更新:以下是手动检查日期的方法。

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

请注意 strptime()< /a> 需要时区,因此您应该从 findQuakeTime() 代码中删除 -3

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.

依 靠 2024-10-27 15:40:38

有两种可能的方法:

  • 将信息中的每一行转换为本地时间并与当前时间进行比较。

  • 获取当前 UTC 时间并与信息中的元数据进行比较。在我看来,该解决方案更简洁,并且在显示多个项目时性能会更好,因为只需要一次转换。

除非您提供有关您正在使用的编程语言、平台和任何框架的更多信息,否则无法提供更详细的帮助。

编辑:

现在我们知道您正在使用Python(您还应该提到它是Python-3,而不是当前的稳定版本),并且因为您的 quakeTime 变量似乎采用以下形式 < code>2011 年 3 月 7 日星期二 15:52:08,这是一个小脚本,用于测试给定日期是否在过去 24 小时内:

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]))

以及几个用例:

$ 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文