Wed Aug 18 22:34:26 2010

Asterisk developer's documentation


localtime.h File Reference

Custom localtime functions for multiple timezones. More...

Go to the source code of this file.

Data Structures

struct  ast_tm

Functions

void ast_get_dst_info (const time_t *const timep, int *dst_enabled, time_t *dst_start, time_t *dst_end, int *gmt_off, const char *const zone)
ast_tmast_localtime (const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
timeval ast_mktime (struct ast_tm *const tmp, const char *zone)
int ast_strftime (char *buf, size_t len, const char *format, const struct ast_tm *tm)


Detailed Description

Custom localtime functions for multiple timezones.

Definition in file localtime.h.


Function Documentation

void ast_get_dst_info ( const time_t *const   timep,
int *  dst_enabled,
time_t *  dst_start,
time_t *  dst_end,
int *  gmt_off,
const char *const   zone 
)

Definition at line 1156 of file localtime.c.

References ast_tzset(), state::ats, AVGSECSPERYEAR, state::goahead, state::goback, state::timecnt, ttinfo::tt_gmtoff, ttinfo::tt_isdst, state::ttis, state::typecnt, state::types, and YEARSPERREPEAT.

Referenced by set_timezone_variables().

01157 {
01158    int i;   
01159    int transition1 = -1;
01160    int transition2 = -1;
01161    time_t      seconds;
01162    int  bounds_exceeded = 0;
01163    time_t  t = *timep;
01164    const struct state *sp;
01165    
01166    if (NULL == dst_enabled)
01167       return;
01168    *dst_enabled = 0;
01169 
01170    if (NULL == dst_start || NULL == dst_end || NULL == gmt_off)
01171       return;
01172 
01173    *gmt_off = 0; 
01174    
01175    sp = ast_tzset(zone);
01176    if (NULL == sp) 
01177       return;
01178    
01179    /* If the desired time exceeds the bounds of the defined time transitions  
01180    * then give give up on determining DST info and simply look for gmt offset 
01181    * This requires that I adjust the given time using increments of Gregorian 
01182    * repeats to place the time within the defined time transitions in the 
01183    * timezone structure.  
01184    */
01185    if ((sp->goback && t < sp->ats[0]) ||
01186          (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
01187       time_t      tcycles;
01188       int_fast64_t   icycles;
01189 
01190       if (t < sp->ats[0])
01191          seconds = sp->ats[0] - t;
01192       else  seconds = t - sp->ats[sp->timecnt - 1];
01193       --seconds;
01194       tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
01195       ++tcycles;
01196       icycles = tcycles;
01197       if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
01198          return;
01199       seconds = icycles;
01200       seconds *= YEARSPERREPEAT;
01201       seconds *= AVGSECSPERYEAR;
01202       if (t < sp->ats[0])
01203          t += seconds;
01204       else
01205          t -= seconds;
01206       
01207       if (t < sp->ats[0] || t > sp->ats[sp->timecnt - 1])
01208          return;  /* "cannot happen" */
01209 
01210       bounds_exceeded = 1;
01211    }
01212 
01213    if (sp->timecnt == 0 || t < sp->ats[0]) {
01214       /* I have no transition times or I'm before time */
01215       *dst_enabled = 0;
01216       /* Find where I can get gmtoff */
01217       i = 0;
01218       while (sp->ttis[i].tt_isdst)
01219          if (++i >= sp->typecnt) {
01220          i = 0;
01221          break;
01222          }
01223          *gmt_off = sp->ttis[i].tt_gmtoff;
01224          return;
01225    } 
01226 
01227    for (i = 1; i < sp->timecnt; ++i) {
01228       if (t < sp->ats[i]) {
01229          transition1 = sp->types[i - 1];
01230          transition2 = sp->types[i];
01231          break;
01232       } 
01233    }
01234    /* if I found transition times that do not bounded the given time and these correspond to 
01235       or the bounding zones do not reflect a changes in day light savings, then I do not have dst active */
01236    if (i >= sp->timecnt || 0 > transition1 || 0 > transition2 ||
01237          (sp->ttis[transition1].tt_isdst == sp->ttis[transition2].tt_isdst)) {
01238       *dst_enabled = 0;
01239       *gmt_off     = sp->ttis[sp->types[sp->timecnt -1]].tt_gmtoff;
01240    } else {
01241       /* I have valid daylight savings information. */
01242       if(sp->ttis[transition2].tt_isdst) 
01243          *gmt_off = sp->ttis[transition1].tt_gmtoff;
01244       else 
01245          *gmt_off = sp->ttis[transition2].tt_gmtoff;
01246 
01247       /* If I adjusted the time earlier, indicate that the dst is invalid */
01248       if (!bounds_exceeded) {
01249          *dst_enabled = 1;
01250          /* Determine which of the bounds is the start of daylight savings and which is the end */
01251          if(sp->ttis[transition2].tt_isdst) {
01252             *dst_start = sp->ats[i];
01253             *dst_end = sp->ats[i -1];
01254          } else {
01255             *dst_start = sp->ats[i -1];
01256             *dst_end = sp->ats[i];
01257          }
01258       }
01259    }  
01260    return;
01261 }

struct ast_tm* ast_localtime ( const struct timeval *  timep,
struct ast_tm p_tm,
const char *  zone 
)

Definition at line 1141 of file localtime.c.

References ast_tzset(), and localsub().

Referenced by __ast_verbose_ap(), acf_strftime(), action_corestatus(), append_date(), ast_check_timing(), ast_log(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), ast_say_date_ka(), ast_say_date_nl(), ast_say_date_pt(), ast_say_date_th(), ast_say_date_with_format_da(), ast_say_date_with_format_de(), ast_say_date_with_format_en(), ast_say_date_with_format_es(), ast_say_date_with_format_fr(), ast_say_date_with_format_gr(), ast_say_date_with_format_he(), ast_say_date_with_format_it(), ast_say_date_with_format_nl(), ast_say_date_with_format_pl(), ast_say_date_with_format_pt(), ast_say_date_with_format_th(), ast_say_date_with_format_zh(), ast_say_datetime_de(), ast_say_datetime_en(), ast_say_datetime_fr(), ast_say_datetime_from_now_en(), ast_say_datetime_from_now_fr(), ast_say_datetime_from_now_he(), ast_say_datetime_from_now_ka(), ast_say_datetime_from_now_pt(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_hu(), ast_say_datetime_ka(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_pt_BR(), ast_say_datetime_th(), ast_say_datetime_zh(), ast_say_time_de(), ast_say_time_en(), ast_say_time_fr(), ast_say_time_gr(), ast_say_time_he(), ast_say_time_hu(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_pt(), ast_say_time_pt_BR(), ast_say_time_th(), ast_say_time_zh(), build_device(), build_radius_record(), callerid_genmsg(), cdr_get_tv(), cli_prompt(), enc_ie_date(), execute_cb(), find_conf_realtime(), get_date(), handle_minivm_show_stats(), handle_show_settings(), handle_time_date_req_message(), httpd_helper_thread(), iax2_datetime(), isodate(), leave_voicemail(), main(), make_email_file(), manager_log(), packdate(), pgsql_log(), phone_call(), phoneprov_callback(), play_message_datetime(), prep_email_sub_vars(), rpt_localtime(), say_date_generic(), send_date_time(), send_date_time2(), send_date_time3(), sendmail(), set_timezone_variables(), sip_show_registry(), sms_compose2(), sms_handleincoming_proto2(), sqlite_log(), static_callback(), timeout_write(), transmit_notify_request_with_callerid(), vmu_tm(), write_history(), and write_metadata().

01142 {
01143    const struct state *sp = ast_tzset(zone);
01144    memset(tmp, 0, sizeof(*tmp));
01145    return sp ? localsub(timep, 0L, tmp, sp) : NULL;
01146 }

struct timeval ast_mktime ( struct ast_tm *const   tmp,
const char *  zone 
)

Definition at line 1756 of file localtime.c.

References ast_tzset(), localsub(), and time1().

Referenced by acf_strptime(), find_conf_realtime(), sms_handleincoming_proto2(), sms_readfile(), and unpackdate().

01757 {
01758    const struct state *sp;
01759    if (!(sp = ast_tzset(zone)))
01760       return WRONG;
01761    return time1(tmp, localsub, 0L, sp);
01762 }

int ast_strftime ( char *  buf,
size_t  len,
const char *  format,
const struct ast_tm tm 
)

Definition at line 1764 of file localtime.c.

References ast_calloc, ast_free, ast_realloc, format, and ast_tm::tm_usec.

Referenced by __ast_verbose_ap(), acf_strftime(), action_corestatus(), append_date(), ast_log(), build_radius_record(), cdr_get_tv(), cli_prompt(), dump_datetime(), execute_cb(), find_conf_realtime(), get_date(), handle_minivm_show_stats(), handle_show_settings(), httpd_helper_thread(), isodate(), leave_voicemail(), make_email_file(), manager_log(), pgsql_log(), phoneprov_callback(), prep_email_sub_vars(), sendmail(), sendpage(), sip_show_registry(), sqlite_log(), static_callback(), timeout_write(), and write_metadata().

01765 {
01766    size_t fmtlen = strlen(tmp) + 1;
01767    char *format = ast_calloc(1, fmtlen), *fptr = format, *newfmt;
01768    int decimals = -1, i, res;
01769    long fraction;
01770 
01771    if (!format)
01772       return -1;
01773    for (; *tmp; tmp++) {
01774       if (*tmp == '%') {
01775          switch (tmp[1]) {
01776          case '1':
01777          case '2':
01778          case '3':
01779          case '4':
01780          case '5':
01781          case '6':
01782             if (tmp[2] != 'q')
01783                goto defcase;
01784             decimals = tmp[1] - '0';
01785             tmp++;
01786             /* Fall through */
01787          case 'q': /* Milliseconds */
01788             if (decimals == -1)
01789                decimals = 3;
01790 
01791             /* Juggle some memory to fit the item */
01792             newfmt = ast_realloc(format, fmtlen + decimals);
01793             if (!newfmt) {
01794                ast_free(format);
01795                return -1;
01796             }
01797             fptr = fptr - format + newfmt;
01798             format = newfmt;
01799             fmtlen += decimals;
01800 
01801             /* Reduce the fraction of time to the accuracy needed */
01802             for (i = 6, fraction = tm->tm_usec; i > decimals; i--)
01803                fraction /= 10;
01804             fptr += sprintf(fptr, "%0*ld", decimals, fraction);
01805 
01806             /* Reset, in case more than one 'q' specifier exists */
01807             decimals = -1;
01808             tmp++;
01809             break;
01810          default:
01811             goto defcase;
01812          }
01813       } else
01814 defcase: *fptr++ = *tmp;
01815    }
01816    *fptr = '\0';
01817 #undef strftime
01818    res = (int)strftime(buf, len, format, (struct tm *)tm);
01819    ast_free(format);
01820    return res;
01821 }


Generated on Wed Aug 18 22:34:26 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7