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: 132 141 93.6 %
Date: 2014-11-10 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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      542371 : void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
      41             : {
      42             :         timelib_sll days, remainder, tmp_days;
      43      542371 :         timelib_sll cur_year = 1970;
      44             :         timelib_sll i;
      45             :         timelib_sll hours, minutes, seconds;
      46             :         int *months;
      47             : 
      48      542371 :         days = ts / SECS_PER_DAY;
      49      542371 :         remainder = ts - (days * SECS_PER_DAY);
      50      542371 :         if (ts < 0 && remainder == 0) {
      51          18 :                 days++;
      52          18 :                 remainder -= SECS_PER_DAY;
      53             :         }
      54      542371 :         TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
      55             : 
      56      542371 :         if (ts >= 0) {
      57      541838 :                 tmp_days = days + 1;
      58             : 
      59      541838 :                 if (tmp_days >= DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
      60          43 :                         cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
      61          43 :                         tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
      62             :                 }
      63             : 
      64    23179436 :                 while (tmp_days >= DAYS_PER_LYEAR) {
      65    22095760 :                         cur_year++;
      66    27678738 :                         if (timelib_is_leap(cur_year)) {
      67     5582978 :                                 tmp_days -= DAYS_PER_LYEAR;
      68             :                         } else {
      69    16512782 :                                 tmp_days -= DAYS_PER_YEAR;
      70             :                         }
      71             :                 }
      72             :         } else {
      73         533 :                 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   295160699 :                 while (tmp_days <= 0) {
      85   295159633 :                         if (tmp_days < -1460970) {
      86   294993033 :                                 cur_year -= 4000;
      87   294993033 :                                 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      88   294993033 :                                 tmp_days += 1460970;
      89             :                         } else {
      90      166600 :                                 cur_year--;
      91      166600 :                                 TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
      92      207131 :                                 if (timelib_is_leap(cur_year)) {
      93       40531 :                                         tmp_days += DAYS_PER_LYEAR;
      94             :                                 } else {
      95      126069 :                                         tmp_days += DAYS_PER_YEAR;
      96             :                                 }
      97             :                         }
      98             :                 }
      99         533 :                 remainder += SECS_PER_DAY;
     100             :         }
     101      542371 :         TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
     102             : 
     103      542371 :         months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
     104      542371 :         if (timelib_is_leap(cur_year) && cur_year < 1970) {
     105         118 :                 tmp_days--;
     106             :         }
     107      542371 :         i = 11;
     108     4240115 :         while (i > 0) {
     109     3621061 :                 TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
     110     3621061 :                 if (tmp_days > months[i]) {
     111      465688 :                         break;
     112             :                 }
     113     3155373 :                 i--;
     114             :         }
     115      542371 :         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      542371 :         hours = remainder / 3600;
     119      542371 :         minutes = (remainder - hours * 3600) / 60;
     120      542371 :         seconds = remainder % 60;
     121      542371 :         TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
     122             : 
     123      542371 :         tm->y = cur_year;
     124      542371 :         tm->m = i + 1;
     125      542371 :         tm->d = tmp_days - months[i];
     126      542371 :         tm->h = hours;
     127      542371 :         tm->i = minutes;
     128      542371 :         tm->s = seconds;
     129      542371 :         tm->z = 0;
     130      542371 :         tm->dst = 0;
     131      542371 :         tm->sse = ts;
     132      542371 :         tm->sse_uptodate = 1;
     133      542371 :         tm->tim_uptodate = 1;
     134      542371 :         tm->is_localtime = 0;
     135      542371 : }
     136             : 
     137      180114 : void timelib_update_from_sse(timelib_time *tm)
     138             : {
     139             :         timelib_sll sse;
     140      180114 :         int z = tm->z;
     141      180114 :         signed int dst = tm->dst;
     142             : 
     143      180114 :         sse = tm->sse;
     144             :         
     145      180114 :         switch (tm->zone_type) {
     146             :                 case TIMELIB_ZONETYPE_ABBR:
     147             :                 case TIMELIB_ZONETYPE_OFFSET: {
     148        1712 :                         timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60) + (tm->dst * 3600));
     149             : 
     150        1712 :                         goto cleanup;
     151             :                 }
     152             : 
     153             :                 case TIMELIB_ZONETYPE_ID: {
     154             :                         timelib_time_offset *gmt_offset;
     155             :                         
     156      178402 :                         gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
     157      178402 :                         timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
     158      178402 :                         timelib_time_offset_dtor(gmt_offset);
     159             :                         
     160      178402 :                         goto cleanup;
     161             :                 }
     162             : 
     163             :                 default:
     164           0 :                         timelib_unixtime2gmt(tm, tm->sse);
     165             :                         goto cleanup;
     166             :         }
     167             : cleanup:
     168      180114 :         tm->sse = sse;
     169      180114 :         tm->is_localtime = 1;
     170      180114 :         tm->have_zone = 1;
     171      180114 :         tm->z = z;
     172      180114 :         tm->dst = dst;
     173      180114 : }
     174             : 
     175       13772 : void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
     176             : {
     177             :         timelib_time_offset *gmt_offset;
     178       13772 :         timelib_tzinfo      *tz = tm->tz_info;
     179             : 
     180       13772 :         switch (tm->zone_type) {
     181             :                 case TIMELIB_ZONETYPE_ABBR:
     182             :                 case TIMELIB_ZONETYPE_OFFSET: {
     183           9 :                         int z = tm->z;
     184           9 :                         signed int dst = tm->dst;
     185             :                         
     186           9 :                         timelib_unixtime2gmt(tm, ts - (tm->z * 60) + (tm->dst * 3600));
     187             : 
     188           9 :                         tm->z = z;
     189           9 :                         tm->dst = dst;
     190           9 :                         break;
     191             :                 }
     192             : 
     193             :                 case TIMELIB_ZONETYPE_ID:
     194       13763 :                         gmt_offset = timelib_get_time_zone_info(ts, tz);
     195       13763 :                         timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
     196             : 
     197             :                         /* we need to reset the sse here as unixtime2gmt modifies it */
     198       13763 :                         tm->sse = ts; 
     199       13763 :                         tm->dst = gmt_offset->is_dst;
     200       13763 :                         tm->z = gmt_offset->offset;
     201       13763 :                         tm->tz_info = tz;
     202             : 
     203       13763 :                         timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
     204       13763 :                         timelib_time_offset_dtor(gmt_offset);
     205       13763 :                         break;
     206             : 
     207             :                 default:
     208           0 :                         tm->is_localtime = 0;
     209           0 :                         tm->have_zone = 0;
     210           0 :                         return;
     211             :         }
     212             : 
     213       13772 :         tm->is_localtime = 1;
     214       13772 :         tm->have_zone = 1;
     215             : }
     216             : 
     217           1 : void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
     218             : {
     219           1 :         if (t->tz_abbr) {
     220           1 :                 free(t->tz_abbr);
     221             :         }
     222           1 :         t->tz_abbr = NULL;
     223             : 
     224           1 :         t->z = utc_offset;
     225           1 :         t->have_zone = 1;
     226           1 :         t->zone_type = TIMELIB_ZONETYPE_OFFSET;
     227           1 :         t->dst = 0;
     228           1 :         t->tz_info = NULL;
     229           1 : }
     230             : 
     231           3 : void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
     232             : {
     233           3 :         if (t->tz_abbr) {
     234           3 :                 free(t->tz_abbr);
     235             :         }
     236           3 :         t->tz_abbr = strdup(abbr_info.abbr);
     237             : 
     238           3 :         t->z = abbr_info.utc_offset;
     239           3 :         t->have_zone = 1;
     240           3 :         t->zone_type = TIMELIB_ZONETYPE_ABBR;
     241           3 :         t->dst = abbr_info.dst;
     242           3 :         t->tz_info = NULL;
     243           3 : }
     244             : 
     245      530938 : void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
     246             : {
     247             :         timelib_time_offset *gmt_offset;
     248             : 
     249      530938 :         gmt_offset = timelib_get_time_zone_info(t->sse, tz);
     250      530938 :         t->z = gmt_offset->offset;
     251             : /*
     252             :         if (t->dst != gmt_offset->is_dst) {
     253             :                 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
     254             :                 exit(1);
     255             :         }
     256             : */
     257      530938 :         t->dst = gmt_offset->is_dst;
     258      530938 :         t->tz_info = tz;
     259      530938 :         if (t->tz_abbr) {
     260      530938 :                 free(t->tz_abbr);
     261             :         }
     262      530938 :         t->tz_abbr = strdup(gmt_offset->abbr);
     263      530938 :         timelib_time_offset_dtor(gmt_offset);
     264             : 
     265      530938 :         t->have_zone = 1;
     266      530938 :         t->zone_type = TIMELIB_ZONETYPE_ID;
     267      530938 : }
     268             : 
     269             : /* Converts the time stored in the struct to localtime if localtime = true,
     270             :  * otherwise it converts it to gmttime. This is only done when necessary
     271             :  * ofcourse. */
     272      346958 : int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
     273             : {
     274      346958 :         if (localtime) {
     275             :                 /* Converting from GMT time to local time */
     276           0 :                 TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
     277             : 
     278             :                 /* Check if TZ is set */
     279           0 :                 if (!t->tz_info) {
     280           0 :                         TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
     281           0 :                         return -1;
     282             :                 }
     283             : 
     284           0 :                 timelib_unixtime2local(t, t->sse);
     285             :         } else {
     286             :                 /* Converting from local time to GMT time */
     287      346958 :                 TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
     288             : 
     289      346958 :                 timelib_unixtime2gmt(t, t->sse);
     290             :         }
     291      346958 :         return 0;
     292             : }

Generated by: LCOV version 1.10

Generated at Mon, 10 Nov 2014 22:46:37 +0000 (11 days ago)

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