Using GNU awk, you can do this by manipulating the TZ
environment variable. The format is a bit nasty when you are dealing with time-offsets only, but if you have the std-string that specifies the time-zone, it is straightforward.
In awk, the environment is stored in the array ENVIRON
. Modificaion of ENVIRON
is implementation defined:
ENVIRON
: An array representing the value of the environment, as described in the exec functions defined in the System Interfaces volume of POSIX.1-2017. The indices of the array shall be strings consisting of the names of the environment variables, and the value of each array element shall be a string consisting of the value of that variable. If appropriate, the environment variable shall be considered a numeric string (see Expressions in awk); the array element shall also have its numeric value.
In all cases where the behaviour of awk is affected by environment variables (including the environment of any commands that awk executes via the system function or via pipeline redirections with the print statement, the printf
statement, or the getline
function), the environment used shall be the environment at the time awk began executing; it is implementation-defined whether any modification of ENVIRON affects this environment.
source: POSIX.1-2017
GNU awk, on the other hand, states the following:
However, beginning with version 4.2, if not in POSIX compatibility mode, gawk does update its own environment when ENVIRON
is changed, thus changing the environment seen by programs that it creates.
So this can now be exploited by doing somethinglike this:something like:
ENVIRON["TZ"] = std offset
Here are a couple of examples:
ENVIRON["TZ"] = "UTC"
ENVIRON["TZ"] = "UTC+03:00"
ENVIRON["TZ"] = "CET"
With respect to the OP, we can do this:
awk 'BEGIN{FS=OFS=","}
{time=$2; gsub(/[^0-9]/," ",time); tz=$2; gsub(/^.* /,"",tz)}
{ENVIRON["TZ"]=tz; print $0,strftime("%F %T",mktime(time),1)}
' file
Note: for CSV files, you should use What's the most robust way to efficiently parse CSV using awk?
Note: sadly, not all known forms of TZ definitions are recognized.