-
Notifications
You must be signed in to change notification settings - Fork 10
/
utconv
executable file
·214 lines (189 loc) · 6.92 KB
/
utconv
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
#!/bin/sh
######################################################################
#
# UTCONV - A Converter Between UNIX-time and Calendar-time
#
# USAGE: utconv [datetime_text_or_file] # for Real-datetime -> Unixtime
# utconv -r [unixtime_text_or_file] # for Unixtime -> Real-datetime
#
# * utconv reads stdin when <datetime_text_or_file> is not given.
# * datetime text must be one of the following format
# - YYMMDD ........... Real Date (2digits-year)
# - YYYYMMDD ......... Real Date (4digits-year)
# - YYMMDDhhmmss ..... Real DateTime (2digits-year)
# - YYYYMMDDhhmmss ... Real DateTime (4digits-year)
# - n ................ UNIX time
# * utconv treats the Real DateTime as a localtime. When you want to be
# treated as a UTC. You can use the TZ environment variable, like this
# > TZ=UTC+0 utconv -r 1
# 19700101000001
#
# Return: 0 (always except helpmessage)
#
# * Any invalid time make me return the text "-1" with the return code 0.
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage ========================
print_usage_and_exit () {
cat <<-USAGE 1>&2
USAGE : ${0##*/} [datetime_text_or_file] # Cal.-time -> Unixtime
${0##*/} -r [unixtime_text_or_file] # Unixtime -> Cal.-time
Args : datetime_text_or_file ... A time to converting
Options : -r ...................... Swithes mode to Unixtime -> Cal.-time
Version : 2020-05-06 22:42:19 JST
(POSIX Bourne Shell/POSIX commands)
USAGE
exit 1
}
######################################################################
# Parse Arguments
######################################################################
# === print the usage and exit if required ===========================
case "${1:-}" in --version|--help|-h) print_usage_and_exit;; esac
######################################################################
# Main Routine
######################################################################
# === Convert ========================================================
exec awk '
BEGIN {
# 1) initialize
unixtime2YYYYMMDDhhmmss_init("LOCALTIME"); # initialize with localtime mode
revmode = 0;
fromstdin = 1;
# 2) parse arguments
for (i=1; i<ARGC; i++) {
if (ARGV[i] == "-r") {
revmode = 1;
continue;
}
fromstdin = 0;
if ((ARGV[i] == "-") && (i == ARGC-1)) {
fromstdin = 1;
break;
} else if ((revmode == 0) && match(ARGV[i], /^[0-9]+$/)) {
printf("%d\n",YYYYMMDDhhmmss2unixtime(ARGV[i]));
} else if (revmode == 0) {
while (getline < ARGV[i]) {
printf("%d\n",YYYYMMDDhhmmss2unixtime($1));
}
} else if ((revmode > 0) && match(ARGV[i], /^-?[0-9]+$/)) {
print unixtime2YYYYMMDDhhmmss(ARGV[i]);
} else {
while (getline < ARGV[i]) {
print unixtime2YYYYMMDDhhmmss($1);
}
}
}
# 3) convert data from the stdin if necessary
if ((fromstdin > 0) && (revmode == 0)) {
while ("cat" | getline) {
printf("%d\n",YYYYMMDDhhmmss2unixtime($1));
}
} else if (fromstdin > 0) {
while ("cat" | getline) {
print unixtime2YYYYMMDDhhmmss($1);
}
}
}
function unixtime2YYYYMMDDhhmmss_init(localtime_flag, gm,lo) {
max_calced_year = 1970; # To remember every days on 01/01 from
days_on_Jan1st_from_epoch[1970] = 0; # the Epoch which was calculated once
split("31 0 31 30 31 30 31 31 30 31 30 31", days_of_month);
if (localtime_flag == "LOCALTIME") {
gm = YYYYMMDDhhmmss2unixtime("'$(TZ=UTC+0 date '+%Y%m%d%H%M%S')'");
lo = YYYYMMDDhhmmss2unixtime("'$( date '+%Y%m%d%H%M%S')'");
offset = lo - gm;
offset -= (offset%2); # calcell the time lag of the two date starting time
} else {
offset = 0;
}
}
function unixtime2YYYYMMDDhhmmss(ut, Y,M,D,h,m,s,t,i,j) {
# 0) timezone adjustment
ut += offset;
if (ut < 0) {return -1;}
# 1) calculate hour,minute,second and number of days from the Epoch
s = ut % 60; t = int(ut/60);
m = t % 60; t = int( t/60);
h = t % 24;
days_from_epoch = int( t/24);
# 2) calculate year
Y = int(days_from_epoch/365.2425)+1970+1;
if (Y > max_calced_year) {
i = days_on_Jan1st_from_epoch[max_calced_year];
for (j=max_calced_year; j<Y; j++) {
i += (j%4!=0)?365:(j%100!=0)?366:(j%400!=0)?365:366;
days_on_Jan1st_from_epoch[j+1] = i;
}
max_calced_year = Y;
}
for (;;Y--) {
if (days_from_epoch >= days_on_Jan1st_from_epoch[Y]) {
break;
}
}
# 3) calculate month,day
days_of_month[2] = (Y%4!=0)?28:(Y%100!=0)?29:(Y%400!=0)?28:29;
D = days_from_epoch - days_on_Jan1st_from_epoch[Y] + 1;
for (M=1; ; M++) {
if (D > days_of_month[M]) {
D -= days_of_month[M];
} else {
break;
}
}
return sprintf("%04d%02d%02d%02d%02d%02d",Y,M,D,h,m,s);
}
function YYYYMMDDhhmmss2unixtime(YYYYMMDDhhmmss, Y,M,D,h,m,s,l) {
# 1) seperate the units
l = length(YYYYMMDDhhmmss);
if (l < 5) { # invalid
return -1;
} else if (l < 8) { # YYMMMDD only
Y = substr(YYYYMMDDhhmmss, 1,l-4)*1+int('$(date '+%Y')'/100)*100;
M = substr(YYYYMMDDhhmmss,l-3, 2)*1;
D = substr(YYYYMMDDhhmmss,l-1, 2)*1;
h = 0; m = 0; s = 0;
} else if (l < 12) { # YYYYMMDD only
Y = substr(YYYYMMDDhhmmss, 1,l-4)*1;
M = substr(YYYYMMDDhhmmss,l-3, 2)*1;
D = substr(YYYYMMDDhhmmss,l-1 )*1;
h = 0; m = 0; s = 0;
} else { # YYYYMMDDhhmmss
Y = substr(YYYYMMDDhhmmss, 1,l-10)*1;
M = substr(YYYYMMDDhhmmss,l-9, 2)*1;
D = substr(YYYYMMDDhhmmss,l-7, 2)*1;
h = substr(YYYYMMDDhhmmss,l-5, 2)*1;
m = substr(YYYYMMDDhhmmss,l-3, 2)*1;
s = substr(YYYYMMDDhhmmss,l-1 )*1;
}
# 2) validate
if ((s>60) || (m>59) || (h>23) || (M>12)) {return -1;}
days_of_month[2] = (Y%4!=0)?28:(Y%100!=0)?29:(Y%400!=0)?28:29;
if (D > days_of_month[M] ) {return -1;}
# 3) adjust the value of year and month
if (M<3) {M+=12; Y--;}
# 4) calculate unixtime
return (365*Y+int(Y/4)-int(Y/100)+int(Y/400)+int(306*(M+1)/10)-428+D-719163)*86400+(h*3600)+(m*60)+s-offset;
}
' ${1+"$@"}