(Video materials in preparation)
calclock : Calculates date and time (calendar clock)
Usage : calclock <f1> <f2> <f3>... filename
Options : -r
--dst
Version : Tue Jan 9 09:02:34 JST 2024
Edition : 2
This tool takes a date strings from specified fields in a file read
in from a specified file or standard input, and add the number of
seconds since 1 Jan 1970 (UNIX Time). Used to convert dates or
times to decimal number in order to perform calculations on them.
The date string must be in one of the following three formats:
YYYYMMDD (8 digits), YYYYMMDDhhmm (12 digits) or YYYYMMDDhhmmss (14
digits).
The converted value can be further converted to minutes by dividing
by 60, to hours by dividing by 3600 or to days by dividing by 86400.
If -r option is specified, reverse conversion (UNIX Time to
YYYYMMDDhhmm conversion) is performed.
If --dst option is specified, conversion is performed considering
daylight saving time.
(Original Data)
$ cat data1
0001 0000007 20160201 20160206 117 8335 -145
0001 0000007 20160203 20160206 221 15470 0
0001 0000007 20160205 20160206 85 5950 0
0001 0000007 20160206 20160206 293 20527 -17
0001 0000007 20160207 20160206 445 31150 0
0002 0000007 20160208 20160206 150 11768 -1268
0002 0000007 20160209 20160206 588 41160 0
0002 0000007 20160210 20160206 444 31080 0
Convert the 3rd and 4th fields, calculate the time difference
between the two and express the result in number of days.
$ calclock 3 4 < data1 |
tee data1_1 |
lcalc '($6 - $4) / 86400' > data1_2
$ cat data1_1
0001 0000007 20160201 1454252400 20160206 1454684400 117 8335 -145
0001 0000007 20160203 1454425200 20160206 1454684400 221 15470 0
0001 0000007 20160205 1454598000 20160206 1454684400 85 5950 0
0001 0000007 20160206 1454684400 20160206 1454684400 293 20527 -17
0001 0000007 20160207 1454770800 20160206 1454684400 445 31150 0
0002 0000007 20160208 1454857200 20160206 1454684400 150 11768 -1268
0002 0000007 20160209 1454943600 20160206 1454684400 588 41160 0
0002 0000007 20160210 1455030000 20160206 1454684400 444 31080 0
$ cat data1_2
5
3
1
0
-1
-2
-3
-4
-r option(reverse conversion):
The value in the specified field which expresses the number of
seconds since 1 Jan 1970 (UNIX Time) is converted to a 14-digit
number in the format YYYYMMDDhhmmss. To convert other format, use
self, dayslash or etc.
(Original Data)
$ cat data2
0001 0000007 201602010900 117 8335 -145
0001 0000007 201602031030 221 15470 0
0001 0000007 201602051200 85 5950 0
0001 0000007 201602061300 293 20527 -17
0001 0000007 201602071430 445 31150 0
0002 0000007 201602081500 150 11768 -1268
0002 0000007 201602091830 588 41160 0
0002 0000007 201602102000 444 31080 0
Return the time that is 12 hours later than the time in the third
field.
$ calclock 3 data2 |
lcalc '$4 + 12 * 3600' |
tee data2_1 |
calclock -r 1 |
tee data2_2 |
self 2.1.12 |
tee data2_3 |
dayslash --output yyyy/mm/dd-HH:MM 1 > data2_4
(Before calclock -r)
$ cat data2_1
1454328000
1454506200
1454684400
1454774400
1454866200
1454954400
1455053400
1455145200
(After calclock -r)
$ cat data2_2
1454328000 20160201210000
1454506200 20160203223000
1454684400 20160206000000
1454774400 20160207010000
1454866200 20160208023000
1454954400 20160209030000
1455053400 20160210063000
1455145200 20160211080000
(Time that is 12 hours later)
$ cat data2_3
201602012100
201602032230
201602060000
201602070100
201602080230
201602090300
201602100630
201602110800
(After shaping)
$ cat data2_4
2016/02/01-21:00
2016/02/03-22:30
2016/02/06-00:00
2016/02/07-01:00
2016/02/08-02:30
2016/02/09-03:00
2016/02/10-06:30
2016/02/11-08:00
--dst option
Conversion is performed considering daylight saving time.
(Original Data)
$ cat data3
19480501010000 19480502010000
Calculate difference between the 1st field snd th 2nd field in hours.
$ calclock --dst 1 2 data3 |
lcalc '$2 / 3600, $4 / 3600' |
tee data3_1 |
lcalc '$2 - $1' > data3_2
$ cat data3_1
-189968 -189945
$ cat data3_2
23
The range allowed for YYYY is from 1900 to 9999. If YYYY is not in
the range, result is "xxxxxxxxxx" (normal conversion)
or "xxxxxxxxxxxxxx" (reverse conversion).
Because UNIX Time is based on UTC, both conversions are affected the
time zone and TZ environment variable. For example, the time zone is
Tokyo/JAPAN (9 hours earlier than UTC).
$ echo 19700101 | calclock 1
19700101 -32400
$ export TZ=0; echo 19700101 | calclock 1
19700101 0
If calculation on dates without time like Example 1, mdate command
is much faster.
When --dst option is specfile, conversion becomes very slow because
conversion is done by localtime(2) or mktime(2). Moreover, in 32
bit environment, conversion range is restricted from 19011214054552
to 20380119121407 (JST).
# 20220805 description of --dst option added. Katayama