日期处理脚本与Windows 10安装Bash指南
一、GNU date的优势
GNU date在日期处理方面表现出色。安装GNU date后,一些日期计算变得十分简单,例如判断某一年是否为闰年,只需使用如下代码:
if [ $( date 12/31/$year +%j ) -eq 366 ]
如果一年的最后一天是该年的第366天,那么这一年就是闰年。此外,GNU date还能处理较早的日期,而标准Unix date命令的“时间零点”是1970年1月1日00:00:00 UTC,无法处理早于这个时间的日期。
二、查找过去特定日期是星期几
以下是 dayinpast 脚本的代码:
#!/bin/bash
# dayinpast--Given a date, reports what day of the week it was
if [ $# -ne 3 ] ; then
echo "Usage: $(basename $0) mon day year" >&2
echo " with just numerical values (ex: 7 7 1776)" >&2
exit 1
fi
date --version > /dev/null 2>&1 # Discard error, if any.
baddate="$?" # Just loo
超级会员免费看
订阅专栏 解锁全文
1

被折叠的 条评论
为什么被折叠?



