title: jq convert timezone in bash script
body: <p>I have a query as below:</p>
  
  <pre><code>dump=$(jq -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                         (."5" | .buckets[] | "\(.key),",
                                         (."3" | .buckets[] | "\(.key),",
                                         (."4" | .buckets[] | "\(.key),",
                                         (."8" | .buckets[] | "\(.key),",
                                         (."6" | .buckets[] | "\(.key),",
                                         (."7" | .buckets[] | "\(.key),",
                                         (."9" | .buckets[] | "\(.key),",
                                         (."10" | .buckets[] | "\(.key),",
                                         (."11" | .buckets[] | "\(.key),",
                                         (."12" | .buckets[] | "\(.key),",
                                         (."13" | .buckets[] | "\(.key),",
                                         (."14" | .buckets[] | "\(.key),",
                                         (."15" | .buckets[] | "\(.key),",
                                         (."1" | ."hits" | .hits[] | ."_source" |
                                             "\(."@timestamp")" | "\n"))))))))))))))' &lt;&lt;&lt; "$cl_report")
  echo -e "${dump}" &gt;&gt; report.csv
  </code></pre>
  
  <p>timestamp is ISO8601 format.</p>
  
  <p>I want to convert the <strong>timestamp</strong> from UTC to Madrid time.</p>
  
  <p>What I want is something like <strong>this</strong>:</p>
  
  <pre><code>dump=$(jq -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                         (."5" | .buckets[] | "\(.key),",
                                         (."3" | .buckets[] | "\(.key),",
                                         (."4" | .buckets[] | "\(.key),",
                                         (."8" | .buckets[] | "\(.key),",
                                         (."6" | .buckets[] | "\(.key),",
                                         (."7" | .buckets[] | "\(.key),",
                                         (."9" | .buckets[] | "\(.key),",
                                         (."10" | .buckets[] | "\(.key),",
                                         (."11" | .buckets[] | "\(.key),",
                                         (."12" | .buckets[] | "\(.key),",
                                         (."13" | .buckets[] | "\(.key),",
                                         (."14" | .buckets[] | "\(.key),",
                                         (."15" | .buckets[] | "\(.key),",
                                         (."1" | ."hits" | .hits[] | ."_source" | "\ 
                                         (."@timestamp") as $tstamp | "TZ Europe/Madrid" date -d $tstamp" | \n"))))))))))))))' &lt;&lt;&lt; "$cl_report")
  </code></pre>
  
  <p>Bash throws error complaining about " and ' etc etc.</p>
  
  <p>What is the correct way of doing this conversion?</p>
  
  <p><strong>Ok. It seems there is a bug with jq 1.5. strftime etc does not read TZ properly in this version.</strong></p>
  
  <p>jq 1.6 can help:</p>
  
  <pre><code>report_dump=$(TZ=Erope/Madrid jq1.6 -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                         (."5" | .buckets[] | "\(.key),",
                                         (."3" | .buckets[] | "\(.key),",
                                         (."4" | .buckets[] | "\(.key),",
                                         (."8" | .buckets[] | "\(.key),",
                                         (."6" | .buckets[] | "\(.key),",
                                         (."7" | .buckets[] | "\(.key),",
                                         (."9" | .buckets[] | "\(.key),",
                                         (."10" | .buckets[] | "\(.key),",
                                         (."11" | .buckets[] | "\(.key),",
                                         (."12" | .buckets[] | "\(.key),",
                                         (."13" | .buckets[] | "\(.key),",
                                         (."14" | .buckets[] | "\(.key),",
                                         (."15" | .buckets[] | "\(.key),",
                                         (."1" | ."hits" | .hits[] | ."_source" | .["@timestamp"] | sub(".[0-9]+Z$"; "Z") | fromdateiso8601 | strflocaltime("%Y-%m-%dT%H:%M:%S %Z"), "\n"))))))))))))))' &lt;&lt;&lt; "$cl_report")
  </code></pre>
  
  <p>As shown above, I piped the stripped timestamp to fromdateiso8601 to get seconds and then piped into strflocaltime that utilizes TZ to get the local time.</p>