If you have $n$ function values and $n$ derivative values, then you have $2n$ pieces of data that you need to interpolate. A $C_2$ cubic spline with breaks at your data points has only (roughly) $n$ degrees of freedom, so clearly it's not going to work.
There are many ways to get what you want. I suggest you start by reading about Hermite interpolation.
A search for "quintic Hermite spline" produced many results, including some that provide code.
If you want to stick with cubics, you will have to introduce additional breaks to get the additional degrees of freedom that you need. Maybe using two cubic pieces in between each pair of data points would work. As long as you have the right number of degrees of freedom, you will probably get a linear system of equations that you can solve. I have never tried this, but it sounds promising.
Another approach: use cubic spline interpolation (or some other technique) to calculate second derivative values at each data point. Then, on each segment, you have function values, and first and second derivative values at each end -- a total of 6 pieces of data. Interpolate these 6 pieces of data with a Hermite quintic polynomial. The end result is a $C_2$ piecewise quintic spline.
The moral is, a discontinuous periodic function can have continuous derivative. (More precisely, a periodic function whose left- and right-hand limits are unequal at some points can, nonetheless, have derivative with removable discontinuities.)
The trig functions and the principal branches of their inverses are good at creating this type of behavior. While it's true that
$$
\tan(\arctan x) = x\quad\text{for all real $x$,}
$$
it's not true that
$$
\arctan(\tan x) = x\quad\text{for all real $x$,}
$$
only that
$$
\arctan(\tan x) = x\quad\text{for all real $x$ with $|x| < \pi/2$.}
$$
Edit: The arctangent function (also known as "the principal branch of arctan"), shown bold in the graph below, is single-valued. The tangent function, however, is periodic with period $\pi$, so there are infinitely many branches of inverse tangent, any two differing by an integer multiple of $\pi$. For every integer $n$, we have
$$
x = \arctan(\tan x) + n\pi\quad\text{for $(n - \tfrac{1}{2})\pi < x < (n + \tfrac{1}{2})\pi$.}
$$
Consider the functions
$$
f(x) = \frac{1}{\sqrt{2}} \arctan(\sqrt{2} \tan x),\qquad
F(x) = \int_{0}^{x} \frac{dt}{1 + \sin^{2} t}.
$$
These functions have the same derivative $g$ for all real $x$ unless $x = (n + \frac{1}{2})\pi$ for some integer $n$; for these $x$, the value $f(x)$ is undefined, because $\tan(n + \frac{1}{2})\pi$ is undefined. Call these points poles of the tangent function.
As $x$ runs over the period interval $(n - \frac{1}{2})\pi < x < (n + \frac{1}{2})\pi$ between two poles of the tangent function, $\sqrt{2}\tan x$ runs once over the entire real line, so $\arctan(\sqrt{2}\tan x)$ runs once over the interval $(-\frac{1}{2}\pi, \frac{1}{2}\pi)$. The graph of $f$ consists of "repeated units", with a "jump" at each point $(n + \frac{1}{2})\pi$; the function $f$ "forgets" how many poles of tan lie between $0$ and $x$.
By contrast, the graph of $F$ "remembers" how many poles lie between $0$ and $x$. When $x$ crosses a pole of tan, $\sqrt{2}\tan x$ "jumps" from being "near $+\infty$" to being "near $-\infty$". Because area under the graph of $g$ accumulates continuously, the value of $F$ "continues" using the "next-highest" branch of arctan. Loosely, instead of "returning to $-\infty$ and re-using the same branch of arctan" (as $f$ does), $F$ continues by "following a dashed line from right to left" and starting on the next branch of arctan.
That is how $f$ and $F$ manage to have the same derivative (except at the poles of tan, where $f$ is undefined), but $f$ is periodic while $F$ extends continuously over the poles.
In the extended real numbers $[-\infty, \infty]$, in fact, arctan has a continuous extension, defined by
$$
\arctan(\pm\infty) = \lim_{x \to \pm\infty} \arctan x = \pm\tfrac{1}{2}\pi.
$$
This isn't much help with $f$, because now $f$ gets two values at each pole of tan, namely $\pm \frac{\pi}{2\sqrt{2}}$, but it does dictate why each time $x$ crosses a pole of tan, $F$ "climbs" to the next branch of arctan, and it shows why $F$ is continuous at the poles of tan.
Best Answer
Your example looks like $x \mapsto x \sin(1/x)$. This function is continuous but not differentiable at the origin.
The key point in the example $x \mapsto x^2 \sin(1/x)$ is that the local extrema are attained for $x$ such that $\sin(1/x) = 1$ and thus lie on a parabola.
Let us compute the extrema in your example. For a polynomial $x \mapsto (x-a)(x-b)$, the derivative is zero for $x = (a+b)/2$ and the absolute value of the function at this point is $(b-a)^2/4$. We have $a=1/2^{n+1}$, $b = 1/2^n$ so that the extremum of $f$ in the interval $[a,b]$ is attained at $x = 3/2^{n+2}$ and $$ \left|f\Bigl({3\over 2^{n+2}}\Bigr)\right| = 2^n {(1/2^n-1/2^{n+1})^2\over 4} = {1 \over 2^{n+4}} = {1 \over 12} ({3\over 2^{n+2}}) $$ The extrema lie on the line $y=x/12$ and not on a parabola.
So $f$ is bounded but the derivative is not continuous at zero, since the variation ${f(3/2^{n+2}) - f(0) \over 3/2^{n+2}}$ takes alternatively the values $-1/12$ and $1/12$.
If you want an example close to the $x^2\sin(1/x)$ map, you can remove the leading coefficient $2^n$. Then the extrema will be on a parabola. You lose the continuity of the derivative at the points $1/2^n$, I don't know if you see it as a problem. By looking at higher degree polynomials, this continuity could be restored. Anyway, once the extrema are on a parabola, the function $f$ can be studied in the same manner as the function $x^2 \sin(1/x)$ so as to reach the same conclusions.