Permalink
Browse files

- Add my new timelib and ext/date. For now only strtotime() makes use…

… of this.
  • Loading branch information...
1 parent f14292d commit 4fb4cac65c735a9253d7b77f17468a5768a7de13 Derick Rethans committed Jun 14, 2005
View
@@ -2,6 +2,8 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? Jun 2005, PHP 5.1 Beta 2
- Added bindto socket context option. (Ilia)
+- Rewrote strtotime() with support for timezones and tons of new formats.
+ (Derick)
- Fixed PDO shutdown problem (possible inifite loop running rollback on
shutdown). (Wez)
- Fixed PECL bug #3714 (beginTransaction doesn't work if you're in
View
@@ -0,0 +1,2 @@
+Date/Time Support
+Derick Rethans
View
@@ -0,0 +1,4 @@
+- Fix one bug that prevent current testcases from working.
+- Implement INI settings default timezone, for now the env var "TZ" can be
+ used.
+- Port over my 200 test cases to .phpt format.
View
@@ -0,0 +1,6 @@
+dnl $Id$
+dnl config.m4 for date extension
+
+AC_DEFINE(HAVE_DATE, 1, [Whether you want date/time support])
+AC_DEFINE(TIMEZONE_DB_PATH, $libdir/timezonedb, [The path to the timezone database])
+PHP_NEW_EXTENSION(date, php_date.c lib/datetime.c lib/dow.c lib/parse_date.c lib/parse_tz.c lib/tm2unixtime.c lib/unixtime2tm.c, no)
View
@@ -0,0 +1,5 @@
+Regenerating Parser
+===================
+
+Make sure you use re2c 0.97 or higher:
+/dat/dev/sf/re2c/re2c -d -b parse_date.re > ../parse_date.c
@@ -0,0 +1,189 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2004 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.0 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_0.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Derick Rethans <dr@ez.no> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+#include "timelib_structs.h"
+#include "datetime.h"
+#include <ctype.h>
+
+timelib_time* timelib_time_ctor()
+{
+ timelib_time *t;
+ t = calloc(1, sizeof(timelib_time));
+
+ return t;
+}
+
+void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
+{
+ int i;
+
+ if (tm->tz_abbr) {
+ free(tm->tz_abbr);
+ }
+ tm->tz_abbr = strdup(tz_abbr);
+ for (i = 0; i < strlen(tz_abbr); i++) {
+ tm->tz_abbr[i] = toupper(tz_abbr[i]);
+ }
+}
+
+void timelib_time_dtor(timelib_time* t)
+{
+ if (t->tz_abbr) {
+ free(t->tz_abbr);
+ }
+ free(t);
+}
+
+timelib_time_offset* timelib_time_offset_ctor()
+{
+ timelib_time_offset *t;
+ t = calloc(1, sizeof(timelib_time_offset));
+
+ return t;
+}
+
+void timelib_time_offset_dtor(timelib_time_offset* t)
+{
+ if (t->abbr) {
+ free(t->abbr);
+ }
+ free(t);
+}
+
+timelib_tzinfo* timelib_tzinfo_ctor(char *name)
+{
+ timelib_tzinfo *t;
+ t = calloc(1, sizeof(timelib_tzinfo));
+ t->name = strdup(name);
+
+ return t;
+}
+
+timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
+{
+ timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
+ tmp->ttisgmtcnt = tz->ttisgmtcnt;
+ tmp->ttisstdcnt = tz->ttisstdcnt;
+ tmp->leapcnt = tz->leapcnt;
+ tmp->timecnt = tz->timecnt;
+ tmp->typecnt = tz->typecnt;
+ tmp->charcnt = tz->charcnt;
+
+ tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
+ tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
+ memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
+ memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
+
+ tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
+ memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
+
+ tmp->timezone_abbr = (char*) malloc(tz->charcnt);
+ memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
+
+ tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
+ memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
+
+ return tmp;
+}
+
+void timelib_tzinfo_dtor(timelib_tzinfo *tz)
+{
+ free(tz->name);
+ free(tz->trans);
+ free(tz->trans_idx);
+ free(tz->type);
+ free(tz->timezone_abbr);
+ free(tz->leap_times);
+ free(tz);
+}
+
+char *timelib_get_tz_abbr_ptr(timelib_time *t)
+{
+ if (!t->sse_uptodate) {
+ timelib_update_ts(t, NULL);
+ };
+ return t->tz_abbr;
+}
+
+signed long timelib_date_to_int(timelib_time *d, int *error)
+{
+ timelib_sll ts;
+
+ ts = d->sse;
+
+ if (ts < LONG_MIN || ts > LONG_MAX) {
+ if (error) {
+ *error = 1;
+ }
+ return 0;
+ }
+ if (error) {
+ *error = 0;
+ }
+ return d->sse;
+}
+
+void timelib_dump_date(timelib_time *d, int options)
+{
+ if ((options & 2) == 2) {
+ printf("TYPE: %d ", d->zone_type);
+ }
+ printf("TS: %lld | %04lld-%02lld-%02lld %02lld:%02lld:%02lld",
+ d->sse, d->y, d->m, d->d, d->h, d->i, d->s);
+ if (d->f > +0.0) {
+ printf(" %.5f", d->f);
+ }
+
+ if (d->is_localtime) {
+ switch (d->zone_type) {
+ case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
+ printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
+ break;
+ case TIMELIB_ZONETYPE_ID: /* Timezone struct */
+ /* Show abbreviation if wanted */
+ if (d->tz_abbr) {
+ printf(" %s", d->tz_abbr);
+ }
+ /* Do we have a TimeZone struct? */
+ if (d->tz_info) {
+ printf(" %s", d->tz_info->name);
+ }
+ break;
+ case TIMELIB_ZONETYPE_ABBR:
+ printf(" %s", d->tz_abbr);
+ printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
+ break;
+ }
+ } else {
+ printf(" GMT 00000");
+ }
+
+ if ((options & 1) == 1) {
+ if (d->have_relative) {
+ printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS",
+ d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
+ }
+ if (d->have_weekday_relative) {
+ printf(" / %d", d->relative.weekday);
+ }
+ }
+ printf("\n");
+}
+
@@ -0,0 +1,63 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2004 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.0 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_0.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Derick Rethans <dr@ez.no> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+#include "timelib_structs.h"
+
+#define TIMELIB_NONE 0x00
+#define TIMELIB_OVERRIDE_TIME 0x01
+
+/* From dow.c */
+int timelib_day_of_week(int y, int m, int d);
+int timelib_daynr_from_weeknr(int y, int w, int d);
+
+/* From parse_date.re */
+timelib_time *timelib_strtotime(char *s);
+void timelib_fill_holes(timelib_time *parsed, timelib_time *now, int options);
+
+/* From tm2unixtime.c */
+void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi);
+
+/* From unixtime2tm.c */
+int timelib_apply_localtime(timelib_time *t, uint localtime);
+void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts);
+void timelib_unixtime2local(timelib_time *tm, timelib_sll ts, timelib_tzinfo* tz);
+void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz);
+
+/* From parse_tz.c */
+timelib_tzinfo *timelib_parse_tzfile(char *timezone);
+int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz);
+timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz);
+
+/* From datetime.c */
+timelib_tzinfo* timelib_tzinfo_ctor();
+void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr);
+void timelib_time_tz_name_update(timelib_time* tm, char* tz_name);
+void timelib_tzinfo_dtor(timelib_tzinfo *tz);
+timelib_tzinfo* timelib_tzinfo_clone(timelib_tzinfo *tz);
+
+timelib_time* timelib_time_ctor();
+void timelib_time_dtor(timelib_time* t);
+
+timelib_time_offset* timelib_time_offset_ctor();
+void timelib_time_offset_dtor(timelib_time_offset* t);
+
+signed long timelib_date_to_int(timelib_time *d, int *error);
+void timelib_dump_date(timelib_time *d, int options);
+
View
@@ -0,0 +1,67 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2004 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.0 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_0.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Derick Rethans <dr@ez.no> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+#include "timelib_structs.h"
+
+static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
+static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
+
+static int century_value(int j)
+{
+ int i = j - 17;
+ int c = (4 - i * 2 + (i + 1) / 4) % 7;
+
+ return c < 0 ? c + 7 : c;
+}
+
+int timelib_day_of_week(int y, int m, int d)
+{
+ int c1, y1, m1;
+
+ /* Only valid for Gregorian calendar */
+ if (y < 1753) {
+ return -1;
+ }
+ c1 = century_value(y / 100);
+ y1 = (y % 100);
+ m1 = is_leap(y) ? m_table_leap[m] : m_table_common[m];
+ return (c1 + y1 + m1 + (y1 / 4) + d) % 7;
+}
+
+int timelib_daynr_from_weeknr(int y, int w, int d)
+{
+ int dow, day;
+
+ /* Figure out the dayofweek for y-1-1 */
+ dow = timelib_day_of_week(y, 1, 1);
+ /* then use that to figure out the offset for day 1 of week 1 */
+ day = 0 - (dow > 4 ? dow - 7 : dow);
+
+ /* Add weeks and days */
+ return day + ((w - 1) * 7) + d;
+}
+
+#if 0
+int main(void)
+{
+ printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
+ printf("dow = %d\n", timelib_day_of_week(2005, 2, 19)); /* 6 */
+}
+#endif
Oops, something went wrong.

0 comments on commit 4fb4cac

Please sign in to comment.