#include <stdio.h>
#include <stdlib.h>
#include <glib-unix.h>
#include <gst/gst.h>

static guint64
gstextractor_timestamp_to_nanoseconds (gchar * timestamp)
{
  gchar **timev = g_strsplit (timestamp, ":", -1);
  gint timec = g_strv_length (timev);
  gdouble time = 0;

  time = time + ((timec - 1) >= 0
      && timev[timec - 1] ? g_strtod (timev[timec - 1], NULL) : 0);
  time = time + ((timec - 2) >= 0
      && timev[timec - 2] ? g_strtod (timev[timec - 2], NULL) * 60 : 0);
  time = time + ((timec - 3) >= 0
      && timev[timec - 3] ? g_strtod (timev[timec - 3], NULL) * 60 * 60 : 0);

  g_strfreev (timev);
  return (guint64) (time * GST_SECOND);
}

int
main(int argc, char *argv[])
{
  GstDateTime *gstdatetime = NULL;
  GDateTime *gdatetime = NULL;

  if(argc < 2) {
    g_printerr("[usage] %s YYYY-MM-DDTHH:MM:SS+HH:MM\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  gstdatetime = gst_date_time_new_from_iso8601_string(argv[1]);
  g_print("gstdatetime: %p\n", gstdatetime);
  gdatetime = gst_date_time_to_g_date_time(gstdatetime);

  g_print("gdatetime nanoseconds: %lu\n",
	  (guint64) (g_date_time_get_second(gdatetime) * GST_SECOND));
  g_print("manual nanoseconds: %lu\n",
	  gstextractor_timestamp_to_nanoseconds(argv[1]));
  return 0;
}
