[综合] 为什么要使用Unix时间戳

转载自:http://blog.sina.com.cn/s/blog_7e4815650100wrdn.html

概念:
UNIX时间戳:Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)
是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
UNIX时间戳的0按照ISO 8601规范为 :1970-01-01T00:00:00Z.
一个小时表示为UNIX时间戳格式为:3600秒;一天表示为UNIX时间戳为86400秒,闰秒不计算。
在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题或Y2038。

时间 秒
1 分钟 60 秒
1 小时 3600 秒
1 天 86400 秒
1 周 604800 秒
1 月 (30.44 天) 2629743 秒
1 年 (365.24 天) 31556926 秒

思考:
为什么使用UNIX时间戳?
在现在的系统中经常遇到跨数据库的应用开发,在数据库系统中不同的数据库对与时间类型却有不同解释,比如ORACLE的date和MYSQL里面的date就不能直接兼容转换,数据方面还可以使用数据迁移工具进行转换,但是对与应用来说那就是灾难(在这不谈hibernate等可以垮数据库的框架)。
为了实现垮平台在应用系统中记录时间的时候我们就可以使用记录UNIX时间戳的方法做到垮平台性。
现在大多数的语言java、PHP、Perl等都支持直接取UNIX时间戳,将需要记录的时间记录为UNIX时间戳,这样就可以不同的数据库系统中的垮平台性,对与时间的操作只要对时间戳操作就行了。其实这样的处理方法在很多开源的系统中都能看到,但是对与经常搞J2EE的人来说很不习惯.

处理:

  下面简单介绍下相关处理的方法:

================================获取系统UNIX时间戳=======================================

Perl time
PHP time()
Ruby Time.now (or Time.new). To display the epoch: Time.now.to_i
Python import time first, then time.time()
Java long epoch = System.currentTimeMillis()/1000;
Microsoft .NET C# epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
VBScript/ASP DateDiff(“s”, “01/01/1970 00:00:00”, Now())
MySQL SELECT unix_timestamp(now())
PostgreSQL SELECT extract(epoch FROM now());
SQL Server SELECT DATEDIFF(s, ‘19700101’, GETDATE())
JavaScript Math.round(new Date().getTime()/1000.0) getTime() returns time in milliseconds.
Unix/Linux date +%s
Other OS’s Command line: perl -e “print time” (If Perl is installed on your system)

================================将时间转换成UNIX时间戳=======================================

Perl Use these Perl Epoch routines
PHP mktime(hour, minute, second, month, day, year) More information
Ruby Time.local(year, month, day, hour, minute, second, usec ) (or Time.gm for GMT/UTC input). To display add .to_i
Python import time first, then int(time.mktime(time.strptime(‘2000-01-01 12:34:00’, ‘%Y-%m-%d %H:%M:%S’)))
Java long epoch = new java.text.SimpleDateFormat (“dd/MM/yyyy HH:mm:ss”).parse(“01/01/1970 01:00:00”);
VBScript/ASP DateDiff(“s”, “01/01/1970 00:00:00”, time field) More information
MySQL SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD More information
PostgreSQL SELECT extract(epoch FROM date(‘2000-01-01 12:34’));
With timestamp: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE ‘2001-02-16 20:38:40-08’);
With interval: SELECT EXTRACT(EPOCH FROM INTERVAL ‘5 days 3 hours’);
SQL Server SELECT DATEDIFF(s, ‘19700101’, time field)
JavaScript use the JavaScript Date object
Unix/Linux date +%s -d”Jan 1, 1980 00:00:01”

================================将UNIX时间戳转换成时间=======================================

Perl Use these Perl Epoch routines
PHP date(output format, epoch); Output format example: ‘r’ = RFC 2822 date More information
Ruby Time.at(epoch)
Python import time first, then time.gmtime(epoch) time is an array of year, month, day, hour, min, sec, day of week, day of year, DST More information
Java String date = new java.text.SimpleDateFormat(“dd/MM/yyyy HH:mm:ss”).format(new java.util.Date (epoch*1000));
VBScript/ASP DateAdd(“s”, epoch, “01/01/1970 00:00:00”) More information
PostgreSQL SELECT TIMESTAMP WITH TIME ZONE ‘epoch’ + epoch * INTERVAL ‘1 second’;
MySQL from_unixtime(epoch, optional output format) The default output format is YYY-MM-DD HH:MM:SS More information
SQL Server DATEADD(s, epoch, ‘19700101’)
JavaScript use the JavaScript Date object
Linux date -d @1190000000 (replace 1190000000 with your epoch, needs newer version of date)
Other OS’s Command line: perl -e “print scalar(localtime(epoch))” (If Perl is installed) Replace ‘localtime’ with ‘gmtime’ for GMT/UTC time.

转自:http://hi.baidu.com/xzeus/blog/item/823dd1001f2d6217738b6510.html

from_unixtime()是MySQL里的时间函数,将时间戳转换成日期
date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串
后面的 ‘%Y%m%d’ 主要是将返回值格式化
例如:
mysql>SELECT FROM_UNIXTIME( 1249488000, ‘%Y%m%d’ )
->20071120
mysql>SELECT FROM_UNIXTIME( 1249488000, ‘%Y年%m月%d’ )
->2007年11月20

UNIX_TIMESTAMP() 是与之相对正好相反的时间函数,将日期转换成时间戳
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
  若无参数调用,则返回一个 Unix timestamp (‘1970-01-01 00:00:00’ GMT 之后的秒数) 作为无符号整数。若用date 来调用 UNIX_TIMESTAMP(),它会将参数值以’1970-01-01 00:00:00’ GMT后的秒数的形式返回。date 可以是一个 DATE 字符串、一个 DATETIME字符串、一个 TIMESTAMP或一个当地时间的YYMMDD 或YYYMMDD格式的数字。
例如:
mysql> SELECT UNIX_TIMESTAMP() ; (执行使得时间:2009-08-06 10:10:40)
->1249524739
mysql> SELECT UNIX_TIMESTAMP(‘2009-08-06’) ;
->1249488000
SELECT *
FROM student
WHERE regTime > UNIX_TIMESTAMP( curdate( ) ) //今天所有学生注册记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值