Back to index

php5  5.3.10
Functions | Variables
tm2unixtime.c File Reference
#include "timelib.h"

Go to the source code of this file.

Functions

static int do_range_limit (timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
static void inc_month (timelib_sll *y, timelib_sll *m)
static void dec_month (timelib_sll *y, timelib_sll *m)
static void do_range_limit_days_relative (timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
static int do_range_limit_days (timelib_sll *y, timelib_sll *m, timelib_sll *d)
static void do_adjust_for_weekday (timelib_time *time)
void timelib_do_rel_normalize (timelib_time *base, timelib_rel_time *rt)
void timelib_do_normalize (timelib_time *time)
static void do_adjust_relative (timelib_time *time)
static void do_adjust_special_weekday (timelib_time *time)
static void do_adjust_special (timelib_time *time)
static void do_adjust_special_early (timelib_time *time)
static timelib_sll do_years (timelib_sll year)
static timelib_sll do_months (timelib_ull month, timelib_ull year)
static timelib_sll do_days (timelib_ull day)
static timelib_sll do_time (timelib_ull hour, timelib_ull minute, timelib_ull second)
static timelib_sll do_adjust_timezone (timelib_time *tz, timelib_tzinfo *tzi)
void timelib_update_ts (timelib_time *time, timelib_tzinfo *tzi)

Variables

static int month_tab_leap [12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }
static int month_tab [12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }
static int days_in_month_leap [13] = { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
static int days_in_month [13] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

Function Documentation

static void dec_month ( timelib_sll y,
timelib_sll m 
) [static]

Definition at line 53 of file tm2unixtime.c.

{
       (*m)--;
       if (*m < 1) {
              *m += 12;
              (*y)--;
       }
}

Here is the caller graph for this function:

static void do_adjust_for_weekday ( timelib_time time) [static]

Definition at line 145 of file tm2unixtime.c.

{
       timelib_sll current_dow, difference;

       current_dow = timelib_day_of_week(time->y, time->m, time->d);
       if (time->relative.weekday_behavior == 2)
       {
              if (time->relative.weekday == 0) {
                     time->relative.weekday = 7;
              }
              time->d -= current_dow;
              time->d += time->relative.weekday;
              return;
       }
       difference = time->relative.weekday - current_dow;
       if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
              difference += 7;
       }
       if (time->relative.weekday >= 0) {
              time->d += difference;
       } else {
              time->d -= (7 - (abs(time->relative.weekday) - current_dow));
       }
       time->relative.have_weekday_relative = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void do_adjust_relative ( timelib_time time) [static]

Definition at line 193 of file tm2unixtime.c.

{
       if (time->relative.have_weekday_relative) {
              do_adjust_for_weekday(time);
       }
       timelib_do_normalize(time);

       if (time->have_relative) {
              time->s += time->relative.s;
              time->i += time->relative.i;
              time->h += time->relative.h;

              time->d += time->relative.d;
              time->m += time->relative.m;
              time->y += time->relative.y;
       }
       switch (time->relative.first_last_day_of) {
              case 1: /* first */
                     time->d = 1;
                     break;
              case 2: /* last */
                     time->d = 0;
                     time->m++;
                     break;
       }
       timelib_do_normalize(time);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void do_adjust_special ( timelib_time time) [static]

Definition at line 274 of file tm2unixtime.c.

{
       if (time->relative.have_special_relative) {
              switch (time->relative.special.type) {
                     case TIMELIB_SPECIAL_WEEKDAY:
                            do_adjust_special_weekday(time);
                            break;
              }
       }
       timelib_do_normalize(time);
       memset(&(time->relative.special), 0, sizeof(time->relative.special));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void do_adjust_special_early ( timelib_time time) [static]

Definition at line 287 of file tm2unixtime.c.

{
       if (time->relative.have_special_relative) {
              switch (time->relative.special.type) {
                     case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
                            time->d = 1;
                            time->m += time->relative.m;
                            time->relative.m = 0;
                            break;
                     case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
                            time->d = 1;
                            time->m += time->relative.m + 1;
                            time->relative.m = 0;
                            break;
              }
       }
       timelib_do_normalize(time);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void do_adjust_special_weekday ( timelib_time time) [static]

Definition at line 221 of file tm2unixtime.c.

{
       timelib_sll current_dow, count;

       count = time->relative.special.amount;

       current_dow = timelib_day_of_week(time->y, time->m, time->d);
       if (count == 0) {
              /* skip over saturday and sunday */
              if (current_dow == 6) {
                     time->d += 2;
              }
              /* skip over sunday */
              if (current_dow == 0) {
                     time->d += 1;
              }
       } else if (count > 0) {
              /* skip over saturday and sunday */
              if (current_dow == 5) {
                     time->d += 2;
              }
              /* skip over sunday */
              if (current_dow == 6) {
                     time->d += 1;
              }
              /* add increments of 5 weekdays as a week */
              time->d += (count / 5) * 7;
              /* if current DOW plus the remainder > 5, add two days */
              current_dow = timelib_day_of_week(time->y, time->m, time->d);
              time->d += (count % 5);
              if ((count % 5) + current_dow > 5) {
                     time->d += 2;
              }
       } else if (count < 0) {
              /* skip over sunday and saturday */
              if (current_dow == 1) {
                     time->d -= 2;
              }
              /* skip over satruday */
              if (current_dow == 0 ) {
                     time->d -= 1;
              }
              /* subtract increments of 5 weekdays as a week */
              time->d += (count / 5) * 7;
              /* if current DOW minus the remainder < 0, subtract two days */
              current_dow = timelib_day_of_week(time->y, time->m, time->d);
              time->d += (count % 5);
              if ((count % 5) + current_dow < 1) {
                     time->d -= 2;
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static timelib_sll do_adjust_timezone ( timelib_time tz,
timelib_tzinfo tzi 
) [static]

Definition at line 362 of file tm2unixtime.c.

{
       switch (tz->zone_type) {
              case TIMELIB_ZONETYPE_OFFSET:

                     tz->is_localtime = 1;
                     return tz->z * 60;
                     break;

              case TIMELIB_ZONETYPE_ABBR: {
                     timelib_sll tmp;

                     tz->is_localtime = 1;
                     tmp = tz->z;
                     tmp -= tz->dst * 60;
                     tmp *= 60;
                     return tmp;
                     }
                     break;

              case TIMELIB_ZONETYPE_ID:
                     tzi = tz->tz_info;
                     /* Break intentionally missing */

              default:
                     /* No timezone in struct, fallback to reference if possible */
                     if (tzi) {
                            timelib_time_offset *before, *after;
                            timelib_sll          tmp;
                            int                  in_transistion;
                            
                            tz->is_localtime = 1;
                            before = timelib_get_time_zone_info(tz->sse, tzi);
                            after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
                            timelib_set_timezone(tz, tzi);

                            in_transistion = (
                                   ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
                                   ((tz->sse - after->offset) < after->transistion_time)
                            );
                            
                            if ((before->offset != after->offset) && !in_transistion) {
                                   tmp = -after->offset;
                            } else {
                                   tmp = -tz->z;
                            }
                            timelib_time_offset_dtor(before);
                            timelib_time_offset_dtor(after);

                            {
                                   timelib_time_offset *gmt_offset;

                                   gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
                                   tz->z = gmt_offset->offset;

                                   tz->dst = gmt_offset->is_dst;
                                   if (tz->tz_abbr) {
                                          free(tz->tz_abbr);
                                   }
                                   tz->tz_abbr = strdup(gmt_offset->abbr);
                                   timelib_time_offset_dtor(gmt_offset);
                            }
                            return tmp;
                     }
       }
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static timelib_sll do_days ( timelib_ull  day) [static]

Definition at line 347 of file tm2unixtime.c.

{
       return ((day - 1) * SECS_PER_DAY);
}

Here is the caller graph for this function:

static timelib_sll do_months ( timelib_ull  month,
timelib_ull  year 
) [static]

Definition at line 338 of file tm2unixtime.c.

{
       if (timelib_is_leap(year)) {
              return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
       } else {
              return ((month_tab[month - 1]) * SECS_PER_DAY);
       }
}

Here is the caller graph for this function:

static int do_range_limit ( timelib_sll  start,
timelib_sll  end,
timelib_sll  adj,
timelib_sll a,
timelib_sll b 
) [static]

Definition at line 31 of file tm2unixtime.c.

{
       if (*a < start) {
              *b -= (start - *a - 1) / adj + 1;
              *a += adj * ((start - *a - 1) / adj + 1);
       }
       if (*a >= end) {
              *b += *a / adj;
              *a -= adj * (*a / adj);
       }
       return 0;
}

Here is the caller graph for this function:

static int do_range_limit_days ( timelib_sll y,
timelib_sll m,
timelib_sll d 
) [static]

Definition at line 104 of file tm2unixtime.c.

{
       timelib_sll leapyear;
       timelib_sll days_this_month;
       timelib_sll last_month, last_year;
       timelib_sll days_last_month;
       
       /* can jump an entire leap year period quickly */
       if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
              *y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
              *d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
       }

       do_range_limit(1, 13, 12, m, y);

       leapyear = timelib_is_leap(*y);
       days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
       last_month = (*m) - 1;

       if (last_month < 1) {
              last_month += 12;
              last_year = (*y) - 1;
       } else {
              last_year = (*y);
       }
       leapyear = timelib_is_leap(last_year);
       days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];

       if (*d <= 0) {
              *d += days_last_month;
              (*m)--;
              return 1;
       }
       if (*d > days_this_month) {
              *d -= days_this_month;
              (*m)++;
              return 1;
       }
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void do_range_limit_days_relative ( timelib_sll base_y,
timelib_sll base_m,
timelib_sll y,
timelib_sll m,
timelib_sll d,
timelib_sll  invert 
) [static]

Definition at line 62 of file tm2unixtime.c.

{
       timelib_sll leapyear;
       timelib_sll month, year;
       timelib_sll days;

       do_range_limit(1, 13, 12, base_m, base_y);

       year = *base_y;
       month = *base_m;

/*
       printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
*/
       if (!invert) {
              while (*d < 0) {
                     dec_month(&year, &month);
                     leapyear = timelib_is_leap(year);
                     days = leapyear ? days_in_month_leap[month] : days_in_month[month];

                     /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */

                     *d += days;
                     (*m)--;
              }
       } else {
              while (*d < 0) {
                     leapyear = timelib_is_leap(year);
                     days = leapyear ? days_in_month_leap[month] : days_in_month[month];

                     /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */

                     *d += days;
                     (*m)--;
                     inc_month(&year, &month);
              }
       }
       /*
       printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
       */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static timelib_sll do_time ( timelib_ull  hour,
timelib_ull  minute,
timelib_ull  second 
) [static]

Definition at line 352 of file tm2unixtime.c.

{
       timelib_sll res = 0;

       res += hour * 3600;
       res += minute * 60;
       res += second;
       return res;
}

Here is the caller graph for this function:

static timelib_sll do_years ( timelib_sll  year) [static]

Definition at line 306 of file tm2unixtime.c.

{
       timelib_sll i;
       timelib_sll res = 0;
       timelib_sll eras;

       eras = (year - 1970) / 40000;
       if (eras != 0) {
              year = year - (eras * 40000);
              res += (SECS_PER_ERA * eras * 100);
       }

       if (year >= 1970) {
              for (i = year - 1; i >= 1970; i--) {
                     if (timelib_is_leap(i)) {
                            res += (DAYS_PER_LYEAR * SECS_PER_DAY);
                     } else {
                            res += (DAYS_PER_YEAR * SECS_PER_DAY);
                     }
              }
       } else {
              for (i = 1969; i >= year; i--) {
                     if (timelib_is_leap(i)) {
                            res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
                     } else {
                            res -= (DAYS_PER_YEAR * SECS_PER_DAY);
                     }
              }
       }
       return res;
}

Here is the caller graph for this function:

static void inc_month ( timelib_sll y,
timelib_sll m 
) [static]

Definition at line 44 of file tm2unixtime.c.

{
       (*m)++;
       if (*m > 12) {
              *m -= 12;
              (*y)++;
       }
}

Here is the caller graph for this function:

Definition at line 182 of file tm2unixtime.c.

{
       if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->s, &time->i));
       if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 60, 60, &time->i, &time->h));
       if (time->s != TIMELIB_UNSET) do {} while (do_range_limit(0, 24, 24, &time->h, &time->d));
       do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));

       do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
       do {} while (do_range_limit(1, 13, 12, &time->m, &time->y));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 171 of file tm2unixtime.c.

{
       do {} while (do_range_limit(0, 60, 60, &rt->s, &rt->i));
       do {} while (do_range_limit(0, 60, 60, &rt->i, &rt->h));
       do {} while (do_range_limit(0, 24, 24, &rt->h, &rt->d));
       do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));

       do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
       do {} while (do_range_limit(0, 12, 12, &rt->m, &rt->y));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void timelib_update_ts ( timelib_time time,
timelib_tzinfo tzi 
)

Definition at line 430 of file tm2unixtime.c.

{
       timelib_sll res = 0;

       do_adjust_special_early(time);
       do_adjust_relative(time);
       do_adjust_special(time);
       res += do_years(time->y);
       res += do_months(time->m, time->y);
       res += do_days(time->d);
       res += do_time(time->h, time->i, time->s);
       time->sse = res;

       res += do_adjust_timezone(time, tzi);
       time->sse = res;

       time->sse_uptodate = 1;
       time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int days_in_month[13] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } [static]

Definition at line 29 of file tm2unixtime.c.

int days_in_month_leap[13] = { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } [static]

Definition at line 28 of file tm2unixtime.c.

int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 } [static]

Definition at line 25 of file tm2unixtime.c.

int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 } [static]

Definition at line 24 of file tm2unixtime.c.