Back to index

php5  5.3.10
unixtime2tm.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2010 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: unixtime2tm.c 319767 2011-11-24 17:13:47Z derick $ */
00020 
00021 #include "timelib.h"
00022 
00023 #include <stdio.h>
00024 
00025 #ifdef HAVE_STDLIB_H
00026 #include <stdlib.h>
00027 #endif
00028 
00029 #ifdef HAVE_STRING_H
00030 #include <string.h>
00031 #else
00032 #include <strings.h>
00033 #endif
00034 
00035 static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
00036 static int month_tab[12] =      { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
00037 
00038 
00039 /* Converts a Unix timestamp value into broken down time, in GMT */
00040 void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
00041 {
00042        timelib_sll days, remainder, tmp_days;
00043        timelib_sll cur_year = 1970;
00044        timelib_sll i;
00045        timelib_sll hours, minutes, seconds;
00046        int *months;
00047 
00048        days = ts / SECS_PER_DAY;
00049        remainder = ts - (days * SECS_PER_DAY);
00050        if (ts < 0 && remainder == 0) {
00051               days++;
00052               remainder -= SECS_PER_DAY;
00053        }
00054        TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
00055 
00056        if (ts >= 0) {
00057               tmp_days = days + 1;
00058 
00059               if (tmp_days >= DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
00060                      cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
00061                      tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
00062               }
00063 
00064               while (tmp_days >= DAYS_PER_LYEAR) {
00065                      cur_year++;
00066                      if (timelib_is_leap(cur_year)) {
00067                             tmp_days -= DAYS_PER_LYEAR;
00068                      } else {
00069                             tmp_days -= DAYS_PER_YEAR;
00070                      }
00071               }
00072        } else {
00073               tmp_days = days;
00074 
00075               /* Guess why this might be for, it has to do with a pope ;-). It's also
00076                * only valid for Great Brittain and it's colonies. It needs fixing for
00077                * other locales. *sigh*, why is this crap so complex! */
00078               /*
00079               if (ts <= TIMELIB_LL_CONST(-6857352000)) {
00080                      tmp_days -= 11;
00081               }
00082               */
00083 
00084               while (tmp_days <= 0) {
00085                      if (tmp_days < -1460970) {
00086                             cur_year -= 4000;
00087                             TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
00088                             tmp_days += 1460970;
00089                      } else {
00090                             cur_year--;
00091                             TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
00092                             if (timelib_is_leap(cur_year)) {
00093                                    tmp_days += DAYS_PER_LYEAR;
00094                             } else {
00095                                    tmp_days += DAYS_PER_YEAR;
00096                             }
00097                      }
00098               }
00099               remainder += SECS_PER_DAY;
00100        }
00101        TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
00102 
00103        months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
00104        if (timelib_is_leap(cur_year) && cur_year < 1970) {
00105               tmp_days--;
00106        }
00107        i = 11;
00108        while (i > 0) {
00109               TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
00110               if (tmp_days > months[i]) {
00111                      break;
00112               }
00113               i--;
00114        }
00115        TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
00116 
00117        /* That was the date, now we do the tiiiime */
00118        hours = remainder / 3600;
00119        minutes = (remainder - hours * 3600) / 60;
00120        seconds = remainder % 60;
00121        TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
00122 
00123        tm->y = cur_year;
00124        tm->m = i + 1;
00125        tm->d = tmp_days - months[i];
00126        tm->h = hours;
00127        tm->i = minutes;
00128        tm->s = seconds;
00129        tm->z = 0;
00130        tm->dst = 0;
00131        tm->sse = ts;
00132        tm->sse_uptodate = 1;
00133        tm->tim_uptodate = 1;
00134        tm->is_localtime = 0;
00135 }
00136 
00137 void timelib_update_from_sse(timelib_time *tm)
00138 {
00139        timelib_sll sse;
00140 
00141        sse = tm->sse;
00142        
00143        switch (tm->zone_type) {
00144               case TIMELIB_ZONETYPE_ABBR:
00145               case TIMELIB_ZONETYPE_OFFSET: {
00146                      int z = tm->z;
00147                      signed int dst = tm->dst;
00148                      
00149                      timelib_unixtime2gmt(tm, tm->sse - (tm->z * 60) + (tm->dst * 3600));
00150 
00151                      tm->z = z;
00152                      tm->dst = dst;
00153                      goto cleanup;
00154               }
00155 
00156               case TIMELIB_ZONETYPE_ID: {
00157                      timelib_time_offset *gmt_offset;
00158                      
00159                      gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
00160                      timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
00161                      timelib_time_offset_dtor(gmt_offset);
00162                      
00163                      goto cleanup;
00164               }
00165 
00166               default:
00167                      timelib_unixtime2gmt(tm, tm->sse);
00168                      goto cleanup;
00169        }
00170 cleanup:
00171        tm->sse = sse;
00172        tm->is_localtime = 1;
00173        tm->have_zone = 1;
00174 }
00175 
00176 void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
00177 {
00178        timelib_time_offset *gmt_offset;
00179        timelib_tzinfo      *tz = tm->tz_info;
00180 
00181        switch (tm->zone_type) {
00182               case TIMELIB_ZONETYPE_ABBR:
00183               case TIMELIB_ZONETYPE_OFFSET: {
00184                      int z = tm->z;
00185                      signed int dst = tm->dst;
00186                      
00187                      timelib_unixtime2gmt(tm, ts - (tm->z * 60) + (tm->dst * 3600));
00188 
00189                      tm->z = z;
00190                      tm->dst = dst;
00191                      break;
00192               }
00193 
00194               case TIMELIB_ZONETYPE_ID:
00195                      gmt_offset = timelib_get_time_zone_info(ts, tz);
00196                      timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
00197 
00198                      /* we need to reset the sse here as unixtime2gmt modifies it */
00199                      tm->sse = ts; 
00200                      tm->dst = gmt_offset->is_dst;
00201                      tm->z = gmt_offset->offset;
00202                      tm->tz_info = tz;
00203 
00204                      timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
00205                      timelib_time_offset_dtor(gmt_offset);
00206                      break;
00207 
00208               default:
00209                      tm->is_localtime = 0;
00210                      tm->have_zone = 0;
00211                      return;
00212        }
00213 
00214        tm->is_localtime = 1;
00215        tm->have_zone = 1;
00216 }
00217 
00218 void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
00219 {
00220        timelib_time_offset *gmt_offset;
00221 
00222        gmt_offset = timelib_get_time_zone_info(t->sse, tz);
00223        t->z = gmt_offset->offset;
00224 /*
00225        if (t->dst != gmt_offset->is_dst) {
00226               printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
00227               exit(1);
00228        }
00229 */
00230        t->dst = gmt_offset->is_dst;
00231        t->tz_info = tz;
00232        if (t->tz_abbr) {
00233               free(t->tz_abbr);
00234        }
00235        t->tz_abbr = strdup(gmt_offset->abbr);
00236        timelib_time_offset_dtor(gmt_offset);
00237 
00238        t->have_zone = 1;
00239        t->zone_type = TIMELIB_ZONETYPE_ID;
00240 }
00241 
00242 /* Converts the time stored in the struct to localtime if localtime = true,
00243  * otherwise it converts it to gmttime. This is only done when necessary
00244  * ofcourse. */
00245 int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
00246 {
00247        if (localtime) {
00248               /* Converting from GMT time to local time */
00249               TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
00250 
00251               /* Check if TZ is set */
00252               if (!t->tz_info) {
00253                      TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
00254                      return -1;
00255               }
00256 
00257               timelib_unixtime2local(t, t->sse);
00258        } else {
00259               /* Converting from local time to GMT time */
00260               TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
00261 
00262               timelib_unixtime2gmt(t, t->sse);
00263        }
00264        return 0;
00265 }