[svn r223] backport of glib functions trunk
authorrenatofilho
Mon Dec 18 18:18:26 2006 +0000 (2006-12-18)
branchtrunk
changeset 222803672c406ec
parent 221 b250ee0438a2
child 223 657379d8f3b6
[svn r223] backport of glib functions
gmyth/src/gmyth_util.c
     1.1 --- a/gmyth/src/gmyth_util.c	Fri Dec 15 00:21:57 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_util.c	Mon Dec 18 18:18:26 2006 +0000
     1.3 @@ -39,6 +39,18 @@
     1.4  
     1.5  #include "gmyth.h"
     1.6  
     1.7 +#if !GLIB_CHECK_VERSION (2, 10, 0)
     1.8 +gchar *
     1.9 +g_time_val_to_iso8601 (GTimeVal *time_);
    1.10 +gboolean
    1.11 +g_time_val_from_iso8601 (const gchar *iso_date,
    1.12 +                         GTimeVal    *time_);
    1.13 +void
    1.14 +g_date_set_time_val (GDate    *date,
    1.15 +             GTimeVal *timeval);
    1.16 +
    1.17 +#endif
    1.18 +
    1.19  static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.20  
    1.21  /** Converts a time_t struct in a GString at ISO standard format 
    1.22 @@ -415,3 +427,195 @@
    1.23      return res;    
    1.24  }
    1.25  
    1.26 +
    1.27 +#if !GLIB_CHECK_VERSION (2, 10, 0)
    1.28 +
    1.29 +
    1.30 +/* Hacked from glib 2.10 <gtime.c> */
    1.31 +
    1.32 +static time_t
    1.33 +mktime_utc (struct tm *tm)
    1.34 +{
    1.35 +  time_t retval;
    1.36 +  
    1.37 +#ifndef HAVE_TIMEGM
    1.38 +  static const gint days_before[] =
    1.39 +  {
    1.40 +    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
    1.41 +  };
    1.42 +#endif
    1.43 +
    1.44 +#ifndef HAVE_TIMEGM
    1.45 +  if (tm->tm_mon < 0 || tm->tm_mon > 11)
    1.46 +    return (time_t) -1;
    1.47 +
    1.48 +  retval = (tm->tm_year - 70) * 365;
    1.49 +  retval += (tm->tm_year - 68) / 4;
    1.50 +  retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
    1.51 +  
    1.52 +  if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
    1.53 +    retval -= 1;
    1.54 +  
    1.55 +  retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
    1.56 +#else
    1.57 +  retval = timegm (tm);
    1.58 +#endif /* !HAVE_TIMEGM */
    1.59 +  
    1.60 +  return retval;
    1.61 +}
    1.62 +
    1.63 +gboolean
    1.64 +g_time_val_from_iso8601 (const gchar *iso_date,
    1.65 +                         GTimeVal    *time_)
    1.66 +{
    1.67 +  struct tm tm;
    1.68 +  long val;
    1.69 +
    1.70 +  g_return_val_if_fail (iso_date != NULL, FALSE);
    1.71 +  g_return_val_if_fail (time_ != NULL, FALSE);
    1.72 +
    1.73 +  val = strtoul (iso_date, (char **)&iso_date, 10);
    1.74 +  if (*iso_date == '-')
    1.75 +    {
    1.76 +      /* YYYY-MM-DD */
    1.77 +      tm.tm_year = val - 1900;
    1.78 +      iso_date++;
    1.79 +      tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
    1.80 +      
    1.81 +      if (*iso_date++ != '-')
    1.82 +        return FALSE;
    1.83 +      
    1.84 +      tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
    1.85 +    }
    1.86 +  else
    1.87 +    {
    1.88 +      /* YYYYMMDD */
    1.89 +      tm.tm_mday = val % 100;
    1.90 +      tm.tm_mon = (val % 10000) / 100 - 1;
    1.91 +      tm.tm_year = val / 10000 - 1900;
    1.92 +    }
    1.93 +
    1.94 +  if (*iso_date++ != 'T')
    1.95 +    return FALSE;
    1.96 +  
    1.97 +  val = strtoul (iso_date, (char **)&iso_date, 10);
    1.98 +  if (*iso_date == ':')
    1.99 +    {
   1.100 +      /* hh:mm:ss */
   1.101 +      tm.tm_hour = val;
   1.102 +      iso_date++;
   1.103 +      tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
   1.104 +      
   1.105 +      if (*iso_date++ != ':')
   1.106 +        return FALSE;
   1.107 +      
   1.108 +      tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
   1.109 +    }
   1.110 +  else
   1.111 +    {
   1.112 +      /* hhmmss */
   1.113 +      tm.tm_sec = val % 100;
   1.114 +      tm.tm_min = (val % 10000) / 100;
   1.115 +      tm.tm_hour = val / 10000;
   1.116 +    }
   1.117 +
   1.118 +  time_->tv_sec = mktime_utc (&tm);
   1.119 +  time_->tv_usec = 1;
   1.120 +  
   1.121 +  if (*iso_date == '.')
   1.122 +    time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
   1.123 +    
   1.124 +  if (*iso_date == '+' || *iso_date == '-')
   1.125 +    {
   1.126 +      gint sign = (*iso_date == '+') ? -1 : 1;
   1.127 +      
   1.128 +      val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
   1.129 +      
   1.130 +      if (*iso_date == ':')
   1.131 +    val = 60 * val + strtoul (iso_date + 1, NULL, 10);
   1.132 +      else
   1.133 +        val = 60 * (val / 100) + (val % 100);
   1.134 +
   1.135 +      time_->tv_sec += (time_t) (val * sign);
   1.136 +    }
   1.137 +
   1.138 +  return TRUE;
   1.139 +}
   1.140 +
   1.141 +
   1.142 +gchar *
   1.143 +g_time_val_to_iso8601 (GTimeVal *time_)
   1.144 +{
   1.145 +  gchar *retval;
   1.146 +
   1.147 +  g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
   1.148 +
   1.149 +#define ISO_8601_LEN    21
   1.150 +#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
   1.151 +  retval = g_new0 (gchar, ISO_8601_LEN + 1);
   1.152 +  
   1.153 +  strftime (retval, ISO_8601_LEN,
   1.154 +        ISO_8601_FORMAT,
   1.155 +        gmtime (&(time_->tv_sec)));
   1.156 +  
   1.157 +  return retval;
   1.158 +}
   1.159 +
   1.160 +
   1.161 +/* Hacked from glib 2.10 <gdate.c> */
   1.162 +
   1.163 +void         
   1.164 +g_date_set_time_t (GDate *date,
   1.165 +           time_t timet)
   1.166 +{
   1.167 +  struct tm tm;
   1.168 +  
   1.169 +  g_return_if_fail (date != NULL);
   1.170 +  
   1.171 +#ifdef HAVE_LOCALTIME_R
   1.172 +  localtime_r (&timet, &tm);
   1.173 +#else
   1.174 +  {
   1.175 +    struct tm *ptm = localtime (&timet);
   1.176 +
   1.177 +    if (ptm == NULL)
   1.178 +      {
   1.179 +    /* Happens at least in Microsoft's C library if you pass a
   1.180 +     * negative time_t. Use 2000-01-01 as default date.
   1.181 +     */
   1.182 +#ifndef G_DISABLE_CHECKS
   1.183 +    g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
   1.184 +#endif
   1.185 +
   1.186 +    tm.tm_mon = 0;
   1.187 +    tm.tm_mday = 1;
   1.188 +    tm.tm_year = 100;
   1.189 +      }
   1.190 +    else
   1.191 +      memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
   1.192 +  }
   1.193 +#endif
   1.194 +  
   1.195 +  date->julian = FALSE;
   1.196 +  
   1.197 +  date->month = tm.tm_mon + 1;
   1.198 +  date->day   = tm.tm_mday;
   1.199 +  date->year  = tm.tm_year + 1900;
   1.200 +  
   1.201 +  g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
   1.202 +  
   1.203 +  date->dmy    = TRUE;
   1.204 +}
   1.205 +
   1.206 +
   1.207 +void
   1.208 +g_date_set_time_val (GDate    *date,
   1.209 +             GTimeVal *timeval)
   1.210 +{
   1.211 +  g_date_set_time_t (date, (time_t) timeval->tv_sec);
   1.212 +}
   1.213 +
   1.214 +
   1.215 +
   1.216 +
   1.217 +#endif