⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdate.c

📁 嵌入式下基于MiniGUI的Web Browser
💻 C
📖 第 1 页 / 共 3 页
字号:
  num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';    s = (const guchar *) str;  pt->num_ints = 0;  while (*s && pt->num_ints < 4)     {            i = 0;      while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)        {          num[pt->num_ints][i] = *s;          ++s;           ++i;        }            if (i > 0)         {          num[pt->num_ints][i] = '\0';          ++(pt->num_ints);        }            if (*s == '\0') break;            ++s;    }    pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;  pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;  pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;    pt->month = G_DATE_BAD_MONTH;    if (pt->num_ints < 3)    {      gchar *casefold;      gchar *normalized;            casefold = g_utf8_casefold (str, -1);      normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);      g_free (casefold);      i = 1;      while (i < 13)        {          if (long_month_names[i] != NULL)             {              const gchar *found = strstr (normalized, long_month_names[i]);	                    if (found != NULL)                {                  pt->month = i;		  break;                }            }	            if (short_month_names[i] != NULL)             {              const gchar *found = strstr (normalized, short_month_names[i]);	                    if (found != NULL)                {                  pt->month = i;		  break;                }            }          ++i;        }      g_free (normalized);    }}/* HOLDS: g_date_global_lock */static voidg_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt){  const gchar *locale = setlocale (LC_TIME, NULL);  gboolean recompute_localeinfo = FALSE;  GDate d;    g_return_if_fail (locale != NULL); /* should not happen */    g_date_clear (&d, 1);              /* clear for scratch use */    if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )     {      recompute_localeinfo = TRUE;  /* Uh, there used to be a reason for the temporary */    }    if (recompute_localeinfo)    {      int i = 1;      GDateParseTokens testpt;      gchar buf[128];            g_free (current_locale); /* still works if current_locale == NULL */            current_locale = g_strdup (locale);            while (i < 13)         {	  gchar *casefold;	            g_date_set_dmy (&d, 1, i, 1);	            g_return_if_fail (g_date_valid (&d));	            g_date_strftime (buf, 127, "%b", &d);	  casefold = g_utf8_casefold (buf, -1);          g_free (short_month_names[i]);          short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);	  g_free (casefold);	            g_date_strftime (buf, 127, "%B", &d);	  casefold = g_utf8_casefold (buf, -1);          g_free (long_month_names[i]);          long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);	  g_free (casefold);                    ++i;        }            /* Determine DMY order */            /* had to pick a random day - don't change this, some strftimes       * are broken on some days, and this one is good so far. */      g_date_set_dmy (&d, 4, 7, 1976);            g_date_strftime (buf, 127, "%x", &d);            g_date_fill_parse_tokens (buf, &testpt);            i = 0;      while (i < testpt.num_ints)        {          switch (testpt.n[i])            {            case 7:              dmy_order[i] = G_DATE_MONTH;              break;            case 4:              dmy_order[i] = G_DATE_DAY;              break;            case 76:              using_twodigit_years = TRUE; /* FALL THRU */            case 1976:              dmy_order[i] = G_DATE_YEAR;              break;            default:              /* leave it unchanged */              break;            }          ++i;        }      #ifdef G_ENABLE_DEBUG      DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));      i = 1;      while (i < 13)         {          DEBUG_MSG (("  %s   %s", long_month_names[i], short_month_names[i]));          ++i;        }      if (using_twodigit_years)	DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));      {         gchar *strings[3];        i = 0;        while (i < 3)          {            switch (dmy_order[i])              {              case G_DATE_MONTH:                strings[i] = "Month";                break;              case G_DATE_YEAR:                strings[i] = "Year";                break;              case G_DATE_DAY:                strings[i] = "Day";                break;              default:                strings[i] = NULL;                break;              }            ++i;          }        DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));        DEBUG_MSG (("**Sample date in this locale: `%s'", buf));      }#endif    }    g_date_fill_parse_tokens (str, pt);}void         g_date_set_parse (GDate       *d,                   const gchar *str){  GDateParseTokens pt;  guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;    g_return_if_fail (d != NULL);    /* set invalid */  g_date_clear (d, 1);    G_LOCK (g_date_global);  g_date_prepare_to_parse (str, &pt);    DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d", 	      pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));      if (pt.num_ints == 4)     {      G_UNLOCK (g_date_global);      return; /* presumably a typo; bail out. */    }    if (pt.num_ints > 1)    {      int i = 0;      int j = 0;            g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */            while (i < pt.num_ints && j < 3)         {          switch (dmy_order[j])            {            case G_DATE_MONTH:	    {	      if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)		{		  m = pt.month;		  ++j;      /* skip months, but don't skip this number */		  continue;		}	      else 		m = pt.n[i];	    }	    break;            case G_DATE_DAY:	    {	      if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)		{		  day = 1;		  ++j;      /* skip days, since we may have month/year */		  continue;		}	      day = pt.n[i];	    }	    break;            case G_DATE_YEAR:	    {	      y  = pt.n[i];	      	      if (using_twodigit_years && y < 100)		{		  guint two     =  twodigit_start_year % 100;		  guint century = (twodigit_start_year / 100) * 100;		  		  if (y < two)		    century += 100;		  		  y += century;		}	    }	    break;            default:              break;            }	            ++i;          ++j;        }                  if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))        {          /* Try YYYY MM DD */          y   = pt.n[0];          m   = pt.n[1];          day = pt.n[2];                    if (using_twodigit_years && y < 100)             y = G_DATE_BAD_YEAR; /* avoids ambiguity */        }      else if (pt.num_ints == 2)	{	  if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)	    {	      m = pt.month;	    }	}    }  else if (pt.num_ints == 1)     {      if (pt.month != G_DATE_BAD_MONTH)        {          /* Month name and year? */          m    = pt.month;          day  = 1;          y = pt.n[0];        }      else        {          /* Try yyyymmdd and yymmdd */	            m   = (pt.n[0]/100) % 100;          day = pt.n[0] % 100;          y   = pt.n[0]/10000;	            /* FIXME move this into a separate function */          if (using_twodigit_years && y < 100)            {              guint two     =  twodigit_start_year % 100;              guint century = (twodigit_start_year / 100) * 100;                            if (y < two)                century += 100;                            y += century;            }        }    }    /* See if we got anything valid out of all this. */  /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */  if (y < 8000 && g_date_valid_dmy (day, m, y))     {      d->month = m;      d->day   = day;      d->year  = y;      d->dmy   = TRUE;    }#ifdef G_ENABLE_DEBUG  else     DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));#endif  G_UNLOCK (g_date_global);}void         g_date_set_time (GDate *d,		 GTime  time){  time_t t = time;  struct tm tm;    g_return_if_fail (d != NULL);  #ifdef HAVE_LOCALTIME_R  localtime_r (&t, &tm);#else  {    struct tm *ptm = localtime (&t);    g_assert (ptm);    memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));  }#endif    d->julian = FALSE;    d->month = tm.tm_mon + 1;  d->day   = tm.tm_mday;  d->year  = tm.tm_year + 1900;    g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));    d->dmy    = TRUE;}void         g_date_set_month (GDate     *d,                   GDateMonth m){  g_return_if_fail (d != NULL);  g_return_if_fail (g_date_valid_month (m));  if (d->julian && !d->dmy) g_date_update_dmy(d);  d->julian = FALSE;    d->month = m;    if (g_date_valid_dmy (d->day, d->month, d->year))    d->dmy = TRUE;  else     d->dmy = FALSE;}void         g_date_set_day (GDate     *d,                 GDateDay day){  g_return_if_fail (d != NULL);  g_return_if_fail (g_date_valid_day (day));    if (d->julian && !d->dmy) g_date_update_dmy(d);  d->julian = FALSE;    d->day = day;    if (g_date_valid_dmy (d->day, d->month, d->year))    d->dmy = TRUE;  else     d->dmy = FALSE;}void         g_date_set_year (GDate     *d,                  GDateYear  y){  g_return_if_fail (d != NULL);  g_return_if_fail (g_date_valid_year (y));    if (d->julian && !d->dmy) g_date_update_dmy(d);  d->julian = FALSE;    d->year = y;    if (g_date_valid_dmy (d->day, d->month, d->year))    d->dmy = TRUE;  else     d->dmy = FALSE;}void         g_date_set_dmy (GDate     *d,                 GDateDay   day,                 GDateMonth m,                 GDateYear  y){  g_return_if_fail (d != NULL);  g_return_if_fail (g_date_valid_dmy (day, m, y));    d->julian = FALSE;    d->month = m;  d->day   = day;  d->year  = y;    d->dmy = TRUE;}void         g_date_set_julian (GDate *d, guint32 j){  g_return_if_fail (d != NULL);  g_return_if_fail (g_date_valid_julian (j));    d->julian_days = j;  d->julian = TRUE;  d->dmy = FALSE;}gboolean     g_date_is_first_of_month (const GDate *d){  g_return_val_if_fail (d != NULL, FALSE);  g_return_val_if_fail (g_date_valid (d), FALSE);    if (!d->dmy)     {      g_date_update_dmy (d);    }  g_return_val_if_fail (d->dmy, FALSE);      if (d->day == 1) return TRUE;  else return FALSE;}gboolean     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -