Ticket #252: utc.patch

File utc.patch, 7.1 KB (added by human, 2 years ago)

Add --utc option to rrd graph

  • src/rrd_graph.c

     
    2626#endif 
    2727 
    2828#include <time.h> 
     29#define LOCALTIME_R(a,b,c) (c ? gmtime_r(a,b) : localtime_r(a,b)) 
     30#define MKTIME(a,b) (b ? timegm(a) : mktime(a)) 
    2931 
    3032#include <locale.h> 
    3133 
     
    13811383time_t find_first_time( 
    13821384    time_t start,       /* what is the initial time */ 
    13831385    enum tmt_en baseint,    /* what is the basic interval */ 
    1384     long basestep       /* how many if these do we jump a time */ 
     1386    long basestep,      /* how many of these do we jump a time */ 
     1387    int utc 
    13851388    ) 
    13861389{ 
    13871390    struct tm tm; 
    13881391 
    1389     localtime_r(&start, &tm); 
     1392    LOCALTIME_R(&start, &tm, utc); 
    13901393 
    13911394    switch (baseint) { 
    13921395    case TMT_SECOND: 
     
    14411444    tm.tm_year + 1900) %basestep; 
    14421445 
    14431446    } 
    1444     return mktime(&tm); 
     1447    return MKTIME(&tm, utc); 
    14451448} 
    14461449 
    14471450/* identify the point where the next gridline, label ... gets placed */ 
    14481451time_t find_next_time( 
    14491452    time_t current,     /* what is the initial time */ 
    14501453    enum tmt_en baseint,    /* what is the basic interval */ 
    1451     long basestep       /* how many if these do we jump a time */ 
     1454    long basestep,      /* how many of these do we jump a time */ 
     1455    int utc 
    14521456    ) 
    14531457{ 
    14541458    struct tm tm; 
    14551459    time_t    madetime; 
    14561460 
    1457     localtime_r(&current, &tm); 
     1461    LOCALTIME_R(&current, &tm, utc); 
    14581462 
    14591463    do { 
    14601464        switch (baseint) { 
     
    14851489        case TMT_YEAR: 
    14861490            tm.       tm_year += basestep; 
    14871491        } 
    1488         madetime = mktime(&tm); 
     1492        madetime = MKTIME(&tm, utc); 
    14891493    } while (madetime == -1);   /* this is necessary to skip impssible times 
    14901494                                   like the daylight saving time skips */ 
    14911495    return madetime; 
     
    15121516    /* wow initializing tmvdef is quite a task :-) */ 
    15131517    time_t    now = time(NULL); 
    15141518 
    1515     localtime_r(&now, &tmvdef); 
     1519    LOCALTIME_R(&now, &tmvdef, im->extra_flags & FORCE_UTC_TIME); 
    15161520    for (i = 0; i < im->gdes_c; i++) { 
    15171521        vidx = im->gdes[i].vidx; 
    15181522        switch (im->gdes[i].gf) { 
     
    15241528             */ 
    15251529            if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */ 
    15261530                printval = im->gdes[vidx].vf.val; 
    1527                 localtime_r(&im->gdes[vidx].vf.when, &tmvdef); 
     1531                LOCALTIME_R(&im->gdes[vidx].vf.when, &tmvdef, im->extra_flags & FORCE_UTC_TIME); 
    15281532            } else {    /* need to calculate max,min,avg etcetera */ 
    15291533                max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start) 
    15301534                          / im->gdes[vidx].step * im->gdes[vidx].ds_cnt); 
     
    24762480                                  gridtm, 
    24772481                                  im-> 
    24782482                                  xlab_user. 
    2479                                   gridst), 
     2483                                  gridst, 
     2484                                  im->extra_flags & FORCE_UTC_TIME), 
    24802485             timajor = 
    24812486             find_first_time(im->start, 
    24822487                             im->xlab_user. 
    24832488                             mgridtm, 
    24842489                             im->xlab_user. 
    2485                              mgridst); 
     2490                             mgridst, 
     2491                             im->extra_flags & FORCE_UTC_TIME); 
    24862492             ti < im->end; 
    24872493             ti = 
    2488              find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst) 
     2494             find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst, im->extra_flags & FORCE_UTC_TIME) 
    24892495            ) { 
    24902496            /* are we inside the graph ? */ 
    24912497            if (ti < im->start || ti > im->end) 
     
    24942500                timajor = find_next_time(timajor, 
    24952501                                         im-> 
    24962502                                         xlab_user. 
    2497                                          mgridtm, im->xlab_user.mgridst); 
     2503                                         mgridtm, im->xlab_user.mgridst, 
     2504                                         im->extra_flags & FORCE_UTC_TIME); 
    24982505            } 
    24992506            if (ti == timajor) 
    25002507                continue;   /* skip as falls on major grid line */ 
     
    25182525                              mgridtm, 
    25192526                              im-> 
    25202527                              xlab_user. 
    2521                               mgridst); 
     2528                              mgridst, 
     2529                              im->extra_flags & FORCE_UTC_TIME); 
    25222530         ti < im->end; 
    2523          ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst) 
     2531         ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst, im->extra_flags & FORCE_UTC_TIME) 
    25242532        ) { 
    25252533        /* are we inside the graph ? */ 
    25262534        if (ti < im->start || ti > im->end) 
     
    25442552                         im->xlab_user. 
    25452553                         labtm, 
    25462554                         im->xlab_user. 
    2547                          labst); 
     2555                         labst, 
     2556                         im->extra_flags & FORCE_UTC_TIME); 
    25482557         ti <= 
    25492558         im->end - 
    25502559         im->xlab_user.precis / 2; 
    2551          ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst) 
     2560         ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst, im->extra_flags & FORCE_UTC_TIME) 
    25522561        ) { 
    25532562        tilab = ti + im->xlab_user.precis / 2;  /* correct time for the label */ 
    25542563        /* are we inside the graph ? */ 
    25552564        if (tilab < im->start || tilab > im->end) 
    25562565            continue; 
    25572566#if HAVE_STRFTIME 
    2558         localtime_r(&tilab, &tm); 
     2567        LOCALTIME_R(&tilab, &tm, im->extra_flags & FORCE_UTC_TIME); 
    25592568        strftime(graph_label, 99, im->xlab_user.stst, &tm); 
    25602569#else 
    25612570# error "your libc has no strftime I guess we'll abort the exercise here." 
     
    42024211        { "border",             required_argument, 0, 1007}, 
    42034212        { "grid-dash",          required_argument, 0, 1008}, 
    42044213        { "dynamic-labels",     no_argument,       0, 1009}, 
     4214        { "utc",                no_argument,       0, 1010}, 
    42054215        {  0, 0, 0, 0} 
    42064216}; 
    42074217/* *INDENT-ON* */ 
     
    42724282        case 1001: 
    42734283            im->extra_flags |= NO_RRDTOOL_TAG; 
    42744284            break; 
     4285        case 1010: 
     4286            im->extra_flags |= FORCE_UTC_TIME; 
     4287            break; 
    42754288        case LONGOPT_UNITS_SI: 
    42764289            if (im->extra_flags & FORCE_UNITS) { 
    42774290                rrd_set_error("--units can only be used once!"); 
  • src/rrd_graph.h

     
    3939 
    4040#define FULL_SIZE_MODE     0x200    /* -width and -height indicate the total size of the image */ 
    4141#define NO_RRDTOOL_TAG 0x400  /* disable the rrdtool tag */ 
     42#define FORCE_UTC_TIME  0x800   /* Work in UTC timezone instead of localtimg */ 
    4243 
    4344enum tmt_en { TMT_SECOND = 0, TMT_MINUTE, TMT_HOUR, TMT_DAY, 
    4445    TMT_WEEK, TMT_MONTH, TMT_YEAR 
     
    334335time_t    find_first_time( 
    335336    time_t, 
    336337    enum tmt_en, 
    337     long); 
     338    long, 
     339    int); 
    338340time_t    find_next_time( 
    339341    time_t, 
    340342    enum tmt_en, 
    341     long); 
     343    long, 
     344    int); 
    342345int       print_calc( 
    343346    image_desc_t *); 
    344347int       leg_place( 

NOTE: The content of this website is accessible with any browser. The graphical design though relies completely on CSS2 styles. If you see this text, this means that your browser does not support CSS2. Consider upgrading to a standard conformant browser like Mozilla Firefox or Opera but also Apple's Safari or KDE's Konqueror for example. It may also be that you are looking at a mirror page which did not copy the CSS for this page. Or if some pictu res are missing, then the mirror may not have picked up the contents of the inc directory.