Date用法(转)

linux下对date和timestamp的互转

 

1. date 到 timestamp:
$ date -d '2009-12-01 23:20' %s    
1259680800

2. timestamp 到 date
$ date -d '1970-01-01 1259680800 sec utc'          
Tue Dec  1 23:20:00 CST 2009

3. timestamp 到 UTC date
[root@CDM ~]# date -d '1970-01-01 UTC 1284117137 sec'  -u
五  9月 10 11:12:17 UTC 2010

4. date,当前时间转换为UTC timestamp
[root@CDM ~]# date  +%s

1284117146

 

GMT
GMT 是 Greenwich Mean Time 的缩写,译为中文为“格林威治标准时间”或“格林尼治标准时间”,直译的话,可译为“格林威治平时”或“格林尼治平时”。这里的格林威治位于英国伦敦东南方向的泰晤士河畔,美国也有个格林威治镇,不过与时间没关系。
 
UTC
UTC 是 Coordinated Universal Time 的缩写,译为中文为“世界标准时间”,直译的话,可译为“协调通用时间”或“协调世界时间”。目前来说也就是指 GMT 时间。为什么说目前就是指 GMT 时间呢?因为本初子午线(子午线即经线,本初子午线即 0 度经线)其实穿过的是沙特阿拉伯西边的麦加,而不是英国的格林威治。当时英国皇家学会暂时确定格林威治为本初子午线的穿过点﹐加之英国正是兴旺发达时期,全世界就将错就错,用到现在。说不定哪天改为麦加时间为标准时间也不是没有可能。所以我们一般使用 UTC,而不是 GMT。
 
PDT
PDT 是 Pacific Daylight Time 的缩写,译为中文为“太平洋夏令时间”,比 UTC 时间晚 7 小时。Google 曾在其 AdSense 中提到“在 09-6-13,我们的工程师会在 PDT 大约 上午10:00 到下午2:00 对网站进行维护。”
其实除了 PDT 时间,北美地区还有很多时间:
美国夏季始于每年4月的第1个周日,止于每年10月的最后一个周日。夏令时比正常时间早一小时与PDT时间相对应的是PST , Pacific Standard Time 。 夏时制结束后就是PST
  • NST - Newfoundland Standard Time 纽芬兰标准时间
  • NDT - Newfoundland Daylight Time 纽芬兰夏令时间
  • AST - Atlantic Standard Time 大西洋标准时间
  • ADT - Atlantic Daylight Time 大西洋夏令时间
  • EST - Eastern Standard Time 东部标准时间
  • EDT - Eastern Daylight Time 东部夏令时间
  • CST - Central Standard Time 中部标准时间
  • CDT - Central Daylight Time 中部夏令时间
  • MST - Mountain Standard Time 山地标准时间
  • MDT - Mountain Daylight Time 山地夏令时间
  • PST - Pacific Standard Time 太平洋标准时间
  • PDT - Pacific Daylight Time 太平洋夏令时间
  • AKST - Alaska Standard Time 阿拉斯加标准时间
  • AKDT - Alaska Daylight Time 阿拉斯加夏令时间
  • HAST - Hawaii-Aleutian Standard Time 夏威夷-阿留申标准时间
  • HADT - Hawaii-Aleutian Daylight Time 夏威夷-阿留申夏令时间

PST
PST是太平洋标准时间(西八区),与北京时间(东八区)时差-16个小时,也就是北京时间减去16就是PST时间。而PDT比PST早1个小时,就是说PDT与北京时间时差为-15小时



夏令时
Daylight Saving Time 或 Daylight Time。
美国和加拿大原本于每年10月的最后一个星期日凌晨2时起实施冬令时间;4月的第一个星期日凌晨2时起,恢复夏令时间。
  但是根据美国国会最 新通过的能源法案,为加强日光节约,自2007年起延长夏令时间,开始日期从每年4月的第一个星期日,提前到3月的第二个星期日,结束日期从每年10月的 最后一个星期日,延后到11月的第一个星期日。换言之,冬令时间将缩短约一个月。之所以安排在周日,是为了便于生活的调整不至于受到较大的影响。
 
 
 
Linux 系统改变时区的命令  timeconfig
或者cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
 
2010年美国夏令时:美国2010年夏令时将于3月14日当地时间早上02:00开始
届时时间服务器会从01:59:59 自动跳到03:00:00
 

 

Linux下修改时间时区

 Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。

  Linux中的所有命令 (包括函数)都是采用的系统时钟设置。在Linux中,用于时钟查看和设置的命令主要有date、hwclock和clock。其中,clock和 hwclock用法相近,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
  1、date
  查看系统时间
  # date
  设置系统时间
  # date --set=“07/07/06 10:19" (月/日/年 时:分:秒)
  2、hwclock/clock
  查看硬件时间
  # hwclock --show
  # clock --show
  设置硬件时间
  # hwclock --set --date="07/07/06 10:19" (月/日/年 时:分:秒)
  # clock --set --date="07/07/06 10:19" (月/日/年 时:分:秒)
  3、硬件时间和系统时间的同步
  按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步,但是在不重新启动的时候,需要用hwclock或clock命令实现同步。
  硬件时钟与系统时钟同步:
  # hwclock --hctosys(hc代表硬件时间,sys代表系统时间)
  或者
  # clock --hctosys
  系统时钟和硬件时钟同步:
  # hwclock --systohc
  或者
  # clock --systohc
  4、时区的设置
  # tzselect
怎么设置linux时区
 
 
Local vs. UTC
首先重要的问题是你使用utc还是local time.
UTC(Universal Time Coordinated)=GMT(Greenwich Mean Time)
Local time 是你手表上的时间

传统的POSIX计算机(Solaris,bsd,unix)使用UTC格式
linux可以处理UTC时间和蹩脚的Windows所使用的local time

到底是使用UTC还是local time可以这样来确定:
如果机器上同时安装有Linux和Windows,建议使用local time
如果机器上只安装有Linux,建议使用utc
确定后编辑/etc/sysconfig/clock, UTC=0 是local time; UTC=1 是UTC(GMT)


确定timezone
运行tzselect,回答问题后会告诉你时区的名称,比如"Asia/Shanghai",把他记下来(后面我用$timezone代替)


设定timezone
# cp /usr/share/zoneinfo/$timezone /etc/localtime


重新启动或者运行时钟设置脚本使之发生作用


版本差异
由于发行版的差异,以上文件位置可能不同。
一般设置时钟所使用的启动脚本为/etc/rc.d/init.d/setclock
redhat是在/etc/rc.d/rc.sysinit中设置时钟,所以一般要重新启动
 
 
查看mysql的时区
 
mysql默认为操作系统的时区,查看命令:
SHOW VARIABLES LIKE '%time_zone%'
  • FROM_UNIXTIME(unix_timestamp) , FROM_UNIXTIME(unix_timestamp,format)

返回'YYYY-MM-DD HH:MM:SS'YYYYMMDDHHMMSS 格式值的unix_timestamp参数表示,

将UTC时间戳转换为本地时区时间格式

 

http://blog.sina.com.cn/s/blog_5482553a0100l6s8.html

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Next: arch invocation, Up: System context


21.1date: Print or set system date and time

Synopses:

     date [option]... [+format]
     date [-u|--utc|--universal] 
     [ MMDDhhmm[[CC]YY][.ss] ]

Invokingdatewith no formatargument is equivalent to invoking it with a default format that depends on theLC_TIMElocale category. In the default C locale, this format is ‘'+%a %b %e %H:%M:%S %Z %Y'’, so the output looks like ‘Thu Mar 3 13:47:51 PST 2005’.

Normally,dateuses the time zone rules indicated by theTZenvironment variable, or the system default rules ifTZis not set. See Specifying the Time Zone withTZ.

If given an argument that starts with a ‘+’,dateprints the current date and time (or the date and time specified by the--dateoption, see below) in the format defined by that argument, which is similar to that of the strftimefunction. Except for conversion specifiers, which start with ‘%’, characters in the format string are printed unchanged. The conversion specifiers are described below.

An exit status of zero indicates success, and a nonzero value indicates failure.


 http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html#date-invocation

 

posted @ 2012-08-21 00:04  CCJPP  阅读(422)  评论(0)    收藏  举报