aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/convert-date-time-to-utc
blob: 1d503d3168a990d13967567d02c41c47d57f5aaf (about) (plain)
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
#!/bin/bash
###################################################################################################
# convert-date-time-to-utc
###################################################################################################
# Copyright 2017-2022 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License; you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Converting the parameter to UTC date time format and rounding up/down the seconds
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <date_time> contains the date time in the same format as the one returned by 
#               "stat -L -c %y <file>"
#
# - -<anything> will display this help message
#
###################################################################################################
#
# Returned values:
# ---------------
#
# - <utc_date_time> contains the date time in UTC format with the seconds rounded up/down
#
###################################################################################################
#
# Usage example:
# -------------
#
# utc_date_time=$(convert-date-time-to-utc "$(stat -L -c %y .driveignore)")
#
###################################################################################################
if [[ $LOG_CONVERT_DATE_TIME_TO_UTC == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi

date_time="$1"

# Checking for no parameter or parameter beginning with '-'
if [[ ($# -eq 0) || ($year =~ ^-) ]]; then
        end_of_help_message_line_number=$(awk '/^if \[\[ \$LOG_/{ print NR; exit }' /usr/bin/convert-date-time-to-utc)
        ((end_of_help_message_line_number--))
        awk -v var=$end_of_help_message_line_number 'NR >= 19 && NR <= var' /usr/bin/convert-date-time-to-utc | zenity --text-info --title "convert-date-time-to-utc help" --width 1150 --height 850
        exit 1
else
        date=$(echo $date_time | cut -d ' ' -f 1)
        date_day=$(echo $date | cut -d '-' -f 3)
        date_month=$(echo $date | cut -d '-' -f 2)
        date_year=$(echo $date | cut -d '-' -f 1)
        time=$(echo $date_time | cut -d ' ' -f 2)
        time_hours=$(echo $time | cut -d ':' -f 1)
        time_mns=$(echo $time | cut -d ':' -f 2)
        time_secs=$(echo $time | cut -d ':' -f 3)
        
        # Removing hypothetical leading 0 from most variables,
        # otherwise subsequent [[ ]] would consider numbers beginning with a zero as octal
        # [ ] does not, but [[ ]] offers more compatibility
        date_day=${date_day#0}
        date_month=${date_month#0}
        time_hours=${time_hours#0}
        time_mns=${time_mns#0}
        time_secs=${time_secs#0}

        # Initializing $last_day_current_month depending on $date_month
        case $date_month in
                # Months with 31 days
                1|8)
                        last_day_current_month=31
                        last_day_previous_month=31
                        ;;
                # Months with 31 days
                3|5|7|10|12)
                        last_day_current_month=31
                        last_day_previous_month=30
                        ;;
                # Months with 30 days
                4|6|9|11)
                        last_day_current_month=30
                        last_day_previous_month=31
                        ;;
                # February
                2)
                        # Is this a leap year?
                        if $(leap-year $date_year); then
                                last_day_current_month=29
                        else
                                last_day_current_month=28
                        fi
                        last_day_previous_month=31
                        ;;
                *)
                        zenity --error --title "Fatal error in convert-date_time-to-utc" --text="\tUnknown date format: $date_time\n${noamp_local_folder}/${noamp_object}\nPlease report that issue to: https://gitlab.com/jean-christophe-manciot/Drive/issues:\n\tdate_month: $date_month does not exist" --width 1200
                        exit 1
                        ;;
        esac                                
                
        # Rounding up/down $time_secs if there are nanoseconds
        if [[ $time_secs =~ '.' ]]; then
                if [[ $(echo $time_secs | cut -d '.' -f 2 | cut -c1-1) -ge 5 ]]; then
                        # Removing all nanoseconds
                        time_secs=$(echo $time_secs | cut -d '.' -f 1)
                        # Rounding up the seconds
                        ((time_secs++))
                        if [[ $time_secs -eq 60 ]]; then
                                time_secs=0
                                ((time_mns++))
                                if [[ $time_mns -eq 60 ]]; then
                                        time_mns=0
                                        ((time_hours++))
                                        if [[ $time_hours -eq 24 ]]; then
                                                time_hours=0
                                                ((date_day++))
                                                if [[ $date_day -gt $last_day_current_month ]]; then
                                                        date_day=1
                                                        ((date_month++))
                                                        if [[ $date_month -gt 12 ]]; then
                                                                date_month=1
                                                                ((date_year++))
                                                        fi
                                                fi                                        
                                        fi                                        
                                fi                                        
                        fi
                else
                        # Rounding down the seconds
                        # Removing all nanoseconds
                        time_secs=$(echo $time_secs | cut -d '.' -f 1)
                fi
        fi

        # Levelling to UTC: date_time can be below (-), equal (+0000) or above (+) UTC
        if [[ $(echo $date_time | cut -d ' ' -f 3) =~ - ]]; then
                below_utc_offset=$(echo $date_time | cut -d ' ' -f 3 | cut -d '-' -f 2)
                below_utc_offset_hours=${below_utc_offset:0:2}
                below_utc_offset_mns=${below_utc_offset:2:2}
                utc_time_hours=`expr $time_hours + $below_utc_offset_hours`
                utc_time_mns=`expr $time_mns + $below_utc_offset_mns`
        elif [[ "$ldate_time" =~ \+0000 ]]; then
                # The job is already done
                utc_date_year=$date_year
                utc_date_month=$date_month
                utc_date_day=$date_day
                utc_time_hours=$time_hours
                utc_time_mns=$time_mns
        elif [[ $(echo $date_time | cut -d ' ' -f 3) =~ \+ ]]; then
                above_utc_offset=$(echo $date_time | cut -d ' ' -f 3 | cut -d '+' -f 2)
                above_utc_offset_hours=${above_utc_offset:0:2}
                above_utc_offset_mns=${above_utc_offset:2:2}
                utc_time_hours=`expr $time_hours - $above_utc_offset_hours`
                utc_time_mns=`expr $time_mns - $above_utc_offset_mns`
        else
                zenity --error --title "Fatal error in convert-date_time-to-utc" --text="\tUnknown date format: $date_time\n${noamp_local_folder}/${noamp_object}\nPlease report that issue to: https://gitlab.com/jean-christophe-manciot/Drive/issues:\n\tdate_time neither + nor -" --width 1200
                exit 1
        fi

        # More job to do if $date_time is not originally UTC
        if [[ ! "$ldate_time" =~ \+0000 ]]; then
                # If the adjusted time goes beyond 24
                if [[ $utc_time_hours -ge 24 ]]; then
                        utc_time_hours=`expr $utc_time_hours - 24`
                        utc_date_day=`expr $date_day + 1`
                # If the adjusted time goes below 0
                elif [[ "$utc_time_hours" =~ - ]]; then
                        utc_time_hours=`expr $utc_time_hours + 24`
                        utc_date_day=`expr $date_day - 1`
                else
                        # time_hours & date_day remain untouched
                        utc_date_day=$date_day
                fi
                
                # If the adjusted day goes beyond $last_day_current_month
                if [[ $utc_date_day -gt $last_day_current_month ]]; then
                        utc_date_day=`expr $utc_date_day - $last_day_current_month`
                        utc_date_month=`expr $date_month + 1`
                # If the adjusted day goes below 1
                elif [[ $utc_date_day -eq 0 ]]; then
                        utc_date_day=$last_day_previous_month
                        utc_date_month=`expr $date_month - 1`
                else
                        # date_day & date_month remain untouched
                        utc_date_month=$date_month
                fi
                        
                # If the adjusted month goes beyond 12
                if [[ $utc_date_month -gt 12 ]]; then
                        utc_date_month=`expr $utc_date_month - 12`
                        utc_date_year=`expr $date_year + 1`
                # If the adjusted month goes below 1
                elif [[ $utc_date_month -eq 0 ]]; then
                        utc_date_month=12
                        utc_date_year=`expr $date_year - 1`
                else
                        # date_month & date_year remain untouched
                        utc_date_year=$date_year
                fi
                        
                if [[ "$utc_date_year" =~ - ]]; then
                        zenity --error --title "Fatal error in convert-date_time-to-utc" --text="\tUnknown date format: $date_time\n${noamp_local_folder}/${noamp_object}\nPlease report that issue to: https://gitlab.com/jean-christophe-manciot/Drive/issues:\n\tutc_date_year: $utc_date_year contains -" --width 1200
                        exit 1
                fi
        fi

        # Building whole UTC-adjusted date time
        utc_date="${utc_date_year}-${utc_date_month}-${utc_date_day}"
        utc_time="${utc_time_hours}:${utc_time_mns}:${time_secs}"
        utc_date_time="${utc_date} ${utc_time}"
        
        # Returning the converted date_time in UTC
        echo "$utc_date_time"
fi