LTP GCOV extension - code coverage report
Current view: directory - ext/date/lib - unixtime2tm.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 108
Code covered: 45.4 % Executed lines: 49
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2007 The PHP Group                                |
       6                 :    +----------------------------------------------------------------------+
       7                 :    | This source file is subject to version 3.01 of the PHP license,      |
       8                 :    | that is bundled with this package in the file LICENSE, and is        |
       9                 :    | available through the world-wide-web at the following url:           |
      10                 :    | http://www.php.net/license/3_01.txt                                  |
      11                 :    | If you did not receive a copy of the PHP license and are unable to   |
      12                 :    | obtain it through the world-wide-web, please send a note to          |
      13                 :    | license@php.net so we can mail you a copy immediately.               |
      14                 :    +----------------------------------------------------------------------+
      15                 :    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
      16                 :    +----------------------------------------------------------------------+
      17                 :  */
      18                 : 
      19                 : /* $Id: unixtime2tm.c,v 1.12.2.4.2.2 2007/01/01 09:35:59 sebastian Exp $ */
      20                 : 
      21                 : #include "timelib.h"
      22                 : 
      23                 : #include <stdio.h>
      24                 : 
      25                 : #ifdef HAVE_STDLIB_H
      26                 : #include <stdlib.h>
      27                 : #endif
      28                 : 
      29                 : #ifdef HAVE_STRING_H
      30                 : #include <string.h>
      31                 : #else
      32                 : #include <strings.h>
      33                 : #endif
      34                 : 
      35                 : static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
      36                 : static int month_tab[12] =      { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
      37                 : 
      38                 : 
      39                 : /* Converts a Unix timestamp value into broken down time, in GMT */
      40                 : void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
      41               2 : {
      42                 :         timelib_sll days, remainder, tmp_days;
      43               2 :         timelib_sll cur_year = 1970;
      44                 :         timelib_sll i;
      45                 :         timelib_sll hours, minutes, seconds;
      46                 :         int *months;
      47                 : 
      48               2 :         days = ts / SECS_PER_DAY;
      49               2 :         remainder = ts - (days * SECS_PER_DAY);
      50               2 :         if (ts < 0 && remainder == 0) {
      51               0 :                 days++;
      52               0 :                 remainder -= SECS_PER_DAY;
      53                 :         }
      54                 :         DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
      55                 : 
      56               2 :         if (ts >= 0) {
      57               2 :                 tmp_days = days + 1;
      58              78 :                 while (tmp_days >= DAYS_PER_LYEAR) {
      59              74 :                         cur_year++;
      60              92 :                         if (timelib_is_leap(cur_year)) {
      61              18 :                                 tmp_days -= DAYS_PER_LYEAR;
      62                 :                         } else {
      63              56 :                                 tmp_days -= DAYS_PER_YEAR;
      64                 :                         }
      65                 :                 }
      66                 :         } else {
      67               0 :                 tmp_days = days;
      68                 : 
      69                 :                 /* Guess why this might be for, it has to do with a pope ;-). It's also
      70                 :                  * only valid for Great Brittain and it's colonies. It needs fixing for
      71                 :                  * other locales. *sigh*, why is this crap so complex! */
      72               0 :                 if (ts <= TIMELIB_LL_CONST(-6857352000)) {
      73               0 :                         tmp_days -= 11;
      74                 :                 }
      75                 : 
      76               0 :                 while (tmp_days <= 0) {
      77               0 :                         cur_year--;
      78                 :                         DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      79               0 :                         if (timelib_is_leap(cur_year)) {
      80               0 :                                 tmp_days += DAYS_PER_LYEAR;
      81                 :                         } else {
      82               0 :                                 tmp_days += DAYS_PER_YEAR;
      83                 :                         }
      84                 :                 }
      85               0 :                 remainder += SECS_PER_DAY;
      86                 :         }
      87                 :         DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      88                 : 
      89               2 :         months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
      90               2 :         if (timelib_is_leap(cur_year) && cur_year < 1970) {
      91               0 :                 tmp_days--;
      92                 :         }
      93               2 :         i = 11;
      94              20 :         while (i > 0) {
      95                 :                 DEBUG(printf("month=%lld (%d)\n", i, months[i]););
      96              18 :                 if (tmp_days > months[i]) {
      97               2 :                         break;
      98                 :                 }
      99              16 :                 i--;
     100                 :         }
     101                 :         DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
     102                 : 
     103                 :         /* That was the date, now we do the tiiiime */
     104               2 :         hours = remainder / 3600;
     105               2 :         minutes = (remainder - hours * 3600) / 60;
     106               2 :         seconds = remainder % 60;
     107                 :         DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
     108                 : 
     109               2 :         tm->y = cur_year;
     110               2 :         tm->m = i + 1;
     111               2 :         tm->d = tmp_days - months[i];
     112               2 :         tm->h = hours;
     113               2 :         tm->i = minutes;
     114               2 :         tm->s = seconds;
     115               2 :         tm->z = 0;
     116               2 :         tm->dst = 0;
     117               2 :         tm->sse = ts;
     118               2 :         tm->sse_uptodate = 1;
     119               2 :         tm->tim_uptodate = 1;
     120               2 :         tm->is_localtime = 0;
     121               2 : }
     122                 : 
     123                 : void timelib_update_from_sse(timelib_time *tm)
     124               0 : {
     125                 :         timelib_sll sse;
     126                 : 
     127               0 :         sse = tm->sse;
     128                 :         
     129               0 :         switch (tm->zone_type) {
     130                 :                 case TIMELIB_ZONETYPE_ABBR:
     131                 :                 case TIMELIB_ZONETYPE_OFFSET: {
     132               0 :                         int z = tm->z;
     133               0 :                         signed int dst = tm->dst;
     134                 :                         
     135               0 :                         timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60));
     136                 : 
     137               0 :                         tm->z = z;
     138               0 :                         tm->dst = dst;
     139               0 :                         goto cleanup;
     140                 :                 }
     141                 : 
     142                 :                 case TIMELIB_ZONETYPE_ID: {
     143                 :                         timelib_time_offset *gmt_offset;
     144                 :                         
     145               0 :                         gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
     146               0 :                         timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
     147               0 :                         timelib_time_offset_dtor(gmt_offset);
     148                 :                         
     149               0 :                         goto cleanup;
     150                 :                 }
     151                 : 
     152                 :                 default:
     153               0 :                         timelib_unixtime2gmt(tm, tm->sse);
     154                 :                         goto cleanup;
     155                 :         }
     156               0 : cleanup:
     157               0 :         tm->sse = sse;
     158               0 :         tm->is_localtime = 1;
     159               0 :         tm->have_zone = 1;
     160               0 : }
     161                 : 
     162                 : void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
     163               2 : {
     164                 :         timelib_time_offset *gmt_offset;
     165               2 :         timelib_tzinfo      *tz = tm->tz_info;
     166                 : 
     167               2 :         switch (tm->zone_type) {
     168                 :                 case TIMELIB_ZONETYPE_ABBR:
     169                 :                 case TIMELIB_ZONETYPE_OFFSET: {
     170               0 :                         int z = tm->z;
     171               0 :                         signed int dst = tm->dst;
     172                 :                         
     173               0 :                         timelib_unixtime2gmt(tm, ts - (tm->z * 60));
     174                 : 
     175               0 :                         tm->z = z;
     176               0 :                         tm->dst = dst;
     177               0 :                         break;
     178                 :                 }
     179                 : 
     180                 :                 case TIMELIB_ZONETYPE_ID:
     181               2 :                         gmt_offset = timelib_get_time_zone_info(ts, tz);
     182               2 :                         timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
     183                 : 
     184                 :                         /* we need to reset the sse here as unixtime2gmt modifies it */
     185               2 :                         tm->sse = ts; 
     186               2 :                         tm->dst = gmt_offset->is_dst;
     187               2 :                         tm->z = gmt_offset->offset;
     188               2 :                         tm->tz_info = tz;
     189                 : 
     190               2 :                         timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
     191               2 :                         timelib_time_offset_dtor(gmt_offset);
     192               2 :                         break;
     193                 : 
     194                 :                 default:
     195               0 :                         tm->is_localtime = 0;
     196               0 :                         tm->have_zone = 0;
     197               0 :                         return;
     198                 :         }
     199                 : 
     200               2 :         tm->is_localtime = 1;
     201               2 :         tm->have_zone = 1;
     202                 : }
     203                 : 
     204                 : void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
     205               0 : {
     206                 :         timelib_time_offset *gmt_offset;
     207                 : 
     208               0 :         gmt_offset = timelib_get_time_zone_info(t->sse, tz);
     209               0 :         t->z = gmt_offset->offset;
     210                 : /*
     211                 :         if (t->dst != gmt_offset->is_dst) {
     212                 :                 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
     213                 :                 exit(1);
     214                 :         }
     215                 : */
     216               0 :         t->dst = gmt_offset->is_dst;
     217               0 :         t->tz_info = tz;
     218               0 :         if (t->tz_abbr) {
     219               0 :                 free(t->tz_abbr);
     220                 :         }
     221               0 :         t->tz_abbr = strdup(gmt_offset->abbr);
     222               0 :         timelib_time_offset_dtor(gmt_offset);
     223                 : 
     224               0 :         t->have_zone = 1;
     225               0 :         t->zone_type = TIMELIB_ZONETYPE_ID;
     226               0 : }
     227                 : 
     228                 : /* Converts the time stored in the struct to localtime if localtime = true,
     229                 :  * otherwise it converts it to gmttime. This is only done when necessary
     230                 :  * ofcourse. */
     231                 : int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
     232               0 : {
     233               0 :         if (localtime) {
     234                 :                 /* Converting from GMT time to local time */
     235                 :                 DEBUG(printf("Converting from GMT time to local time\n"););
     236                 : 
     237                 :                 /* Check if TZ is set */
     238               0 :                 if (!t->tz_info) {
     239                 :                         DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
     240               0 :                         return -1;
     241                 :                 }
     242                 : 
     243               0 :                 timelib_unixtime2local(t, t->sse);
     244                 :         } else {
     245                 :                 /* Converting from local time to GMT time */
     246                 :                 DEBUG(printf("Converting from local time to GMT time\n"););
     247                 : 
     248               0 :                 timelib_unixtime2gmt(t, t->sse);
     249                 :         }
     250               0 :         return 0;
     251                 : }

Generated by: LTP GCOV extension version 1.5