用 Python 获取并显示现在的日期和时间
最编程
2024-08-08 17:20:59
...
在python中,可以使用datetime来处理时间和日期,使用是要现导入datetime库
>>> import datetime
1.获得当前日期不显示时间
>>> print(datetime.date.today())
2019-03-28
2.使用today和now获取当前日期和时间,时间精确到毫秒级
>>> print(datetime.datetime.today())
2019-03-28 20:34:05.079049
>>> print(datetime.datetime.now())
2019-03-28 20:34:11.377443
3.使用strftime()格式化时间为标准格式,strftime()可以将日期输出为我们想要的格式(要特别注意参数区分大小写)
如只输出日期:
>>> print(datetime.datetime.now().strftime('%Y-%m-%d'))
2019-03-28
若输出当前日期和时间:
>>> print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
2019-03-28 20:41:11
若在输出日期和时间的同时还要输出星期几:%a 输出星期的简写,%A输出星期的全写
>>> print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %A'))
2019-03-28 20:43:06 Thursday
>>> print(datetime.datetime.now().strftime('%Y-%m-%d %a'))
2019-03-28 Thu
输出月份:%b 简写,%B 全写
>>> print(datetime.datetime.now().strftime('%Y-%m-%d %a %B'))
2019-03-28 Thu March
4.计算倒计时:实现倒计时的方法有很多。其中一种:首先设置一个未来的时间,通过strptime对象设置未来的时间,设置的时间包括年、月、日、时、分、秒。
如:计算从现在到2020年3月31号又多少天:
import datetime
future=datetime.datetime.strptime("2020-3-31 0:0:0","%Y-%m-%d %H:%M:%S") #使用strptime设置一个未来的时间
now=datetime.datetime.now() #获取当前时间
delta=future-now #获取两时间之间的差值,(days,seconds,microseconds)
days=delta.days #获取倒计时的天数
hours=int(delta.seconds/60/60) #获取倒计时的小时数
minutes=int((delta.seconds-hours*60*60)/60) #获取倒计时的分钟数
secondes=delta.seconds-hours*60*60-minutes*60 #获取倒计时的秒数
print("当前时间:",now.strftime("%Y-%m-%d %H:%M:%S"),"距离未来时间:",future.strftime("%Y-%m-%d %H:%M:%S"))
print("还有:",days,"天,",hours,"时",minutes,"分",secondes,"秒")
#运行结果
当前时间: 2019-03-28 21:25:33 距离未来时间: 2020-03-31 00:00:00
还有: 368 天, 2 时 34 分 26 秒
5.计算过去或未来的日期:可以使用datetime模块的timedelta对象结合具体的时间对象来实现,timedelta对象可以格式化天、小时、分钟。
import datetime
now=datetime.datetime.now()
print("现在的时间:",now.strftime("%Y-%m-%d %H:%M:%S"))
futureDays1=now+datetime.timedelta(days=5) #五天后的时间)
print("五天后的时间:",futureDays1.strftime("%Y-%m-%d %H:%M:%S"))
OldDays2=now-datetime.timedelta(days=3) #三天前的时间
print("三天前的时间:",OldDays2.strftime("%Y-%m-%d %H:%M:%S"))
futureHours=now+datetime.timedelta(hours=11) #11小时后的时间
print("11小时后的时间:",futureHours.strftime("%Y-%m-%d %H:%M:%S"))
futureMinutes=now-datetime.timedelta(minutes=2999) #2999分钟前的时间
print("2999分钟前的时间:",futureMinutes.strftime("%Y-%m-%d %H:%M:%S"))
#运行结果
现在的时间: 2019-03-28 22:14:34
五天后的时间: 2019-04-02 22:14:34
三天前的时间: 2019-03-25 22:14:34
11小时后的时间: 2019-03-29 09:14:34
2999分钟前的时间: 2019-03-26 20:15:34
推荐阅读
-
用JavaScript获取实时和昨天的日期时间方法
-
用PHP轻松获取当前的日期、时间和年月日
-
用 Python 获取并显示现在的日期和时间
-
用Python的datetime和time模块获取实时日期和时间 - 学习如何通过strftime修改时间对象的显示格式
-
在JavaScript中获取和显示今天的日期、时间和星期
-
用 JavaScript 的 Date.toLocaleString() 方法来定制日期和时间的显示样式
-
测试事物 (I) - 同步和异步 - 同步:当函数调用启动时,调用不会返回或调用者不会继续执行后续操作,直到获得该调用的结果。异步:异步与同步相反,当发出异步过程调用时,调用者可以继续执行后续操作,直到调用者得到结果。调用完成后,通常会通过状态(轮询)、通知(消息)和回调通知调用者。对于异步调用,调用的返回不受调用者的控制。 从上述定义中我们可以看出,同步和异步可以由调用者或被调用者控制。我们暂且将调用者视为客户端,而被调用者视为服务器。 在客户端调用服务器接口后,如果客户端需要等待服务器返回结果才能进行下一步,那么它就是同步的。如果在进行下一步之前不需要等待服务器返回结果,那么它就是同步的。如果在进行下一步之前不需要等待服务器的结果,那么它就是异步的。 服务器本身既可以控制同步,也可以控制异步。对于需要长时间计算的函数,服务器会将其设置为异步,在客户端发出请求后立即向客户端返回结果,这没有任何实际意义,只是表示服务器收到了请求。对于很快就能得到结果的请求,使用同步就很好,返回的结果包含了得到这个请求所需的数据。 那么,同步测试和异步测试需要注意什么呢? 1. 用户体验 现在很多 APP 都有审核的功能,需要用户提供各种信息来完成相应的认证。这些审核可以是自动的,也可以是手动的等。自动审核一般比较快,因为它需要用户提供各种信息来完成相应的认证。自动审核一般比较快,因为需要比对的数据都存在服务器端,直接进行程序比对即可。人工审核则比较慢,快的几分钟,如果赶上周末,2-3 天都不一定。因此,对于自动审核和人工审核,需要合理利用同步和异步,达到最佳的用户体验。 自动审核速度快,用户提交信息后稍微等待一下就应该能得到审核结果,最多在客户端等待不超过 5 秒,看到加载等待一会儿就可以了。在这种情况下,使用同步就非常合适了,客户端没有得到结果,等到服务器端把审核结果传回来马上显示在客户端上,用户的体验会非常好,毕竟很快就得到了答案。 人工审核时,我们不能寄希望于总有一个人一直在那里审核,即使有,快的话也要几分钟,毕竟核对数据什么的都是人工的。用户在这种情况下不能等,但也不能总在这个页面上等。于是异步就派上用场了,客户端发出审核请求,服务器收到后马上告诉客户端我收到了,但不可能给出结果。客户端不管有没有收到服务器的请求,都会告诉用户等待 XX 时间才能查看结果,用户不需要在这个页面上一直等待。直到服务器端计算完成,然后通过推送消息通知客户端,或者用户再次进入身份验证页面进行身份验证结果请求时,才会得知自己的身份验证结果。 进行测试时,如果遇到长时间无法获取结果的界面,建议客户端使用异步请求,以减少用户体验不佳的情况!
-
包婷婷 (201550484)作业一 统计软件简介与数据操作-SPSS(Statistical Product and Service Solutions),"统计产品与服务解决方案"软件。最初软件全称为"(SolutionsStatistical Package for the Social Sciences),但是随着SPSS产品服务领域的扩大和服务深度的增加,SPSS公司已于2000年正式将英文全称更改为"统计产品与服务解决方案",标志着SPSS的战略方向正在做出重大调整。为IBM公司推出的一系列用于统计学分析运算、数据挖掘、预测分析和决策支持任务的软件产品及相关服务的总称SPSS,有Windows和Mac OS X等版本。 1984年SPSS总部首先推出了世界上第一个统计分析软件微机版本SPSS/PC+,开创了SPSS微机系列产品的开发方向,极大地扩充了它的应用范围,并使其能很快地应用于自然科学、技术科学、社会科学的各个领域。世界上许多有影响的报刊杂志纷纷就SPSS的自动统计绘图、数据的深入分析、使用方便、功能齐全等方面给予了高度的评价。 R统计软件介绍 R是一套完整的数据处理、计算和制图软件系统。其功能包括:数据存储和处理系统;数组运算工具(其向量、矩阵运算方面功能尤其强大);完整连贯的统计分析工具;优秀的统计制图功能;简便而强大的编程语言:可操纵数据的输入和输出,可实现分支、循环,用户可自定义功能。 与其说R是一种统计软件,还不如说R是一种数学计算的环境,因为R并不是仅仅提供若干统计程序、使用者只需指定数据库和若干参数便可进行一个统计分析。R的思想是:它可以提供一些集成的统计工具,但更大量的是它提供各种数学计算、统计计算的函数,从而使使用者能灵活机动的进行数据分析,甚至创造出符合需要的新的统计计算方法。 该语言的语法表面上类似 C,但在语义上是函数设计语言(functional programming language)的变种并且和Lisp 以及 APL有很强的兼容性。特别的是,它允许在"语言上计算"(computing on the language)。这使得它可以把表达式作为函数的输入参数,而这种做法对统计模拟和绘图非常有用。 R是一个免费的*软件,它有UNIX、LINUX、MacOS和WINDOWS版本,都是可以免费下载和使用的。在R主页那儿可以下载到R的安装程序、各种外挂程序和文档。在R的安装程序中只包含了8个基础模块,其他外在模块可以通过CRAN获得。 二、R语言 R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个*、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。 R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX,Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R还有以下特点: 1.R是*软件。这意味着它是完全免费,开放源代码的。可以在它的网站及其镜像中下载任何有关的安装程序、源代码、程序包及其源代码、文档资料。标准的安装文件身自身就带有许多模块和内嵌统计函数,安装好后可以直接实现许多常用的统计功能。[2] 2.R是一种可编程的语言。作为一个开放的统计编程环境,语法通俗易懂,很容易学会和掌握语言的语法。而且学会之后,我们可以编制自己的函数来扩展现有的语言。这也就是为什么它的更新速度比一般统计软件,如,SPSS,SAS等快得多。大多数最新的统计方法和技术都可以在R中直接得到。[2] 3. 所有R的函数和数据集是保存在程序包里面的。只有当一个包被载入时,它的内容才可以被访问。一些常用、基本的程序包已经被收入了标准安装文件中,随着新的统计分析方法的出现,标准安装文件中所包含的程序包也随着版本的更新而不断变化。在另外版安装文件中,已经包含的程序包有:base一R的基础模块、mle一极大似然估计模块、ts一时间序列分析模块、mva一多元统计分析模块、survival一生存分析模块等等.[2] 4.R具有很强的互动性。除了图形输出是在另外的窗口处,它的输入输出窗口都是在同一个窗口进行的,输入语法中如果出现错误会马上在窗口口中得到提示,对以前输入过的命令有记忆功能,可以随时再现、编辑修改以满足用户的需要。输出的图形可以直接保存为JPG,BMP,PNG等图片格式,还可以直接保存为PDF文件。另外,和其他编程语言和数据库之间有很好的接口。[2] 5.如果加入R的帮助邮件列表一,每天都可能会收到几十份关于R的邮件资讯。可以和全球一流的统计计算方面的专家讨论各种问题,可以说是全世界最大、最前沿的统计学家思维的聚集地.[2] R是基于S语言的一个GNU项目,所以也可以当作S语言的一种实现,通常用S语言编写的代码都可以不作修改的在R环境下运行。 R的语法是来自Scheme。R的使用与S-PLUS有很多类似之处,这两种语言有一定的兼容性。S-PLUS的使用手册,只要稍加修改就可作为R的使用手册。所以有人说:R,是S-PLUS的一个“克隆”。 但是请不要忘了:R是免费的(R is free)。R语言源代码托管在github,具体地址可以看参考资料。[3] 。 R语言的下载可以通过CRAN的镜像来查找。 R语言有域名为.cn的下载地址,有六个,其中两个由Datagurn,由 中国科学技术大学提供的。R语言Windows版,其中由两个下载地点是Datagurn和 USTC提供的。 三、stata Stata 是一套提供其使用者数据分析、数据管理以及绘制专业图表的完整及整合性统计软件。它提供许许多多功能,包含线性混合模型、均衡重复反复及多项式普罗比模式。用Stata绘制的统计图形相当精美。 新版本的STATA采用最具亲和力的窗口接口,使用者自行建立程序时,软件能提供具有直接命令式的语法。Stata提供完整的使用手册,包含统计样本建立、解释、模型与语法、文献等超过一万余页的出版品。 除此之外,Stata软件可以透过网络实时更新每天的最新功能,更可以得知世界各地的使用者对于STATA公司提出的问题与解决之道。使用者也可以透过Stata. Journal获得许许多多的相关讯息以及书籍介绍等。另外一个获取庞大资源的管道就是Statalist,它是一个独立的listserver,每月交替提供使用者超过1000个讯息以及50个程序。 四、PYTHON
-
《京沪公园使用大数据报告》解读城市公园新机遇-Part One 公园基本情况 1 城市公园分布 城市公园分布广泛,主要集中在中心城区和郊区的居住密集区 根据公开数据,北京市注册公园数为403个(2016年),上海市为165个(2015年)。基于腾讯地图,找到京沪两地所有公园的位置点信息,将它们画在地图上,可以发现,城市公园分布广泛,并且主要集中在中心城区和郊区的居住密集区。 公园数量数据来源:北京-《瞭望东方周刊》,上海-2016年上海市统计年鉴; 公园位置点信息来源:腾讯地图POI数据; 2 人均公园绿地面积 北京北部、上海东北部人均公园绿地面积较多 数据显示,截至2015年年末,北京的人均公园绿地面积为13.6平方米/人,上海则为7.6平方米/人。从各区人均公园绿地面积的数据可以看出,受区域面积和人口数量的双重制约,城市中心区的人均公园绿地面积通常较小。北京人均公园绿地面积较多的地区主要是在北部,而上海则是在东北部。 数据来源:北京市园林局网站,2016年上海市统计年鉴 Part Two 公园受欢迎程度 1 网络热度 哪些公园是“网红”? 樱花季促成玉渊潭公园和顾村公园最热! 根据腾讯位置大数据,春季时,在我们选取的几个公园中,用户通过社交分享最多的公园,北京是玉渊潭公园,上海是顾村公园。这两个公园的热度远远领先其他公园,成为当之无愧的“网红”公园。玉渊潭公园和顾村公园在春季都有樱花节活动,京沪两地的植物园在春季也有较高的网络热度。 注:社交分享包括微信朋友圈、QQzone等社交工具中的签到信息 2 公园吸引力程度 公园有多吸引人? 部分大型公园超50%的游客来源于10公里外 公园的吸引力可以用到访者居住地到公园的直线距离的中位数来衡量。根据腾讯位置大数据分析,京沪两地都是知名公园吸引力较大。以北京颐和园和上海辰山植物园为例,50%的游客来源于20.3公里和17.6公里以外。热度最高的北京玉渊潭公园和上海顾村公园也有较高的吸引力。 3 外地游客比例 只有知名公园有外地游客? 社区公园仍有5~10%的外地游客到访 颐和园作为全国景点,毫不意外,外地游客比例高达40%,远超京沪其他公园。上海的公园中,人民公园的外地游客比例达到19%,可能与其临近旅游热点南京路和人民广场有关。总的来说,本身就是景点或临近人群聚集地的公园外地游客比例高。通常意义上社区公园主要服务于当地居民,而数据显示,京沪的社区公园仍有5~10%的外地游客到访。 Part Three 公园使用情况 1 工作日和周末人流量对比 北京奥林匹克森林公园超200%, 上海辰山植物园周末游客增幅达170% 大型的综合公园、主题公园、郊区公园周末人流量都有至少50%的增长,北京的奥林匹克森林公园、南海子郊野公园,上海的辰山植物园、顾村公园,周末游客增幅达到了100%以上。一些距离工作区较近的公园如上海陆家嘴中心广场公园、北京CBD历史文化公园,周末时人流量则出现了明显下降。 2 人流量随时间变化 上海的周末人流高峰期更晚更集中 上海中心型公园、主题型公园和郊区公园的高峰期在周末更为集中,并会发生明显推迟,到14~15点才达到人流量高峰。而北京的公园在上午10~12点期间就进入了高峰期,在15~16点也有一个高峰期。 Part Four 位置大数据带来的启示 1 建立合理的公园体系结构 丰富体系结构:大型公园和小微公园结合,优化空间利用率 公园的使用情况可以用空间利用效率衡量,即“到访人数/公园面积” 。从数据来看,京沪两地空间利用效率高的公园都是中小型公园,而大型公园的空间利用效率则较低; 考虑到安全、环境等管理成本,大型公园多采取收费、围合等管理模式,这会降低公园的空间利用率,可进一步研究费用高低、范围设定和利用率的关系,优化空间利用率; 合理的城市公园体系还需要与就业中心结合的公园以及服务社区的公园。在空间资源日益紧张的大城市,除了在郊区新建大型公园外,在城市中心区新建更多的微型、小型公园也是不错的选择。 2 混搭多种功能区域 混搭区域功能:城市公园选址宜与多种功能区域搭配,关注慢行设施 城市公园可以和工作区、商圈、公共活动区等多种区域相结合。公园在工作日可服务于区域就业、商务人士,在周末也可以服务市民休闲和社会活动。有利于凝聚人气,更大地发挥公园的功能。如上海的徐家汇公园,周边是写字楼聚集区,又是商圈,同时又有大量居民区,周末的空间利用效率相比于工作日反升8%;。 关注公园周边慢行设施的设计,方便游客步行到达。结合热力图和三维地图可以看出,徐家汇公园东西两侧的汇金广场、港汇广场、徐家汇国际大厦、宛平宾馆、上海财政局等是公园使用者集聚程度最高的地区。五洲国际广场、均瑶国际广场虽然距离较远,但是沿肇嘉浜路到徐家汇公园较为便捷,也在步行范围内,所以也同样具有较高互动性。可见便利的慢行设施可以增强公园和周边的互动。 3 位置大数据优化公园服务 优化公园服务:根据到访人群来源判断服务偏向,提供精准服务 通过位置大数据,可以识别出公园的服务偏向,判断公园的使用是否符合预期,及时优化公园内部及周边服务设施。服务偏向可以根据到访过公园的游客工作或居住在公园周边2公里内的占比判断。如北京的北小河公园、上海的彭浦公园等服务周边的居住人群更多,这些公园可以考虑多配备小广场、健身器材、儿童游乐场等设施;而北京的CBD历史文化公园、上海的西康公园服务工作人群更多,则可以考虑多进行绿化并配备长椅等休闲设施。 4 位置大数据助力公园管理 助力公园管理:位置大数据提供精准宣传和功能评估依据 在“互联网+”日益发达的今天,位置大数据可以帮助有意发展旅游产业的城市公园:
-
正负偏差变量 即 d2+、d2- 分别表示决策值中超出和未达到目标值的部分。而 di+、di- 均大于 0 刚性约束和目标约束(柔性目标约束有偏差) 在多目标规划中,>=/<= 在刚性约束中保持不变。当需要将约束条件转换为柔性约束条件时,需要将 >=/<= 更改为 =(因为已经有 d2+、d2- 用来表示正负偏差),并附加上 (+dii-di+) 注意这里是 +di、-di+!之所以是 +di,-di+,是因为需要将目标还原为最接近的原始刚性约束条件 优先级因素和权重因素 对多个目标进行优先排序和优先排序 目标规划的目标函数 是所有偏差变量的加权和。值得注意的是,这个加权和都取最小值。而 di+ 和 dii- 并不一定要出现在每个不同的需求层次中。具体分析需要具体问题具体分析 下面是一个例子: 题目中说设备 B 既要求充分利用,又要求尽可能不加班,那么列出的时间计量表达式即为:min z = P3 (d3- + d3 +) 使用 + 而不是 -d3 + 的原因是:正负偏差不可能同时存在,必须有 di+di=0 (因为判定值不可能同时大于目标值和小于目标值),而前面是 min,所以只要取 + 并让 di+ 和 dii- 都为正值即可。因此,得出以下规则: 最后,给出示例和相应的解法: 问题:某企业生产 A 和 B 两种产品,需要使用 A、B、C 三种设备。下表显示了与工时和设备使用限制有关的产品利润率。问该企业应如何组织生产以实现下列目标? (1) 力争利润目标不低于 1 500 美元; (2) 考虑到市场需求,A、B 两种产品的生产比例应尽量保持在 1:2; (3)设备 A 是贵重设备,严禁超时使用; (4)设备 C 可以适当加班,但要控制;设备 B 要求充分利用,但尽量不加班。 从重要性来看,设备 B 的重要性是设备 C 的三倍。 建立相应的目标规划模型并求解。 解:设企业生产 A、B 两种产品的件数分别为 x1、x2,并建立相应的目标计划模型: 以下为顺序求解法,利用 LINGO 求解: 1 级目标: 模型。 设置。 variable/1..2/:x;! s_con_num/1...4/:g,dplus,dminus;!所需软约束数量(g=dplus=dminus 数量)及相关参数; s_con(s_con_num);! s_con(s_con_num,variable):c;!软约束系数; 结束集 数据。 g=1500 0 16 15. c=200 300 2 -1 4 0 0 5; 结束数据 min=dminus(1);!第一个目标函数;!对应于 min=z 的第一小部分;! 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); !使用设置完成的数据构建软约束表达式; ! !软约束表达式 @for(variable:@gin(x)); !将变量约束为整数; ! 结束 此时,第一级目标的最优值为 0,第一级偏差为 0: 第二级目标: !求 dminus(1)=0,然后求解第二级目标。 模型。 设置。 变量/1..2/:x;!设置:变量/1..2/:x; ! s_con_num/1...4/:g,dplus,dminus;!软约束数量及相关参数; s_con(s_con_num(s_con_num));! s_con(s_con_num,variable):c;! 软约束系数; s_con(s_con_num,variable):c;! 结束集 数据。 g=1500 0 16 15; c=200 300 2 -1 4 0 0 5; 结束数据 min=dminus(2)+dplus(2);!第二个目标函数 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); ! 软约束表达式;! dminus(1)=0; !第一个目标结果 @for(variable:@gin(x)); ! 结束 此时,第二个目标的最优值为 0,偏差为 0: 第三目标 !求 dminus(2)=0,然后求解第三个目标。 模型。 设置。 变量/1..2/:x;!设置:变量/1..2/:x; ! s_con_num/1...4/:g,dplus,dminus;!软约束数量及相关参数; s_con(s_con_num(s_con_num));! s_con(s_con_num,variable):c;! 软约束系数; s_con(s_con_num,variable):c;! 结束集 数据。 g=1500 0 16 15; c=200 300 2 -1 4 0 0 5; 结束数据 min=3*dminus(3)+3*dplus(3)+dminus(4);!第三个目标函数。 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); ! 软约束表达式;! dminus(1)=0; !第一个目标约束条件; ! dminus(2)+dplus(2)=0; !第二个目标约束条件 @for(variable:@gin(x));! 结束 最终结果为 x1=2,x2=4,dplus(1)=100,最优利润为