Linux 时间&时区

 

shell没有专门的时间转换函数,更没有oop之类的类方法,不过操作时间还是很简单的

 

date:
date 直接后跟格式化字符串默认显示当前时区的当前时间

  1. %a abbreviated weekday 简写的weekday
  2. %A full weekday name  全写weekday
  3. %b abbreviated month name 简写month
  4. %B full month name 全写month name

     

  5. %c locale's DATE and TIME
  6. %F full date, same as %Y-%m-%d

  7. %j day of year,今天是今年的第几天
  8. %s seconds since 1970-01-01 00:00:00 UTC  注意时区
  9. %S 当前秒数(00..60)
  10. %T full time, same as%H:%M:%S
  11. %z  +hhmm numeric time zone (e.g. -0400)
  12. %Z alphebatic time zone abbreviation (e.g. EDT)

  

 

 

 

Options:

 

  1. date 后跟+%FORMAT_STRING来格式化时间
    date '+The Current Datetime: %c'

     

     

  2. --date=STRING  Display time described by string not now
    进行时间计算
    date -d '1 second' +%c  # 1 second later
    date -d '-1 second' +%c  # 1 second ago
    date -d '1 minute' +%c  # 1 minute later
    date -d '-1 minute' +%c  # 1 minute ago
    date -d '1 hour' +%c  # 1 hour later
    date -d '-1 hour' +%c  # 1 hour ago
    date -d '1 day' +%c  # 1 day later
    date -d '-1 day' +%c  # 1 day ago
    date -d '1 week' +%c  # 1 week later
    date -d '-1 week' +%c  # 1 week ago
    date -d '1 month' +%c  # 1 month later
    date -d '-1 month' +%c  # 1 month ago
    date -d '1 year' +%c  # 1 year later
    date -d '-1 year' +%c  # 1 year ago

    -d "@timestamp" 转换时间戳

     @后跟时间戳即可

  3. --reference 显示文件最后修改时间
    date --reference file

     

     

  4. --utc 显示UTC时间

     

     

  5. --set 设置时间

    date --set 1:1:1  # 设置时间,日期保持不变
    date -s '1999:1:1 1:1'  # 同时修改日期和时间

     

  6. 计算时间脚本
    s=`date +'%H:%M:%S'`
    echo -e Incept Time: "\t$s"
    sleep 2
    e=`date +'%H:%M:%S'`
    echo -e Closure Time: "\t$e"
    
    s=$(date --date=$s +%s)
    e=$(date --date=$e +%s)
    
    echo -e Elapsed: "\t$[e-s]" seconds
    
    s=`date +%s`
    nmap -v -A -sV -F scanme.nmap.org
    e=`date +%s`
    difference=$((e-s))
    echo -e Elapsed: "\t$difference"

     

 

Linux设定时区:

  1. tzselect命令
  2. 覆盖/etc/localtime软连接
    ln -svfn /usr/share/zoneinfo/America/New_York /etc/localtime

    一定要连接正确,如果/etc/localtime没有连接到正确时区,会显示UTC时间

     

     

  3. 设置系统变量TZ,该变量默认没有设置,此变量优先级最高
    echo 'export TZ="Asia/Shanghai"' >> ~/.bashrc
    source ~/.bashrc

     

     

  4. 通过timedatectl
    systemd默认有一个systemd类型的static服务

     

     

    timedatectl status

     

     

    timedatectl set-timezone 'America/New_York'

    优先级依然低于TZ变量

 

clock  hwclock  RTC

 

 

   

 

man  rtc

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

posted @ 2020-11-25 14:40  ascertain  阅读(470)  评论(0编辑  收藏  举报