python各种语言间时间的转化实现代码
2016-03-23 00:00
741 查看
一 基本知识
millisecond 毫秒
microsecond 微秒
nanosecond 纳秒
1秒=1000毫秒 1毫秒=1000微秒 1微秒=1000纳秒
二 perl
perl中可以使用time或localtime来获得时间,time返回从1970年1月1日0点的秒数,localtime返回当前时间的字符串表示,或者年月日等得tuple表示。
三 c#
1tick = 100 nanosecond
四 python
python的perl相似,time也是从1970年1月1日开始的秒数。
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:%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
相关文章推荐
- 如何在 Linux 上安装、配置 NTP 服务器和客户端?
- Chrony:一个类 Unix 系统上 NTP 客户端和服务器替代品
- DB2 日期和时间的函数应用说明
- flex actionScript时间处理相加返回相加后的date
- Delphi实现读取系统时间与日期完整实例
- Lua常用时间函数使用实例
- asp格式化日期时间格式的代码
- Powershell小技巧之通过EventLog查看近期电脑开机和关机时间
- C#实现判断一个时间点是否位于给定时间区间的方法
- 比较简洁的JavaScript 实时显示时间的脚本 修正版
- C#简单读取、改变文件的创建、修改及访问时间的方法
- 在C语言中转换时间的基本方法介绍
- C语言循环结构与时间函数用法实例教程
- C#获取指定目录最后写入时间的方法
- php mssql 时间格式问题
- php实现的Timer页面运行时间监测类
- sqlite时间戳转时间语句(时间转时间戳)
- 页面导出为Excel的时间格式的问题
- Javascript中的getUTCHours()方法使用详解
- JS中处理时间之setUTCMinutes()方法的使用