forked from hebruce110/BashShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.sh
439 lines (439 loc) · 9.85 KB
/
Date.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/sh
# Copyright (c) 2010 codingstandards. All rights reserved.
# file: datetime.sh
# description: Bash中关于日期时间操作的常用自定义函数
# license: LGPL
# author: codingstandards
# email: [email protected]
# version: 1.0
# date: 2010.02.27
# usage: yesterday
# 昨天
# 比如今天是2010年2月27日,那么结果就是2010-02-26
yesterday()
{
date --date='1 day ago' +%Y-%m-%d
}
# usage: today
# 今天
# 比如今天是2010年2月27日,那么结果就是2010-02-27
today()
{
date +%Y-%m-%d
}
# usage: now
# 现在,包括日期和时间、纳秒
# 比如:2010-02-27 11:29:52.991774000
now()
{
date "+%Y-%m-%d %H:%M:%S.%N"
}
# usage: curtime
# 当前时间,包括日期和时间
# 比如:2010-02-27 11:51:04
curtime()
{
date '+%Y-%m-%d %H:%M:%S'
# 也可写成:date '+%F %T'
}
# usage: last_month
# 取上个月的年月
# 比如:2010-01
last_month()
{
date --date='1 month ago' '+%Y-%m'
}
# usage: last_month_packed
# 取上个月的年月
# 比如:201001
last_month_packed()
{
date --date='1 month ago' '+%Y%m'
}
# usage: first_date_of_last_month
# 取上个月的第一天
# 比如本月是2010年2月,那么结果就是2010-01-01
first_date_of_last_month()
{
date --date='1 month ago' '+%Y-%m-01'
}
# usage: last_date_of_last_month
# 取上个月的最后一天
# 比如当前是2010年2月,那么结果就是2010-01-31
last_date_of_last_month()
{
date --date="$(date +%e) days ago" '+%Y-%m-%d'
}
# usage: day_of_week
# 今天的星期
# day of week (0..6); 0 represents Sunday
day_of_week()
{
date +%w
}
# usage: last_hour
# 上个小时
# 比如:2010-02-27-10
# 适合处理log4j生成的日志文件名
last_hour()
{
date --date='1 hour ago' +%Y-%m-%d-%H
}
# usage: the_hour
# 当前的小时,为方便算术比较,结果不以0开头
# 比如:12
the_hour()
{
#date +%H # hour (00..23)
date +%k # hour ( 0..23)
}
# usage: the_minute
# 当前的分钟,为方便算术比较,结果不以0开头
# 比如:
the_minute()
{
MM=$(date +%M) # minute (00..59)
echo $[1$MM-100]
}
# usage: the_second
# 当前的秒数
# 比如:
the_second()
{
SS=$(date +%S) # second (00..60); the 60 is necessary to accommodate a leap second
echo $[1$SS-100]
}
# usage: the_year
# 当前的年份 year (1970...)
# 比如:2010
the_year()
{
date +%Y
}
# usage: the_month
# 当前的月份,为方便算术比较,结果不以0开头
# 比如:2
the_month()
{
M=$(date +%m) # month (01..12)
echo $[1$M-100]
}
# usage: the_date
# 当前的日期,为方便算术比较,结果不以0开头
# 比如:27
the_date()
{
date +%e # day of month, blank padded ( 1..31)
}
# usage: days_ago
# 取n天前的日期
# 比如:days_ago 0就是今天,days_ago 1就是昨天,days_ago 2就是前天,days_ago -1就是明天
# 格式:2010-02-27
days_ago()
{
date --date="$1 days ago" +%Y-%m-%d
}
# usage: chinese_date_and_week()
# 打印中文的日期和星期
# 比如:2月27日 星期六
chinese_date_and_week()
{
WEEKDAYS=(星期日 星期一 星期二 星期三 星期四 星期五 星期六)
WEEKDAY=$(date +%w)
#DT="$(date +%Y年%m月%d日) ${WEEKDAYS[$WEEKDAY]}"
MN=1$(date +%m)
MN=$[MN-100]
DN=1$(date +%d)
DN=$[DN-100]
DT="$MN月$DN日 ${WEEKDAYS[$WEEKDAY]}"
echo "$DT"
}
# usage: rand_digit
# 随机数字,0-9
rand_digit()
{
S="$(date +%N)"
echo "${S:5:1}"
}
# usage: seconds_of_date [ [<time>]]
# 获取指定日期的秒数(自1970年)
# 比如:seconds_of_date "2010-02-27" 返回 1267200000
seconds_of_date()
{
if [ "$1" ]; then
date -d "$1 $2" +%s
else
date +%s
fi
}
# usage: date_of_seconds
# 根据秒数(自1970年)得到日期
# 比如:date_of_seconds 1267200000 返回 2010-02-27
date_of_seconds()
{
date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d"
}
# usage: datetime_of_seconds
# 根据秒数(自1970年)得到日期时间
# 比如:datetime_of_seconds 1267257201 返回 2010-02-27 15:53:21
datetime_of_seconds()
{
date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d %H:%M:%S"
}
# usage: leap_year
# 判断是否闰年
# 如果yyyy是闰年,退出码为0;否则非0
# 典型示例如下:
# if leap_year 2010; then
# echo "2010 is leap year";
# fi
# if leap_year 2008; then
# echo "2008 is leap year";
# fi
# 摘自脚本:datetime_util.sh (2007.06.11)
# 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年份,现改成通过参数指定)
# Shell program to read any year and find whether leap year or not
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
leap_year()
{
# store year
yy=$1
isleap="false"
#echo -n "Enter year (yyyy) : "
#read yy
# find out if it is a leap year or not
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of isleap
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
isleap="true"
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of isleap
else
# it is a leap year
isleap="true"
fi
#echo $isleap
if [ "$isleap" == "true" ]; then
# echo "$yy is leap year"
return 0
else
# echo "$yy is NOT leap year"
return 1
fi
}
# usage: validity_of_date<dd># 判断yyyy-mm-dd是否合法的日期
# 如果是,退出码为0;否则非0
# 典型示例如下:
# if validity_of_date 2007 02 03; then
# echo "2007 02 03 is valid date"
# fi
# if validity_of_date 2007 02 28; then
# echo "2007 02 28 is valid date"
# fi
# if validity_of_date 2007 02 29; then
# echo "2007 02 29 is valid date"
# fi
# if validity_of_date 2007 03 00; then
# echo "2007 03 00 is valid date"
# fi
# 摘自脚本:datetime_util.sh (2007.06.11)
# 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年月日,现改成通过参数指定)
# Shell program to find the validity of a given date
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
validity_of_date()
{
# store day, month and year
yy=$1
mm=$2
dd=$3
# store number of days in a month
days=0
# get day, month and year
#echo -n "Enter day (dd) : "
#read dd
#echo -n "Enter month (mm) : "
#read mm
#echo -n "Enter year (yyyy) : "
#read yy
# if month is negative ( # then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ]; then
#echo "$mm is invalid month."
return 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
01) days=31;;
2) days=28 ;;
02) days=28 ;;
3) days=31 ;;
03) days=31 ;;
4) days=30 ;;
04) days=30 ;;
5) days=31 ;;
05) days=31 ;;
6) days=30 ;;
06) days=30 ;;
7) days=31 ;;
07) days=31 ;;
8) days=31 ;;
08) days=31 ;;
9) days=30 ;;
09) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
#echo $days
# if day is negative ( # that months days then day is invaild
if [ $dd -le 0 -o $dd -gt $days ]; then
#echo "$dd day is invalid"
return 3
fi
# if no error that means date dd/mm/yyyy is valid one
#echo "$dd/$mm/$yy is a vaild date"
#echo "$yy-$mm-$dd is a valid date"
#echo "valid"
return 0
}
# usage: days_of_month
# 获取yyyy年mm月的天数,注意参数顺序
# 比如:days_of_month 2 2007 结果是28
days_of_month()
{
# store day, month and year
mm=$1
yy=$2
# store number of days in a month
days=0
# get day, month and year
#echo -n "Enter day (dd) : "
#read dd
#echo -n "Enter month (mm) : "
#read mm
#echo -n "Enter year (yyyy) : "
#read yy
# if month is negative ( # then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ]; then
#echo "$mm is invalid month."
echo -1
return 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
01) days=31;;
2) days=28 ;;
02) days=28 ;;
3) days=31 ;;
03) days=31 ;;
4) days=30 ;;
04) days=30 ;;
5) days=31 ;;
05) days=31 ;;
6) days=30 ;;
06) days=30 ;;
7) days=31 ;;
07) days=31 ;;
8 ) days=31 ;;#防止博客表情化,多加了一个空格
08) days=31 ;;
9) days=30 ;;
09) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
echo $days
}