Sorry if this has been asked before.
I have a text file that contain a field with GMT time on Unix. I want to convert that field to the PST time zone in the same format.
source file test.txt
20200804T221806.214 GMT,2003060015,2003060018
20200804T232027.571 GMT,2005260045,2005260095
20200804T232027.572 GMT,2005260045,2005260095
20200805T000119.715 GMT,2005290022C,2005290042D
20200805T000119.715 GMT,2005290022C,2005290042D
20200801T000326.111 GMT,2005290028C,2005290050D
20200101T000326.111 GMT,2005290028C,2005290050D
20000101T000326.111 GMT,2005290028C,2005290050D
Desired result
20200804T151806.214 PST,2003060015,2003060018
20200804T162027.571 PST,2005260045,2005260095
20200804T162027.572 PST,2005260045,2005260095
20200804T000119.715 PST,2005290022C,2005290042D
20200804T000119.715 PST,2005290022C,2005290042D
20200731T000326.111 PST,2005290028C,2005290050D
20191231T000326.111 PST,2005290028C,2005290050D
19991231T000326.111 PST,2005290028C,2005290050D
This is my current awk code and got the result. However, this solution only works if all months have the same 31 days. I feel like there should be a much simpler solution for this and not necessary to use awk.
awk -F ',' '{ sCCYY = substr($1,1,4); sMM = substr($1,5,2); sDD = substr($1,7,2); stime = substr($1,10,6) - 70000; if (stime < 0) { sDD = sDD - 1; stime = substr($1,10,6); if (sDD <= 0) { sMM = sMM - 1; sDD = 31; if (sMM <= 0) { sCCYY = sCCYY - 1; sMM = 12; } } } printf"%04s%02s%02s%06s%s%s%s\n",sCCYY,sMM,sDD,"T" stime,substr($1,16,4) " PST,",$2 ",",$3; }' test.txt
result:
20200804T151806.214 PST,2003060015,2003060018
20200804T162027.571 PST,2005260045,2005260095
20200804T162027.572 PST,2005260045,2005260095
20200804T000119.715 PST,2005290022C,2005290042D
20200804T000119.715 PST,2005290022C,2005290042D
20200731T000326.111 PST,2005290028C,2005290050D
20191231T000326.111 PST,2005290028C,2005290050D
19991231T000326.111 PST,2005290028C,2005290050D
Please share if you know a better way to convert it. Thanks in advance!