• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python各种语言间时间的转化实现代码

python 搞代码 4年前 (2022-01-07) 24次浏览 已收录 0个评论

这篇文章主要介绍了python各种语言间时间的转化,需要的朋友可以参考下

一 基本知识

millisecond 毫秒
microsecond 微秒
nanosecond 纳秒
1秒=1000毫秒 1毫秒=1000微秒 1微秒=1000纳秒

二 perl

perl中可以使用time或localtime来获得时间,time返回从1970年1月1日0点的秒数,localtime返回当前时间的字符串表示,或者年月日等得tuple表示。

 #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); # seconds from 1970.01.01 00:00:00 my $ti = time(); print $ti; print "\n"; print strftime("%Y-%m-%d %H:%M:%S\n", localtime($ti)); #1310623469 #2011-07-14 14:03:58 my $t = localtime(); print $t; print "\n"; #Thu Jul 14 12:25:16 2011 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(); print $year; print "\n"; #111 print strftime("%Y-%m-%d %H:%M<mark style="color:transparent">来源gaodaimacom搞#^代%!码&网</mark>:%S\n", localtime()); #2011-07-14 12:26:01

三 c#
1tick = 100 nanosecond

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyTest { class Program { static void DateTimeTest() { DateTime dt2 = DateTime.Now; Console.WriteLine(dt2.Ticks); Console.WriteLine(dt2.ToString("MM/dd/yyyy hh:mm:ss")); } static DateTime? ConvertPerlTimeToDateTime(string perltime) { DateTime? dt = null; //perl time variable : seconds from 1970.01.01 00:00:00 string sdt = perltime; long ldt = 0; if (long.TryParse(sdt, out ldt)) { long ldt2 = new DateTime(1970, 1, 1).Ticks + ldt * 1000 * 1000 * 10; dt = new DateTime(ldt2, DateTimeKind.Local); Console.WriteLine(dt.Value.ToString("MM/dd/yyyy hh:mm:ss")); } return dt; } static void Main(string[] args) { DateTimeTest(); ConvertPerlTimeToDateTime("1309423883"); //634462479788396720 //07/14/2011 01:46:18 //06/30/2011 08:51:23 } } }

四 python

python的perl相似,time也是从1970年1月1日开始的秒数。

 import time ISOTIMEFORMAT='%Y-%m-%d %X' # seconds from 1970.01.01 00:00:00 t = time.time() print (t) print time.strftime(ISOTIMEFORMAT,time.localtime(t)) #1310623143.12 #2011-07-14 13:59:03 (year,mon,day,hour,min,sec,wday,yday,isdst) = time.localtime() print (year) print (time.strftime(ISOTIMEFORMAT, time.localtime())) #2011 #2011-07-14 13:59:03

以上就是python各种语言间时间的转化实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python各种语言间时间的转化实现代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址