Ejemplo n.º 1
0
gboolean
stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp)
{
    // first check for XEP-0203 delayed delivery
    xmpp_stanza_t *delay = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_DELAY);
    if (delay != NULL) {
        char *xmlns = xmpp_stanza_get_attribute(delay, STANZA_ATTR_XMLNS);
        if ((xmlns != NULL) && (strcmp(xmlns, "urn:xmpp:delay") == 0)) {
            char *stamp = xmpp_stanza_get_attribute(delay, STANZA_ATTR_STAMP);
            if ((stamp != NULL) && (g_time_val_from_iso8601(stamp, tv_stamp))) {
                return TRUE;
            }
        }
    }

    // otherwise check for XEP-0091 legacy delayed delivery
    // stanp format : CCYYMMDDThh:mm:ss
    xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
    if (x != NULL) {
        char *xmlns = xmpp_stanza_get_attribute(x, STANZA_ATTR_XMLNS);
        if ((xmlns != NULL) && (strcmp(xmlns, "jabber:x:delay") == 0)) {
            char *stamp = xmpp_stanza_get_attribute(x, STANZA_ATTR_STAMP);
            if ((stamp != NULL) && (g_time_val_from_iso8601(stamp, tv_stamp))) {
                return TRUE;
            }
        }
    }

    return FALSE;
}
Ejemplo n.º 2
0
static gboolean
get_time_value(const gchar *name, const GValue *value, GTimeVal *time_value)
{
    gboolean success;
    GType value_type;

    value_type = G_VALUE_TYPE(value);
    if (value_type == GSF_TIMESTAMP_TYPE) {
        GsfTimestamp *time_stamp;
        time_stamp = g_value_get_boxed(value);
        time_value->tv_sec = time_stamp->timet;
        time_value->tv_usec = 0;
        success = TRUE;
    } else if (value_type == G_TYPE_STRING) {
        const gchar *time_string;
        time_string = g_value_get_string(value);
        success = g_time_val_from_iso8601(time_string, time_value);
        if (!success) {
            chupa_warning("[decomposer][excel][metadata][invalid][%s][time]"
                          ": <%s>",
                          name, time_string);
        }
    } else {
        chupa_warning("[decomposer][excel][metadata][unsupported][%s][%s]",
                      name, g_type_name(value_type));
        success = FALSE;
    }
    return success;
}
Ejemplo n.º 3
0
/**
 * pk_iso8601_to_datetime: (skip)
 * @iso_date: The ISO8601 date to convert
 *
 * Return value: If valid then a new %GDateTime, else NULL
 *
 * Since: 0.8.11
 **/
GDateTime *
pk_iso8601_to_datetime (const gchar *iso_date)
{
	gboolean ret = FALSE;
	guint retval;
	guint d = 0;
	guint m = 0;
	guint y = 0;
	GTimeVal time_val;
	GDateTime *date = NULL;

	if (iso_date == NULL || iso_date[0] == '\0')
		goto out;

	/* try to parse complete ISO8601 date */
	if (g_strstr_len (iso_date, -1, " ") != NULL)
		ret = g_time_val_from_iso8601 (iso_date, &time_val);
	if (ret && time_val.tv_sec != 0) {
		g_debug ("Parsed %s %i", iso_date, ret);
		date = g_date_time_new_from_timeval_utc (&time_val);
		goto out;
	}

	/* g_time_val_from_iso8601() blows goats and won't
	 * accept a valid ISO8601 formatted date without a
	 * time value - try and parse this case */
	retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d);
	if (retval != 3)
		goto out;

	/* create valid object */
	date = g_date_time_new_utc (y, m, d, 0, 0, 0);
out:
	return date;
}
Ejemplo n.º 4
0
/**
 * Imports an old (protocol v1.2) timestamp, format "%Y-%m-%d
 * %H:%M:%S".
 */
static char *
import_old_timestamp(const char *p)
{
	char *q;
	bool success;
	GTimeVal time_val;

	if (strlen(p) <= 10 || p[10] != ' ')
		return NULL;

	g_debug("Importing time stamp '%s'", p);

	/* replace a space with 'T', as expected by
	   g_time_val_from_iso8601() */
	q = g_strdup(p);
	q[10] = 'T';

	success = g_time_val_from_iso8601(q, &time_val);
	g_free(q);
	if (!success) {
		g_debug("Import of '%s' failed", p);
		return NULL;
	}

	g_debug("'%s' -> %ld", p, time_val.tv_sec);
	return g_strdup_printf("%ld", time_val.tv_sec);
}
Ejemplo n.º 5
0
static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr) 
{
  json_t *jsrvr_addr = NULL;
  json_t *jsrvr_kn = NULL;
  json_t *jsrvr_dh = NULL;
  json_t *jsrvr_expire = NULL;

  if (jsrvr == NULL)
    return -1;


  if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
      (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
      (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
    tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
    return -1;
  }
  
  /* TBD -- handle IPv6 Addresses */
  inet_aton(json_string_value(jsrvr_addr), &(srvr->aaa_server_addr));
  srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
  srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
  srvr->path = json_object_get(jsrvr, "path");
  jsrvr_expire = json_object_get(jsrvr, "key_expiration");
  if (jsrvr_expire && json_is_string(jsrvr_expire)) {
    if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
				 &srvr->key_expiration))
      tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
  }
  
  return 0;
}
Ejemplo n.º 6
0
/**
 * as_iso8601_to_datetime:
 *
 * Helper function to work around a bug in g_time_val_from_iso8601.
 * Can be dropped when the bug gets resolved upstream:
 * https://bugzilla.gnome.org/show_bug.cgi?id=760983
 **/
GDateTime*
as_iso8601_to_datetime (const gchar *iso_date)
{
	GTimeVal tv;
	guint dmy[] = {0, 0, 0};

	/* nothing set */
	if (iso_date == NULL || iso_date[0] == '\0')
		return NULL;

	/* try to parse complete ISO8601 date */
	if (g_strstr_len (iso_date, -1, " ") != NULL) {
		if (g_time_val_from_iso8601 (iso_date, &tv) && tv.tv_sec != 0)
			return g_date_time_new_from_timeval_utc (&tv);
	}

	/* g_time_val_from_iso8601() blows goats and won't
	 * accept a valid ISO8601 formatted date without a
	 * time value - try and parse this case */
	if (sscanf (iso_date, "%u-%u-%u", &dmy[0], &dmy[1], &dmy[2]) != 3)
		return NULL;

	/* create valid object */
	return g_date_time_new_utc (dmy[0], dmy[1], dmy[2], 0, 0, 0);
}
Ejemplo n.º 7
0
fs_value fs_value_datetime_from_string(const char *s)
{
    fs_value v = fs_value_blank();
    v.attr = fs_c.xsd_datetime;

    struct tm td;

    memset(&td, 0, sizeof(struct tm));

    GTimeVal gtime;
    if (g_time_val_from_iso8601(s, &gtime)) {
        v.in = gtime.tv_sec;
        v.valid = fs_valid_bit(FS_V_IN);
        v.lex = (char *)s;

        return v;
    }
    char *ret = strptime(s, "%Y-%m-%d", &td);
    if (ret) {
	v.in = timegm(&td);
	v.valid = fs_valid_bit(FS_V_IN);

	return v;
    }

    return fs_value_error(FS_ERROR_INVALID_TYPE,
	    "cannot convert value to xsd:dateTime");
}
Ejemplo n.º 8
0
 DateTime DateTime::from_iso8601(const std::string &iso8601)
 {
   DateTime retval;
   if(g_time_val_from_iso8601(iso8601.c_str(), &retval.m_date)) {
     return retval;
   }
   return DateTime();
 }
Ejemplo n.º 9
0
static gint64
str_to_gint64 (xmlChar *str)
{
  GTimeVal time = {0,0};

  g_time_val_from_iso8601 ((gchar*) str, &time);
  return (gint64) time.tv_sec;
}
Ejemplo n.º 10
0
/**
 * gd_time_val_from_iso8601:
 * @string: (allow-none):
 * @timeval: (out):
 *
 * Returns:
 */
gboolean
gd_time_val_from_iso8601 (const gchar *string,
                          GTimeVal *timeval)
{
  if (string == NULL)
    g_get_current_time (timeval);

  return g_time_val_from_iso8601 (string, timeval);
}
Ejemplo n.º 11
0
time_t rm_iso8601_parse(const char *string) {
    GTimeVal time_result;
    if(!g_time_val_from_iso8601(string, &time_result)) {
        rm_log_perror("Converting time failed");
        return 0;
    }

    return time_result.tv_sec;
}
Ejemplo n.º 12
0
static GDateTime*
item_cursor_to_time (TrackerSparqlCursor *cursor)
{
	const gchar *date;
	GTimeVal val;

	date = tracker_sparql_cursor_get_string (cursor, 6, NULL);
	g_time_val_from_iso8601 (date, &val);
	return g_date_time_new_from_timeval_local (&val);
}
Ejemplo n.º 13
0
static bool
SAMLCheckTimeAttr(const DOMElement *elem,
                  const char *attrName,
                  bool notBefore)
{
   const XMLCh *timeAttr = elem->getAttribute(MAKE_UNICODE_STRING(attrName));
   if ((NULL == timeAttr) || (0 == *timeAttr)) {
      /*
       * The presence of all time restrictions in SAML are optional, so if
       * the attribute is not present, that is fine.
       */
      return true;
   }

   SAMLStringWrapper timeStr(timeAttr);
   GTimeVal attrTime;

   if (!g_time_val_from_iso8601(timeStr.c_str(), &attrTime)) {
      Log("%s: Could not parse %s value (%s).\n", __FUNCTION__, attrName,
          timeStr.c_str());
      return false;
   }

   GTimeVal now;
   g_get_current_time(&now);

   glong diff;

   /*
    * Check the difference, doing the math so that a positive
    * value is bad.  Ignore the micros since we're letting clock
    * skew add a fudge-factor.
    */
   if (notBefore) {
      // expect time <= now
      diff = attrTime.tv_sec - now.tv_sec;
   } else {
      // expect now <= time
      diff = now.tv_sec - attrTime.tv_sec;
   }

   /*
    * A negative value is fine, a postive value
    * greater than the clock skew range is bad.
    */
   if (diff > clockSkewAdjustment) {
      Warning("%s: FAILED SAML assertion (timeStamp %s, delta %d) %s.\n",
              __FUNCTION__, timeStr.c_str(), (int) diff,
              notBefore ? "is not yet valid" : "has expired");
      return false;
   }

   return true;
}
Ejemplo n.º 14
0
/**
 * CacheUtilDatetimeFromString:
 * @value: String containing the date, in the "{yyyy}-{mm}-{dd}T{hh}:{mm}:{ss}Z"
 * format (ISO 8601). Dates are always UTC.
 * @result: (out): GDateTime respresenting the given @value.
 *
 * Returns: #TRUE if @result was modified or #FALSE if the date couldn't be
 * parsed.
 */
gboolean CacheUtilDatetimeFromString(const gchar *value, GDateTime **result) {
  GTimeVal tv = {0, 0};
  g_assert(result);

  if (g_time_val_from_iso8601(value, &tv)) {
    *result = g_date_time_new_from_timeval_utc(&tv);
    return TRUE;
  } else {
    return FALSE;
  }
}
Ejemplo n.º 15
0
/**
 * grl_date_time_from_iso8601:
 * @date: a date expressed in iso8601 format
 *
 * Returns: a newly-allocated #GDateTime set to the time corresponding to
 * @date, or %NULL if @date could not be parsed properly.
 *
 * Since: 0.2.0
 */
GDateTime *
grl_date_time_from_iso8601 (const gchar *date)
{
  GTimeVal t = { 0, };
  gboolean ret;
  gchar *date_time;
  gint date_length;

  if (!date) {
    return NULL;
  }

  ret = g_time_val_from_iso8601 (date, &t);

  /* second condition works around
   * https://bugzilla.gnome.org/show_bug.cgi?id=650968 */
  if (!ret || (t.tv_sec == 0 && t.tv_usec == 0)) {
    /* We might be in the case where there is a date alone. In that case, we
     * take the convention of setting it to noon GMT */

    /* Date can could be YYYY, YYYY-MM, YYYY-MM-DD or YYYYMMDD */
    date_length = strlen (date);
    switch (date_length) {
    case 4:
      date_time = g_strdup_printf ("%s-01-01T12:00:00Z", date);
      break;
    case 7:
      date_time = g_strdup_printf ("%s-01T12:00:00Z", date);
      break;
    default:
      date_time = g_strdup_printf ("%sT12:00:00Z", date);
    }
    ret = g_time_val_from_iso8601 (date_time, &t);
    g_free (date_time);
  }

  if (ret)
    return g_date_time_new_from_timeval_utc (&t);

  return NULL;
}
Ejemplo n.º 16
0
static bool parse_start_ts(const char *key, const char *value,
					gpointer user_data, GError **error)
{
	GTimeVal time_val;

	if (!g_time_val_from_iso8601(value, &time_val))
		return false;

	option_start_ts = time_val.tv_sec;

	return true;
}
Ejemplo n.º 17
0
gboolean
gdata_parser_int64_from_iso8601 (const gchar *date, gint64 *_time)
{
	GTimeVal time_val;

	if (g_time_val_from_iso8601 (date, &time_val) == TRUE) {
		*_time = time_val.tv_sec;
		return TRUE;
	}

	return FALSE;
}
Ejemplo n.º 18
0
void
sv_ev_roster_received(void)
{
    if (prefs_get_boolean(PREF_ROSTER)) {
        ui_show_roster();
    }

    char *account_name = session_get_account_name();

#ifdef HAVE_LIBGPGME
    // check pgp key valid if specified
    ProfAccount *account = accounts_get_account(account_name);
    if (account && account->pgp_keyid) {
        char *err_str = NULL;
        if (!p_gpg_valid_key(account->pgp_keyid, &err_str)) {
            cons_show_error("Invalid PGP key ID specified: %s, %s", account->pgp_keyid, err_str);
        }
        free(err_str);
    }
    account_free(account);
#endif

    // send initial presence
    resource_presence_t conn_presence = accounts_get_login_presence(account_name);
    char *last_activity_str = accounts_get_last_activity(account_name);
    if (last_activity_str) {
        GDateTime *nowdt = g_date_time_new_now_utc();

        GTimeVal lasttv;
        gboolean res = g_time_val_from_iso8601(last_activity_str, &lasttv);
        if (res) {
            GDateTime *lastdt = g_date_time_new_from_timeval_utc(&lasttv);
            GTimeSpan diff_micros = g_date_time_difference(nowdt, lastdt);
            int diff_secs = (diff_micros / 1000) / 1000;
            if (prefs_get_boolean(PREF_LASTACTIVITY)) {
                cl_ev_presence_send(conn_presence, NULL, diff_secs);
            } else {
                cl_ev_presence_send(conn_presence, NULL, 0);
            }
            g_date_time_unref(lastdt);
        } else {
            cl_ev_presence_send(conn_presence, NULL, 0);
        }

        free(last_activity_str);
        g_date_time_unref(nowdt);
    } else {
        cl_ev_presence_send(conn_presence, NULL, 0);
    }

    const char *fulljid = connection_get_fulljid();
    plugins_on_connect(account_name, fulljid);
}
Ejemplo n.º 19
0
fs_value fn_seconds(fs_query *q, fs_value v)
{
    if (v.attr != fs_c.xsd_datetime) {
        return fs_value_error(FS_ERROR_INVALID_TYPE, NULL);
    }
    v = fs_value_fill_lexical(q, v);
    GTimeVal tv;
    if (!g_time_val_from_iso8601(v.lex, &tv)) {
        return fs_value_error(FS_ERROR_INVALID_TYPE, "cannot get seconds from xsd:date");
    }

    return fs_value_integer(tv.tv_sec % 60);
}
Ejemplo n.º 20
0
static GDateTime *
parse_date (const gchar *iso8601_string)
{
  char *padded_date;
  GDateTime *date;
  GTimeVal tv;

  padded_date = g_strconcat (iso8601_string, "T00:00:00Z", NULL);
  g_time_val_from_iso8601 (padded_date, &tv);
  date = g_date_time_new_from_timeval_utc (&tv);
  g_free (padded_date);
  return date;
}
Ejemplo n.º 21
0
/**
 * vu_check_latest_version:
 * @window: Somewhere where we may need use the display to inform the user about the version status
 *
 * Periodically checks the released latest VERSION file on the website to compare with the running version
 *
 */
void vu_check_latest_version ( GtkWindow *window )
{
	if ( ! a_vik_get_check_version () )
		return;

	gboolean do_check = FALSE;

	gint check_period;
	if ( ! a_settings_get_integer ( VIK_SETTINGS_VERSION_CHECK_PERIOD, &check_period ) ) {
		check_period = 14;
	}

	// Get last checked date...
	GDate *gdate_last = g_date_new();
	GDate *gdate_now = g_date_new();
	GTimeVal time_last;
	gchar *last_checked_date = NULL;

	// When no previous date available - set to do the version check
	if ( a_settings_get_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, &last_checked_date) ) {
		if ( g_time_val_from_iso8601 ( last_checked_date, &time_last ) ) {
			g_date_set_time_val ( gdate_last, &time_last );
		}
		else
			do_check = TRUE;
	}
	else
		do_check = TRUE;

	GTimeVal time_now;
	g_get_current_time ( &time_now );
	g_date_set_time_val ( gdate_now, &time_now );

	if ( ! do_check ) {
		// Dates available so do the comparison
		g_date_add_days ( gdate_last, check_period );
		if ( g_date_compare ( gdate_last, gdate_now ) < 0 )
			do_check = TRUE;
	}

	g_date_free ( gdate_last );
	g_date_free ( gdate_now );

	if ( do_check ) {
#if GLIB_CHECK_VERSION (2, 32, 0)
		g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
#else
		g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
#endif
	}
}
Ejemplo n.º 22
0
GDate* node_to_date (xmlNode *node)
{
    GTimeVal timeval;
    GDate *ret;
    xmlChar *tmp;

    ret = g_date_new ();
    tmp = xmlNodeGetContent (node);

    if (g_time_val_from_iso8601 ((char*) tmp, &timeval))
        g_date_set_time_val (ret, &timeval);

    xmlFree (tmp);
    return ret;
}
Ejemplo n.º 23
0
gboolean
gdata_parser_time_val_from_date (const gchar *date, GTimeVal *_time)
{
	gchar *iso8601_date;
	gboolean success;

	if (strlen (date) != 10 && strlen (date) != 8)
		return FALSE;

	/* Note: This doesn't need translating, as it's outputting an ISO 8601 time string */
	iso8601_date = g_strdup_printf ("%sT00:00:00Z", date);
	success = g_time_val_from_iso8601 (iso8601_date, _time);
	g_free (iso8601_date);

	return success;
}
Ejemplo n.º 24
0
glong date_compare_against_build_date(const char *iso8601_date)
{
    GTimeVal time;
    glong build_time = atol(GIT_COMMIT_TIMESTAMP);
    gboolean parsed;
    glong ret = 0;

    parsed = g_time_val_from_iso8601(iso8601_date, &time);
    if (parsed) {
        ret = time.tv_sec - build_time;
    } else {
        printf("%s could not parse date. Not a ISO 8601 format.", __func__);
    }

    return ret;
}
Ejemplo n.º 25
0
void
pragha_scanner_scan_library(PraghaScanner *scanner)
{
	PraghaPreferences *preferences;
	gchar *last_scan_time = NULL;

	if(scanner->update_timeout)
		return;

	preferences = pragha_preferences_get();

	/* Get last time that update the library and folders to analyze */

	last_scan_time = pragha_preferences_get_string(preferences,
	                                               GROUP_LIBRARY,
	                                               KEY_LIBRARY_LAST_SCANNED);
	if (last_scan_time) {
		if (!g_time_val_from_iso8601(last_scan_time, &scanner->last_update))
			g_warning("Unable to convert last rescan time");
		g_free(last_scan_time);
	}

	scanner->folder_list =
		pragha_preferences_get_filename_list(preferences,
		                                     GROUP_LIBRARY,
		                                     KEY_LIBRARY_DIR);
	scanner->folder_scanned =
		pragha_preferences_get_filename_list(preferences,
		                                     GROUP_LIBRARY,
		                                     KEY_LIBRARY_SCANNED);
	g_object_unref(G_OBJECT(preferences));

	/* Update the gui */

	scanner->update_timeout = g_timeout_add_seconds(1, (GSourceFunc)pragha_scanner_update_progress, scanner);

	pragha_preferences_set_show_status_bar (preferences, TRUE);
	gtk_widget_show_all(scanner->hbox);

	/* Launch threads */

	scanner->no_files_thread = g_thread_new("Count no files", pragha_scanner_count_no_files_worker, scanner);

	scanner->worker_thread = pragha_async_launch_full(pragha_scanner_scan_worker,
	                                                  pragha_scanner_worker_finished,
	                                                  scanner);
}
Ejemplo n.º 26
0
/**
 * gpk_log_get_localised_date:
 **/
static gchar *
gpk_log_get_localised_date (const gchar *timespec)
{
	GDate *date;
	GTimeVal timeval;
	gchar buffer[100];

	/* the old date */
	g_time_val_from_iso8601 (timespec, &timeval);

	/* get printed string */
	date = g_date_new ();
	g_date_set_time_val (date, &timeval);

	/* TRANSLATORS: strftime formatted please */
	g_date_strftime (buffer, 100, _("%A, %d %B %Y"), date);
	g_date_free (date);
	return g_strdup (buffer);
}
Ejemplo n.º 27
0
void _gpx_parser_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data,
                      GError **error)
{
  dt_gpx_t *gpx = (dt_gpx_t *)user_data;

  if(!gpx->current_track_point) return;

  if(gpx->current_parser_element == GPX_PARSER_ELEMENT_TIME)
  {

    if(!g_time_val_from_iso8601(text, &gpx->current_track_point->time))
    {
      gpx->invalid_track_point = TRUE;
      fprintf(stderr, "broken gpx file, failed to pars is8601 time '%s' for trackpoint\n", text);
    }
  }
  else if(gpx->current_parser_element == GPX_PARSER_ELEMENT_ELE)
    gpx->current_track_point->elevation = g_ascii_strtod(text, NULL);
}
Ejemplo n.º 28
0
/*
 * gdata_parser_int64_time_from_element:
 * @element: the element to check against
 * @element_name: the name of the element to parse
 * @options: a bitwise combination of parsing options from #GDataParserOptions, or %P_NONE
 * @output: (out caller-allocates): the return location for the parsed time value
 * @success: the return location for a value which is %TRUE if the time val was parsed successfully, %FALSE if an error was encountered,
 * and undefined if @element didn't match @element_name
 * @error: a #GError, or %NULL
 *
 * Gets the time value of @element if its name is @element_name, subject to various checks specified by @options. It expects the text content
 * of @element to be a date or time value in ISO 8601 format. The returned time value will be a UNIX timestamp (seconds since the epoch).
 *
 * If @element doesn't match @element_name, %FALSE will be returned, @error will be unset and @success will be unset.
 *
 * If @element matches @element_name but one of the checks specified by @options fails, %TRUE will be returned, @error will be set to a
 * %GDATA_SERVICE_ERROR_PROTOCOL_ERROR error and @success will be set to %FALSE.
 *
 * If @element matches @element_name and all of the checks specified by @options pass, %TRUE will be returned, @error will be unset and
 * @success will be set to %TRUE.
 *
 * The reason for returning the success of the parsing in @success is so that calls to gdata_parser_int64_time_from_element() can be chained
 * together in a large "or" statement based on their return values, for the purposes of determining whether any of the calls matched
 * a given @element. If any of the calls to gdata_parser_int64_time_from_element() return %TRUE, the value of @success can be examined.
 *
 * Return value: %TRUE if @element matched @element_name, %FALSE otherwise
 *
 * Since: 0.7.0
 */
gboolean
gdata_parser_int64_time_from_element (xmlNode *element, const gchar *element_name, GDataParserOptions options,
                                      gint64 *output, gboolean *success, GError **error)
{
	xmlChar *text;
	GTimeVal time_val;

	/* Check it's the right element */
	if (xmlStrcmp (element->name, (xmlChar*) element_name) != 0)
		return FALSE;

	/* Check if the output time val has already been set */
	if (options & P_NO_DUPES && *output != -1) {
		*success = gdata_parser_error_duplicate_element (element, error);
		return TRUE;
	}

	/* Get the string and check it for NULLness */
	text = xmlNodeListGetString (element->doc, element->children, TRUE);
	if (options & P_REQUIRED && (text == NULL || *text == '\0')) {
		xmlFree (text);
		*success = gdata_parser_error_required_content_missing (element, error);
		return TRUE;
	}

	/* Attempt to parse the string as a GTimeVal */
	if (g_time_val_from_iso8601 ((gchar*) text, &time_val) == FALSE) {
		*success = gdata_parser_error_not_iso8601_format (element, (gchar*) text, error);
		xmlFree (text);
		return TRUE;
	}

	*output = time_val.tv_sec;

	/* Success! */
	xmlFree (text);
	*success = TRUE;

	return TRUE;
}
Ejemplo n.º 29
0
/**
 * pk_iso8601_to_date:
 * @iso_date: The ISO8601 date to convert
 *
 * Return value: If valid then a new %GDate, else NULL
 *
 * Since: 0.5.2
 **/
GDate *
pk_iso8601_to_date (const gchar *iso_date)
{
	gboolean ret;
	guint retval;
	guint d = 0;
	guint m = 0;
	guint y = 0;
	GTimeVal time_val;
	GDate *date = NULL;

	if (iso_date == NULL || iso_date[0] == '\0')
		goto out;

	/* try to parse complete ISO8601 date */
	ret = g_time_val_from_iso8601 (iso_date, &time_val);
	if (ret && time_val.tv_sec != 0) {
		date = g_date_new ();
		g_date_set_time_val (date, &time_val);
		goto out;
	}

	/* g_time_val_from_iso8601() blows goats and won't
	 * accept a valid ISO8601 formatted date without a
	 * time value - try and parse this case */
	retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d);
	if (retval != 3)
		goto out;

	/* check it's valid */
	ret = g_date_valid_dmy (d, m, y);
	if (!ret)
		goto out;

	/* create valid object */
	date = g_date_new_dmy (d, m, y);
out:
	return date;
}
Ejemplo n.º 30
0
/*
 * gdata_parser_int64_time_from_json_member:
 * @reader: #JsonReader cursor object to read JSON node from
 * @element_name: the name of the element to parse
 * @options: a bitwise combination of parsing options from #GDataParserOptions, or %P_NONE
 * @output: (out caller-allocates): the return location for the parsed time value
 * @success: the return location for a value which is %TRUE if the time val was parsed successfully, %FALSE if an error was encountered,
 * and undefined if @element didn't match @element_name
 * @error: a #GError, or %NULL
 *
 * Gets the time value of @element if its name is @element_name, subject to various checks specified by @options. It expects the text content
 * of @element to be a date or time value in ISO 8601 format. The returned time value will be a UNIX timestamp (seconds since the epoch).
 *
 * If @element doesn't match @element_name, %FALSE will be returned, @error will be unset and @success will be unset.
 *
 * If @element matches @element_name but one of the checks specified by @options fails, %TRUE will be returned, @error will be set to a
 * %GDATA_SERVICE_ERROR_PROTOCOL_ERROR error and @success will be set to %FALSE.
 *
 * If @element matches @element_name and all of the checks specified by @options pass, %TRUE will be returned, @error will be unset and
 * @success will be set to %TRUE.
 *
 * The reason for returning the success of the parsing in @success is so that calls to gdata_parser_int64_time_from_element() can be chained
 * together in a large "or" statement based on their return values, for the purposes of determining whether any of the calls matched
 * a given @element. If any of the calls to gdata_parser_int64_time_from_element() return %TRUE, the value of @success can be examined.
 *
 * Return value: %TRUE if @element matched @element_name, %FALSE otherwise
 *
 * Since: 0.15.0
 */
gboolean
gdata_parser_int64_time_from_json_member (JsonReader *reader, const gchar *member_name, GDataParserOptions options,
                                          gint64 *output, gboolean *success, GError **error)
{
	const gchar *text;
	GTimeVal time_val;
	const GError *child_error = NULL;

	/* Check if there's such element */
	if (g_strcmp0 (json_reader_get_member_name (reader), member_name) != 0) {
		return FALSE;
	}

	/* Check if the output time val has already been set. The JSON parser guarantees this can't happen. */
	g_assert (!(options & P_NO_DUPES) || *output == -1);

	/* Get the string and check it for NULLness. Check for errors first. */
	text = json_reader_get_string_value (reader);
	child_error = json_reader_get_error (reader);
	if (child_error != NULL) {
		*success = parser_error_from_json_error (reader, child_error, error);
		return TRUE;
	} else if (options & P_REQUIRED && (text == NULL || *text == '\0')) {
		*success = gdata_parser_error_required_json_content_missing (reader, error);
		return TRUE;
	}

	/* Attempt to parse the string as a GTimeVal */
	if (g_time_val_from_iso8601 ((gchar*) text, &time_val) == FALSE) {
		*success = gdata_parser_error_not_iso8601_format_json (reader, text, error);
		return TRUE;
	}

	/* Success! */
	*output = time_val.tv_sec;
	*success = TRUE;

	return TRUE;
}