I have put some pseudocode in a listings object, with a caption and box around it. I have used the fancybox solution to listings objects suggested here: How to center a lstlisting
Everything is fine, except the caption isn't centered.
Here's the output:

Here's the code:
\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}
\usepackage{fourier} % Use the Adobe Utopia font for the document
\usepackage[english]{babel} % English language/hyphenation
\usepackage{listings}
\lstset{numbers=left, numbersep=10pt, frame = single, framexleftmargin=17pt}
\usepackage{fancybox}
\makeatletter
\newenvironment{CenteredBox}{%
\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered
\makeatother
\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps
\begin{document}
\section{Chapter}
\begin{CenteredBox}
\begin{lstlisting}[caption={Cool code},label=cool,linewidth=6in]
validity_type <- 0,1,2
uid <- '00000'
start_date <- '2014-11-20 00:00:00'
end_date <- '2014-11-21 00:00:00'
sql_query <- "SELECT variable_1
, variable_2
, variable_3
FROM prices
WHERE validity_type = ",validity_type," AND
uid = '",uid,"'
GROUP BY variable_1, variable_2, variable_3
HAVING from_ts >= '",start_date,"' AND from_ts < '",end_date,"'
ORDER BY variable_1 ASC, variable_2 ASC")
\end{lstlisting}
\end{CenteredBox}
\end{document}
Any help is much appreciated.
