如何检查“ 23:00至6:30之间”的状况?

时间:2018-09-02 16:03:28

标签: python python-3.x datetime python-arrow

我想用 var myMaze = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 0, 0, 1, 1, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0], [0, 1, 0, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 1, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; var c_path=new Array(); var path=new Array(); var last ; function maze(myMaze){ this.find = function(col,row){ if(myMaze[row][col] == 1 ){ path.push(new Array(row,col)); console.log(row,col,myMaze[row][col]) } if(myMaze[row][col] == 2){ last =new Array(row,col); console.log('done') } if(myMaze[row][col] == 1 ){ if(c_path.includes(row+"-"+col)){ return; } c_path.push(row+"-"+col); if(row < myMaze.length - 1){ this.find(col,row+1) } if(col< myMaze[row].length -1){ this.find(col+1,row) } if(row > 0){ this.find(col,row-1) } if(col > 0){ this.find(col-1,row) } } } this.show =function(){ for (var i = path.length-1 ;i>0 ;i--){ var tmp =path[i]; if((tmp[0] ==last[0] && Math.abs(tmp[1] - last[1]) ==1) || (tmp[1] ==last[1] && Math.abs(tmp[0] - last[0]) ==1)){ last =tmp; myMaze[tmp[0]][tmp[1]] =3; } } console.log(myMaze); } } var maze= new maze(myMaze) maze.find(0,3) maze.show(); (*)检查当前时间是否在23:00和6:30之间。

我目前在下面的代码中使用过幼稚的检查,但想知道是否存在arrow本机构造

arrow

(*)我很开心地使用import arrow def check(now): if now.hour >= 23 or now.hour < 6 or (now.hour == 6 and now.minute <= 30): print("we are between 23:00 and 6:30 the next day") else: print("we are outside the range") #out now = arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss') check(now) # in now = arrow.get('2013-05-05 23:30:45', 'YYYY-MM-DD HH:mm:ss') check(now) # in now = arrow.get('2013-05-06 01:30:45', 'YYYY-MM-DD HH:mm:ss') check(now) # in now = arrow.get('2013-05-06 05:45:45', 'YYYY-MM-DD HH:mm:ss') check(now) # out now = arrow.get('2013-05-06 07:30:45', 'YYYY-MM-DD HH:mm:ss') check(now) 好多年了,但是如果使用另一个软件包(arrow或另一个我忘了名字的类似软件包)更简单的解决方案很好

1 个答案:

答案 0 :(得分:1)

您可以使用默认的datetime python库,但是请记住,您必须遵守时区,如果出现以下情况,我建议您致电datetime.utcnowdatetime.now使用UTC你需要当地时间。这是一个示例:

In [12]: from datetime import datetime
In [13]: now = datetime.utcnow()
In [14]: now.hour
Out[14]: 16
In [15]: 6 < now.hour < 23
Out[15]: True

PS。如果由于某种原因不能选择datetime,则可以使用arrow.utcnow()或相同的本地化arrow.now()变体。