PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - date/lib - unixtime2tm.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 109
Code covered: 82.6 % Executed lines: 90
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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 272374 2008-12-31 11:17:49Z sebastian $ */
      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            8326 : {
      42                 :         timelib_sll days, remainder, tmp_days;
      43            8326 :         timelib_sll cur_year = 1970;
      44                 :         timelib_sll i;
      45                 :         timelib_sll hours, minutes, seconds;
      46                 :         int *months;
      47                 : 
      48            8326 :         days = ts / SECS_PER_DAY;
      49            8326 :         remainder = ts - (days * SECS_PER_DAY);
      50            8326 :         if (ts < 0 && remainder == 0) {
      51               7 :                 days++;
      52               7 :                 remainder -= SECS_PER_DAY;
      53                 :         }
      54                 :         DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
      55                 : 
      56            8326 :         if (ts >= 0) {
      57            7948 :                 tmp_days = days + 1;
      58          296181 :                 while (tmp_days >= DAYS_PER_LYEAR) {
      59          280285 :                         cur_year++;
      60          351981 :                         if (timelib_is_leap(cur_year)) {
      61           71696 :                                 tmp_days -= DAYS_PER_LYEAR;
      62                 :                         } else {
      63          208589 :                                 tmp_days -= DAYS_PER_YEAR;
      64                 :                         }
      65                 :                 }
      66                 :         } else {
      67             378 :                 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                 :                 /*
      73                 :                 if (ts <= TIMELIB_LL_CONST(-6857352000)) {
      74                 :                         tmp_days -= 11;
      75                 :                 }
      76                 :                 */
      77                 : 
      78           16369 :                 while (tmp_days <= 0) {
      79           15613 :                         if (tmp_days < -1460970) {
      80               0 :                                 cur_year -= 4000;
      81                 :                                 DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      82               0 :                                 tmp_days += 1460970;
      83                 :                         } else {
      84           15613 :                                 cur_year--;
      85                 :                                 DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      86           19519 :                                 if (timelib_is_leap(cur_year)) {
      87            3906 :                                         tmp_days += DAYS_PER_LYEAR;
      88                 :                                 } else {
      89           11707 :                                         tmp_days += DAYS_PER_YEAR;
      90                 :                                 }
      91                 :                         }
      92                 :                 }
      93             378 :                 remainder += SECS_PER_DAY;
      94                 :         }
      95                 :         DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      96                 : 
      97            8326 :         months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
      98            8326 :         if (timelib_is_leap(cur_year) && cur_year < 1970) {
      99              83 :                 tmp_days--;
     100                 :         }
     101            8326 :         i = 11;
     102           48628 :         while (i > 0) {
     103                 :                 DEBUG(printf("month=%lld (%d)\n", i, months[i]););
     104           38702 :                 if (tmp_days > months[i]) {
     105            6726 :                         break;
     106                 :                 }
     107           31976 :                 i--;
     108                 :         }
     109                 :         DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
     110                 : 
     111                 :         /* That was the date, now we do the tiiiime */
     112            8326 :         hours = remainder / 3600;
     113            8326 :         minutes = (remainder - hours * 3600) / 60;
     114            8326 :         seconds = remainder % 60;
     115                 :         DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
     116                 : 
     117            8326 :         tm->y = cur_year;
     118            8326 :         tm->m = i + 1;
     119            8326 :         tm->d = tmp_days - months[i];
     120            8326 :         tm->h = hours;
     121            8326 :         tm->i = minutes;
     122            8326 :         tm->s = seconds;
     123            8326 :         tm->z = 0;
     124            8326 :         tm->dst = 0;
     125            8326 :         tm->sse = ts;
     126            8326 :         tm->sse_uptodate = 1;
     127            8326 :         tm->tim_uptodate = 1;
     128            8326 :         tm->is_localtime = 0;
     129            8326 : }
     130                 : 
     131                 : void timelib_update_from_sse(timelib_time *tm)
     132              67 : {
     133                 :         timelib_sll sse;
     134                 : 
     135              67 :         sse = tm->sse;
     136                 :         
     137              67 :         switch (tm->zone_type) {
     138                 :                 case TIMELIB_ZONETYPE_ABBR:
     139                 :                 case TIMELIB_ZONETYPE_OFFSET: {
     140               7 :                         int z = tm->z;
     141               7 :                         signed int dst = tm->dst;
     142                 :                         
     143               7 :                         timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60));
     144                 : 
     145               7 :                         tm->z = z;
     146               7 :                         tm->dst = dst;
     147               7 :                         goto cleanup;
     148                 :                 }
     149                 : 
     150                 :                 case TIMELIB_ZONETYPE_ID: {
     151                 :                         timelib_time_offset *gmt_offset;
     152                 :                         
     153              60 :                         gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
     154              60 :                         timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
     155              60 :                         timelib_time_offset_dtor(gmt_offset);
     156                 :                         
     157              60 :                         goto cleanup;
     158                 :                 }
     159                 : 
     160                 :                 default:
     161               0 :                         timelib_unixtime2gmt(tm, tm->sse);
     162                 :                         goto cleanup;
     163                 :         }
     164              67 : cleanup:
     165              67 :         tm->sse = sse;
     166              67 :         tm->is_localtime = 1;
     167              67 :         tm->have_zone = 1;
     168              67 : }
     169                 : 
     170                 : void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
     171            7101 : {
     172                 :         timelib_time_offset *gmt_offset;
     173            7101 :         timelib_tzinfo      *tz = tm->tz_info;
     174                 : 
     175            7101 :         switch (tm->zone_type) {
     176                 :                 case TIMELIB_ZONETYPE_ABBR:
     177                 :                 case TIMELIB_ZONETYPE_OFFSET: {
     178               0 :                         int z = tm->z;
     179               0 :                         signed int dst = tm->dst;
     180                 :                         
     181               0 :                         timelib_unixtime2gmt(tm, ts - (tm->z * 60));
     182                 : 
     183               0 :                         tm->z = z;
     184               0 :                         tm->dst = dst;
     185               0 :                         break;
     186                 :                 }
     187                 : 
     188                 :                 case TIMELIB_ZONETYPE_ID:
     189            7101 :                         gmt_offset = timelib_get_time_zone_info(ts, tz);
     190            7101 :                         timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
     191                 : 
     192                 :                         /* we need to reset the sse here as unixtime2gmt modifies it */
     193            7101 :                         tm->sse = ts; 
     194            7101 :                         tm->dst = gmt_offset->is_dst;
     195            7101 :                         tm->z = gmt_offset->offset;
     196            7101 :                         tm->tz_info = tz;
     197                 : 
     198            7101 :                         timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
     199            7101 :                         timelib_time_offset_dtor(gmt_offset);
     200            7101 :                         break;
     201                 : 
     202                 :                 default:
     203               0 :                         tm->is_localtime = 0;
     204               0 :                         tm->have_zone = 0;
     205               0 :                         return;
     206                 :         }
     207                 : 
     208            7101 :         tm->is_localtime = 1;
     209            7101 :         tm->have_zone = 1;
     210                 : }
     211                 : 
     212                 : void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
     213            4504 : {
     214                 :         timelib_time_offset *gmt_offset;
     215                 : 
     216            4504 :         gmt_offset = timelib_get_time_zone_info(t->sse, tz);
     217            4504 :         t->z = gmt_offset->offset;
     218                 : /*
     219                 :         if (t->dst != gmt_offset->is_dst) {
     220                 :                 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
     221                 :                 exit(1);
     222                 :         }
     223                 : */
     224            4504 :         t->dst = gmt_offset->is_dst;
     225            4504 :         t->tz_info = tz;
     226            4504 :         if (t->tz_abbr) {
     227            4315 :                 free(t->tz_abbr);
     228                 :         }
     229            4504 :         t->tz_abbr = strdup(gmt_offset->abbr);
     230            4504 :         timelib_time_offset_dtor(gmt_offset);
     231                 : 
     232            4504 :         t->have_zone = 1;
     233            4504 :         t->zone_type = TIMELIB_ZONETYPE_ID;
     234            4504 : }
     235                 : 
     236                 : /* Converts the time stored in the struct to localtime if localtime = true,
     237                 :  * otherwise it converts it to gmttime. This is only done when necessary
     238                 :  * ofcourse. */
     239                 : int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
     240               0 : {
     241               0 :         if (localtime) {
     242                 :                 /* Converting from GMT time to local time */
     243                 :                 DEBUG(printf("Converting from GMT time to local time\n"););
     244                 : 
     245                 :                 /* Check if TZ is set */
     246               0 :                 if (!t->tz_info) {
     247                 :                         DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
     248               0 :                         return -1;
     249                 :                 }
     250                 : 
     251               0 :                 timelib_unixtime2local(t, t->sse);
     252                 :         } else {
     253                 :                 /* Converting from local time to GMT time */
     254                 :                 DEBUG(printf("Converting from local time to GMT time\n"););
     255                 : 
     256               0 :                 timelib_unixtime2gmt(t, t->sse);
     257                 :         }
     258               0 :         return 0;
     259                 : }

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:05 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.