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

LCOV - code coverage report
Current view: top level - ext/date/lib - unixtime2tm.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 112 121 92.6 %
Date: 2014-07-13 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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$ */
      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      537015 : void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
      41             : {
      42             :         timelib_sll days, remainder, tmp_days;
      43      537015 :         timelib_sll cur_year = 1970;
      44             :         timelib_sll i;
      45             :         timelib_sll hours, minutes, seconds;
      46             :         int *months;
      47             : 
      48      537015 :         days = ts / SECS_PER_DAY;
      49      537015 :         remainder = ts - (days * SECS_PER_DAY);
      50      537015 :         if (ts < 0 && remainder == 0) {
      51           7 :                 days++;
      52           7 :                 remainder -= SECS_PER_DAY;
      53             :         }
      54      537015 :         TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
      55             : 
      56      537015 :         if (ts >= 0) {
      57      536511 :                 tmp_days = days + 1;
      58             : 
      59      536511 :                 if (tmp_days >= DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
      60          22 :                         cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
      61          22 :                         tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
      62             :                 }
      63             : 
      64    22949366 :                 while (tmp_days >= DAYS_PER_LYEAR) {
      65    21876344 :                         cur_year++;
      66    27404167 :                         if (timelib_is_leap(cur_year)) {
      67     5527823 :                                 tmp_days -= DAYS_PER_LYEAR;
      68             :                         } else {
      69    16348521 :                                 tmp_days -= DAYS_PER_YEAR;
      70             :                         }
      71             :                 }
      72             :         } else {
      73         504 :                 tmp_days = days;
      74             : 
      75             :                 /* Guess why this might be for, it has to do with a pope ;-). It's also
      76             :                  * only valid for Great Brittain and it's colonies. It needs fixing for
      77             :                  * other locales. *sigh*, why is this crap so complex! */
      78             :                 /*
      79             :                 if (ts <= TIMELIB_LL_CONST(-6857352000)) {
      80             :                         tmp_days -= 11;
      81             :                 }
      82             :                 */
      83             : 
      84   292378443 :                 while (tmp_days <= 0) {
      85   292377435 :                         if (tmp_days < -1460970) {
      86   292277522 :                                 cur_year -= 4000;
      87   292277522 :                                 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      88   292277522 :                                 tmp_days += 1460970;
      89             :                         } else {
      90       99913 :                                 cur_year--;
      91       99913 :                                 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      92      124275 :                                 if (timelib_is_leap(cur_year)) {
      93       24362 :                                         tmp_days += DAYS_PER_LYEAR;
      94             :                                 } else {
      95       75551 :                                         tmp_days += DAYS_PER_YEAR;
      96             :                                 }
      97             :                         }
      98             :                 }
      99         504 :                 remainder += SECS_PER_DAY;
     100             :         }
     101      537015 :         TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
     102             : 
     103      537015 :         months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
     104      537015 :         if (timelib_is_leap(cur_year) && cur_year < 1970) {
     105         110 :                 tmp_days--;
     106             :         }
     107      537015 :         i = 11;
     108     4243277 :         while (i > 0) {
     109     3629937 :                 TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
     110     3629937 :                 if (tmp_days > months[i]) {
     111      460690 :                         break;
     112             :                 }
     113     3169247 :                 i--;
     114             :         }
     115      537015 :         TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
     116             : 
     117             :         /* That was the date, now we do the tiiiime */
     118      537015 :         hours = remainder / 3600;
     119      537015 :         minutes = (remainder - hours * 3600) / 60;
     120      537015 :         seconds = remainder % 60;
     121      537015 :         TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
     122             : 
     123      537015 :         tm->y = cur_year;
     124      537015 :         tm->m = i + 1;
     125      537015 :         tm->d = tmp_days - months[i];
     126      537015 :         tm->h = hours;
     127      537015 :         tm->i = minutes;
     128      537015 :         tm->s = seconds;
     129      537015 :         tm->z = 0;
     130      537015 :         tm->dst = 0;
     131      537015 :         tm->sse = ts;
     132      537015 :         tm->sse_uptodate = 1;
     133      537015 :         tm->tim_uptodate = 1;
     134      537015 :         tm->is_localtime = 0;
     135      537015 : }
     136             : 
     137      175102 : void timelib_update_from_sse(timelib_time *tm)
     138             : {
     139             :         timelib_sll sse;
     140             : 
     141      175102 :         sse = tm->sse;
     142             :         
     143      175102 :         switch (tm->zone_type) {
     144             :                 case TIMELIB_ZONETYPE_ABBR:
     145             :                 case TIMELIB_ZONETYPE_OFFSET: {
     146         322 :                         int z = tm->z;
     147         322 :                         signed int dst = tm->dst;
     148             :                         
     149         322 :                         timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60) + (tm->dst * 3600));
     150             : 
     151         322 :                         tm->z = z;
     152         322 :                         tm->dst = dst;
     153         322 :                         goto cleanup;
     154             :                 }
     155             : 
     156             :                 case TIMELIB_ZONETYPE_ID: {
     157             :                         timelib_time_offset *gmt_offset;
     158             :                         
     159      174780 :                         gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
     160      174780 :                         timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
     161      174780 :                         timelib_time_offset_dtor(gmt_offset);
     162             :                         
     163      174780 :                         goto cleanup;
     164             :                 }
     165             : 
     166             :                 default:
     167           0 :                         timelib_unixtime2gmt(tm, tm->sse);
     168             :                         goto cleanup;
     169             :         }
     170             : cleanup:
     171      175102 :         tm->sse = sse;
     172      175102 :         tm->is_localtime = 1;
     173      175102 :         tm->have_zone = 1;
     174      175102 : }
     175             : 
     176       13439 : void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
     177             : {
     178             :         timelib_time_offset *gmt_offset;
     179       13439 :         timelib_tzinfo      *tz = tm->tz_info;
     180             : 
     181       13439 :         switch (tm->zone_type) {
     182             :                 case TIMELIB_ZONETYPE_ABBR:
     183             :                 case TIMELIB_ZONETYPE_OFFSET: {
     184           2 :                         int z = tm->z;
     185           2 :                         signed int dst = tm->dst;
     186             :                         
     187           2 :                         timelib_unixtime2gmt(tm, ts - (tm->z * 60) + (tm->dst * 3600));
     188             : 
     189           2 :                         tm->z = z;
     190           2 :                         tm->dst = dst;
     191           2 :                         break;
     192             :                 }
     193             : 
     194             :                 case TIMELIB_ZONETYPE_ID:
     195       13437 :                         gmt_offset = timelib_get_time_zone_info(ts, tz);
     196       13437 :                         timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
     197             : 
     198             :                         /* we need to reset the sse here as unixtime2gmt modifies it */
     199       13437 :                         tm->sse = ts; 
     200       13437 :                         tm->dst = gmt_offset->is_dst;
     201       13437 :                         tm->z = gmt_offset->offset;
     202       13437 :                         tm->tz_info = tz;
     203             : 
     204       13437 :                         timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
     205       13437 :                         timelib_time_offset_dtor(gmt_offset);
     206       13437 :                         break;
     207             : 
     208             :                 default:
     209           0 :                         tm->is_localtime = 0;
     210           0 :                         tm->have_zone = 0;
     211           0 :                         return;
     212             :         }
     213             : 
     214       13439 :         tm->is_localtime = 1;
     215       13439 :         tm->have_zone = 1;
     216             : }
     217             : 
     218      530440 : void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
     219             : {
     220             :         timelib_time_offset *gmt_offset;
     221             : 
     222      530440 :         gmt_offset = timelib_get_time_zone_info(t->sse, tz);
     223      530440 :         t->z = gmt_offset->offset;
     224             : /*
     225             :         if (t->dst != gmt_offset->is_dst) {
     226             :                 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
     227             :                 exit(1);
     228             :         }
     229             : */
     230      530440 :         t->dst = gmt_offset->is_dst;
     231      530440 :         t->tz_info = tz;
     232      530440 :         if (t->tz_abbr) {
     233      530440 :                 free(t->tz_abbr);
     234             :         }
     235      530440 :         t->tz_abbr = strdup(gmt_offset->abbr);
     236      530440 :         timelib_time_offset_dtor(gmt_offset);
     237             : 
     238      530440 :         t->have_zone = 1;
     239      530440 :         t->zone_type = TIMELIB_ZONETYPE_ID;
     240      530440 : }
     241             : 
     242             : /* Converts the time stored in the struct to localtime if localtime = true,
     243             :  * otherwise it converts it to gmttime. This is only done when necessary
     244             :  * ofcourse. */
     245      346950 : int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
     246             : {
     247      346950 :         if (localtime) {
     248             :                 /* Converting from GMT time to local time */
     249           0 :                 TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
     250             : 
     251             :                 /* Check if TZ is set */
     252           0 :                 if (!t->tz_info) {
     253           0 :                         TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
     254           0 :                         return -1;
     255             :                 }
     256             : 
     257           0 :                 timelib_unixtime2local(t, t->sse);
     258             :         } else {
     259             :                 /* Converting from local time to GMT time */
     260      346950 :                 TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
     261             : 
     262      346950 :                 timelib_unixtime2gmt(t, t->sse);
     263             :         }
     264      346950 :         return 0;
     265             : }

Generated by: LCOV version 1.10

Generated at Sun, 13 Jul 2014 23:57:48 +0000 (8 days ago)

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