Changeset 2e3a857


Ignore:
Timestamp:
2010-04-08T00:27:42Z (15 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
08579a1
Parents:
91cec2f
Message:

Added a mktime_utc() to misc.c using code that used to be in jabber_util.c.
I want to use this in the Twitter module.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    r91cec2f r2e3a857  
    7777       
    7878        return mktime(&tm);
     79}
     80
     81time_t mktime_utc( struct tm *tp )
     82{
     83        struct tm utc;
     84        time_t res, tres;
     85       
     86        tp->tm_isdst = -1;
     87        res = mktime( tp );
     88        /* Problem is, mktime() just gave us the GMT timestamp for the
     89           given local time... While the given time WAS NOT local. So
     90           we should fix this now.
     91           
     92           Now I could choose between messing with environment variables
     93           (kludgy) or using timegm() (not portable)... Or doing the
     94           following, which I actually prefer...
     95           
     96           tzset() may also work but in other places I actually want to
     97           use local time.
     98           
     99           FFFFFFFFFFFFFFFFFFFFFUUUUUUUUUUUUUUUUUUUU!! */
     100        gmtime_r( &res, &utc );
     101        utc.tm_isdst = -1;
     102        if( utc.tm_hour == tp->tm_hour && utc.tm_min == tp->tm_min )
     103                /* Sweet! We're in UTC right now... */
     104                return res;
     105       
     106        tres = mktime( &utc );
     107        res += res - tres;
     108       
     109        /* Yes, this is a hack. And it will go wrong around DST changes.
     110           BUT this is more likely to be threadsafe than messing with
     111           environment variables, and possibly more portable... */
     112       
     113        return res;
    79114}
    80115
  • lib/misc.h

    r91cec2f r2e3a857  
    4343
    4444G_MODULE_EXPORT time_t get_time( int year, int month, int day, int hour, int min, int sec );
     45G_MODULE_EXPORT time_t mktime_utc( struct tm *tp );
    4546double gettime( void );
    4647
  • protocols/jabber/jabber_util.c

    r91cec2f r2e3a857  
    667667time_t jabber_get_timestamp( struct xt_node *xt )
    668668{
    669         struct tm tp, utc;
    670669        struct xt_node *c;
    671         time_t res, tres;
    672670        char *s = NULL;
     671        struct tm tp;
    673672       
    674673        for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
     
    688687        tp.tm_year -= 1900;
    689688        tp.tm_mon --;
    690         tp.tm_isdst = -1; /* GRRRRRRRRRRR */
    691        
    692         res = mktime( &tp );
    693         /* Problem is, mktime() just gave us the GMT timestamp for the
    694            given local time... While the given time WAS NOT local. So
    695            we should fix this now.
    696        
    697            Now I could choose between messing with environment variables
    698            (kludgy) or using timegm() (not portable)... Or doing the
    699            following, which I actually prefer... */
    700         gmtime_r( &res, &utc );
    701         utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */
    702         if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min )
    703                 /* Sweet! We're in UTC right now... */
    704                 return res;
    705        
    706         tres = mktime( &utc );
    707         res += res - tres;
    708        
    709         /* Yes, this is a hack. And it will go wrong around DST changes.
    710            BUT this is more likely to be threadsafe than messing with
    711            environment variables, and possibly more portable... */
    712        
    713         return res;
     689       
     690        return mktime_utc( &tp );
    714691}
    715692
Note: See TracChangeset for help on using the changeset viewer.