本文的目的是介绍如何检查当前时间是否在python中?的详细情况,特别关注判断当前时间是否在某一时间段的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解如何检查当前时间
本文的目的是介绍如何检查当前时间是否在python中?的详细情况,特别关注判断当前时间是否在某一时间段的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解如何检查当前时间是否在python中?的机会,同时也不会遗漏关于java-如何检查当前时间是否在android中的预设时间范围之间、python pytz检查时间是否在悉尼/墨尔本时间而不是UTC时间、Python如何检查文件名是否在UTF8中?、ruby-on-rails – 检查当前时间是否在晚上10:27之前的知识。
本文目录一览:- 如何检查当前时间是否在python中?(判断当前时间是否在某一时间段)
- java-如何检查当前时间是否在android中的预设时间范围之间
- python pytz检查时间是否在悉尼/墨尔本时间而不是UTC时间
- Python如何检查文件名是否在UTF8中?
- ruby-on-rails – 检查当前时间是否在晚上10:27之前
如何检查当前时间是否在python中?(判断当前时间是否在某一时间段)
我需要检查当前时间是否在时间范围内。最简单的情况是time_end> time_start:
if time(6,0) <= now.time() <= time(12,00): print ''1''
但是,当用户输入结束时间小于开始时间的时间范围时,麻烦就开始了,例如“ 23:00-06:00”。像“
00:00”这样的时间将在此范围内。大约5年前,我编写了以下PHP函数:
function checkInterval($start, $end) { $dt = date("H:i:s"); $tstart = explode(":", $start); $tend = explode(":", $end); $tnow = explode(":", $dt); if (!$tstart[2]) $tstart[2] = 0; if (!$tend[2]) $tend[2] = 0; $tstart = $tstart[0]*60*60 + $tstart[1]*60 + $tstart[2]; $tend = $tend[0]*60*60 + $tend[1]*60 + $tend[2]; $tnow = $tnow[0]*60*60 + $tnow[1]*60 + $tnow[2]; if ($tend < $tstart) { if ($tend - $tnow > 0 && $tnow > $tstart) return true; else if ($tnow - $tstart > 0 && $tnow > $tend) return true; else if ($tend > $tnow && $tend < $tstart && $tstart > $tnow) return true; else return false; } else { if ($tstart < $tnow && $tend > $tnow) return true; else return false; }
现在我需要做同样的事情,但是我想让它看起来好看。因此,我应该使用哪种算法来确定当前时间‘00:00’是否在 反向 范围内[''23:00'',''01:00'']
?
答案1
小编典典Python解决方案要短得多。
def time_in_range(start, end, x): """Return true if x is in the range [start, end]""" if start <= end: return start <= x <= end else: return start <= x or x <= end
使用datetime.time
类start
,end
和x
。
>>> import datetime>>> start = datetime.time(23, 0, 0)>>> end = datetime.time(1, 0, 0)>>> time_in_range(start, end, datetime.time(23, 30, 0))True>>> time_in_range(start, end, datetime.time(12, 30, 0))False
java-如何检查当前时间是否在android中的预设时间范围之间
Hai,我现在有一个问题,我想检查当前时间(10:00 am)是否在预设时间之间
框说(10:00 pm-4:00 am)我该怎么做.
我尝试了一些不总是满足条件的事情
public class Test {
static int flag;
public static void main(String[] args) {
// Todo Auto-generated method stub
// final Timer timer = new Timer();
// Timer timer2 = new Timer();
SimpleDateFormat parser = new SimpleDateFormat("HH.mm");
Date start = null;
Date end = null;
try {
start = parser.parse("22.00");
} catch (ParseException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
}
try {
end = parser.parse("8.00");
} catch (ParseException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
}
try {
Date userDate = parser.parse("23.06");
if (userDate.after(start) && userDate.before(end)) {
System.out.println("innnnnnnnnnnn");
} else {
System.out.println("outttttttttt");
}
} catch (ParseException e) {
// Invalid date was entered
}
}
}
解决方法:
避免约会
通常,众所周知,与Java捆绑在一起的java.util.Date和.Calendar和SimpleDateFormat类非常麻烦,应避免使用.专门针对您的需求,它们结合了日期和时间,而您似乎只需要时间.
请改用一个不错的日期时间库.
>对于Java,这意味着:
> Joda-Time
> java.time package,新于Java 8.
>对于Android,则表示Joda-Time.
当地时间
Joda-Time(和java.time)提供LocalTime类来表示没有任何时区的时间.获取当前时间时,应传递一个DateTimeZone来将计算机/ JVM的日期时间调整为所需的时区,而不是默认的时区.
半开
通常在日期时间工作中,比较时间跨度的最佳方法是使用“半开式”方法.半开时,开头是包容性的,结尾是唯一的.注意在此示例代码中使用Joda-Time 2.3进行的比较.
Joda-Time示例
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
LocalTime Now = LocalTime.Now( timeZone ); // Adjust computer/JVM time zone's current time to desired time zone's current time.
LocalTime start = new LocalTime( "10:00:00" );
LocalTime stop = new LocalTime( "16:00:00" );
boolean isNotBeforeStart = ( ! Now.isBefore( start ) ); // Is Now equal to or after start?
boolean isBeforeEnd = Now.isBefore( stop ); // Is Now any time up to, but not including, stop?
boolean isWithinRange = ( isNotBeforeStart && isBeforeEnd );
python pytz检查时间是否在悉尼/墨尔本时间而不是UTC时间
我发现如果你使用这个 strftime() 你可以使用 pytz 进行转换,虽然你必须让你正在检查的东西也是字符串以便它可以比较,否则你正在比较 str 和 datetime.time,并且这是一个错误
def is_time_between(begin_time,end_time,check_time=None):
# If check time is not given,default to current UTC time
tz_Aus = pytz.timezone('Australia/Sydney')
datetime_Aus = datetime.now(tz_Aus)
check_time = datetime_Aus.strftime("%H:%M:%S")
if begin_time < end_time:
return str(check_time) >= str(begin_time) and str(check_time) <= str(end_time)
else: # crosses midnight
return str(check_time) >= str(begin_time) or str(check_time) <= str(end_time)
,
在进行比较之前将您的时间输入转换为日期。还要注意让他们知道时区。我已经调整了您的方法,这可以正常工作并返回澳大利亚/悉尼当前本地时间的正确结果:
from datetime import datetime,date,time
import pytz
def is_time_between(begin_time,end_time):
tz = pytz.timezone('Australia/Sydney')
now = datetime.now(tz)
begin_date = datetime.combine(date(now.year,now.month,now.day),begin_time,tzinfo=now.tzinfo)
end_date = datetime.combine(date(now.year,tzinfo=now.tzinfo)
if begin_time < end_time:
return now >= begin_date and now <= end_date
else: # crosses midnight
return now >= begin_date or now <= end_date
print(is_time_between(time(6,59,45),time(18,10)))
print(is_time_between(time(22,00),time(23,00)))
print(is_time_between(time(21,time(7,00)))
print(is_time_between(time(23,30),00)))
输出:
False
True
True
False
(使用 Python 3.8.0 测试)
Python如何检查文件名是否在UTF8中?
我有一个PHP脚本来创build一个目录中的文件列表,但是,PHP只能看到英文文件名,并完全忽略其他语言(如俄语或亚洲语言)的文件名。
经过大量的努力,我发现唯一的解决scheme可以为我工作 – 使用python脚本将文件重命名为UTF8,所以PHP脚本可以在那之后处理它们。
(在PHP完成文件处理后,我将这些文件重命名为英文,我不保留它们在UTF8中)。
我用下面的python脚本,工作正常:
Git拉无效的Windows文件名
在Bash中提取文件名和扩展名
为什么所有名为win32的?
Windows“zip文件夹”文件编码
Windows命令行和batch file:文件名中未转义的特殊字符的潜在问题?
import sys import os import glob import ntpath from random import randint for infile in glob.glob( os.path.join(''C:\MyFiles'',u''*'') ): if os.path.isfile(infile): infile_utf8 = infile.encode(''utf8'') os.rename(infile,infile_utf8)
问题是,它也转换文件名已经在UTF8。 如果文件名已经是UTF8,我需要一种方法来跳过转换。
我正在尝试这个Python脚本:
for infile in glob.glob( os.path.join(''C:\MyFiles'',u''*'') ): if os.path.isfile(infile): try: infile.decode(''UTF-8'',''strict'') except UnicodeDecodeError: infile_utf8 = infile.encode(''utf8'') os.rename(infile,infile_utf8)
但是,如果文件名已经在utf8中,我得到致命的错误:
UnicodeDecodeError: ''ascii'' codec can''t decode characters in position 18-20 ordinal not in range(128)
我也尝试了另一种方式,这也没有工作:
for infile in glob.glob( os.path.join(''C:\MyFiles'',u''*'') ): if os.path.isfile(infile): try: tmpstr = str(infile) except UnicodeDecodeError: infile_utf8 = infile.encode(''utf8'') os.rename(infile,infile_utf8)
我得到了和以前完全一样的错误。
有任何想法吗?
Python对我来说是非常新的,对我来说,debugging即使是一个简单的脚本也是一个巨大的努力,所以请写一个明确的答案(即代码)。 我没有testing一般想法的能力,可能工作,也可能不是。 谢谢。
文件名称的例子:
hello.txt你好.txt 안녕하세요.html chào.doc
重命名在Unix上创build的在Windows中有特殊字符的文件的最佳实践?
是否可以在文件名中使用“/”?
冒号在Python中的文件名
如何从任意string中创build一个有效的Windows文件名?
如何检查一个string是否是使用R的窗口的有效文件名?
我认为你混淆了你的术语,并做出了一些错误的假设。 AFAIK,PHP可以打开任何编码类型的文件名 – PHP对于编码类型是非常不可知的。
你不清楚你想要达到什么样的UTF-8!=英文,例子中的外文件名可以用多种方式编码,但从来不用ASCII英文! 你能解释一下你认为现有的UTF-8文件是什么样的,什么是非UTF-8文件?
添加到你的困惑,在Windows下,文件名被透明地存储为UTF-16。 因此,您不应该尝试将文件名编码为UTF-8。 相反,您应该使用Unicode字符串,并允许Python进行正确的转换。 (不要用UTF-16编码!)
请进一步澄清你的问题。
更新 :
我现在明白你的问题与PHP。 http://evertpot.com/filesystem-encoding-and-PHP/告诉我们非拉丁字符在PHP + Windows中很麻烦。 似乎只能看到并打开由Windows 1252字符集字符组成的文件。
您所面临的挑战是将您的文件名转换为Windows 1252兼容。 正如你在你的问题中所说的那样,最好不要重命名已经兼容的文件。 我已经重新尝试了:
import os from glob import glob import shutil import urllib files = glob(u''*.txt'') for my_file in files: try: print "File %s" % my_file except UnicodeEncodeError: print "File (escaped): %s" % my_file.encode("unicode_escape") new_name = my_file try: my_file.encode("cp1252","strict") print " Name unchanged. copying anyway" except UnicodeEncodeError: print " Can not convert to cp1252" utf_8_name = my_file.encode("UTF-8") new_name = urllib.quote(utf_8_name ) print " New name: (%% encoded): %s" % new_name shutil.copy2(my_file,os.path.join("fixed",new_name))
分解:
打印文件名。 默认情况下,Windows shell仅在本地DOS代码页中显示结果。 例如,我的shell可以显示ü.txt但是€.txt显示为?.txt 。 因此,您需要小心Python抛出异常,因为它不能正确打印。 此代码尝试打印Unicode版本,但是尝试打印Unicode代码点转义。
尝试将字符串编码为Windows-1252。 如果这个工作,文件名是好的
否则:将文件名转换为UTF-8,然后百分比编码。 这样,文件名仍然是唯一的,你可以在PHP中反转这个过程。
将文件复制到新的/已验证的文件。
例如,你好.txt成为%E4%BD%A0%E5%A5%BD.txt
对于Python的所有UTF-8问题,我热烈地建议在PyCon 2012上花36分钟观看Ned Batchelder( http://nedbatchelder.com/text/unipain.html )上的“Pragmatic Unicode” 。对我来说这是一个启示! 本演示文稿中的很多内容实际上并不是Python特定的,但有助于理解Unicode字符串和UTF-8编码字节之间的区别。
我向你推荐这个视频的原因(就像我为许多朋友所做的一样),是因为你的代码包含了一些矛盾,比如尝试decode ,然后在解码失败的时候encode :这种方法不适用于同一个对象! 尽管在Python2中它可能是语法上可能的,但是在Python 3中, bytes和str之间的分歧使事情变得更加清晰:
一个str对象可以用bytes 编码 :
>>> a = ''a'' >>> type(a) <class ''str''> >>> a.encode <built-in method encode of str object at 0x7f1f6b842c00> >>> a.decode Traceback (most recent call last): File "<stdin>",line 1,in <module> AttributeError: ''str'' object has no attribute ''decode''
…而一个bytes对象可以在str 解码 :
>>> b = b''b'' >>> type(b) <class ''bytes''> >>> b.decode <built-in method decode of bytes object at 0x7f1f6b79ddc8> >>> b.encode Traceback (most recent call last): File "<stdin>",in <module> AttributeError: ''bytes'' object has no attribute ''encode''
回到你使用文件名的问题,你需要回答的棘手的问题是:“什么是你的文件名的编码”。 语言无所谓,只有编码 !
总结
以上是小编为你收集整理的Python如何检查文件名是否在UTF8中?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
ruby-on-rails – 检查当前时间是否在晚上10:27之前
当前代码(仅比较小时,而不是分钟):
Time.Now.getlocal("-05:00").hour.between?(10,22)
例如,我们的营业时间是上午9:24 – 晚上10:27,我们想检查它是否当前是打开的.
解决方法
You can also do standard functions like compare two times.
我想你可以这样做:
require 'time' close_time = Time.parse "10:27 pm" #=> 2015-11-23 22:27:00 -0800 current_time = Time.Now #=> 2015-11-23 19:38:06 -0800 current_time < close_time #=> true # means the store is open late_time = Time.Now + 4*60*60 #=> 2015-11-23 23:39:15 -0800 late_time < close_time #=> false # means the store is close
今天关于如何检查当前时间是否在python中?和判断当前时间是否在某一时间段的讲解已经结束,谢谢您的阅读,如果想了解更多关于java-如何检查当前时间是否在android中的预设时间范围之间、python pytz检查时间是否在悉尼/墨尔本时间而不是UTC时间、Python如何检查文件名是否在UTF8中?、ruby-on-rails – 检查当前时间是否在晚上10:27之前的相关知识,请在本站搜索。
本文标签: