aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/date.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2003-11-23 18:12:54 +0000
committerrsc <devnull@localhost>2003-11-23 18:12:54 +0000
commitfd04aacee17b348da206c13a550dc1029669805f (patch)
tree9bdd35a25ff6e3d6e9a0171b06240a76723f922c /src/lib9/date.c
parent74f990ad84deb1591ddb91be4fc8152ec0c46222 (diff)
downloadplan9port-fd04aacee17b348da206c13a550dc1029669805f.tar.gz
plan9port-fd04aacee17b348da206c13a550dc1029669805f.tar.bz2
plan9port-fd04aacee17b348da206c13a550dc1029669805f.zip
Various additions and fixes.
Diffstat (limited to 'src/lib9/date.c')
-rw-r--r--src/lib9/date.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/lib9/date.c b/src/lib9/date.c
new file mode 100644
index 00000000..2e84fd0a
--- /dev/null
+++ b/src/lib9/date.c
@@ -0,0 +1,77 @@
+#include <u.h>
+#include <libc.h>
+
+#undef gmtime
+#undef localtime
+#undef asctime
+#undef ctime
+#undef cputime
+#undef times
+#undef tm2sec
+#undef nsec
+
+#include <time.h>
+
+static Tm bigtm;
+
+static void
+tm2Tm(struct tm *tm, Tm *bigtm)
+{
+ memset(bigtm, 0, sizeof *bigtm);
+ bigtm->sec = tm->tm_sec;
+ bigtm->min = tm->tm_min;
+ bigtm->hour = tm->tm_hour;
+ bigtm->mday = tm->tm_mday;
+ bigtm->mon = tm->tm_mon;
+ bigtm->year = tm->tm_year;
+ bigtm->wday = tm->tm_wday;
+ strecpy(bigtm->zone, bigtm->zone+4, tm->tm_zone);
+ bigtm->tzoff = tm->tm_gmtoff;
+}
+
+static void
+Tm2tm(Tm *bigtm, struct tm *tm)
+{
+ memset(tm, 0, sizeof *tm);
+ tm->tm_sec = bigtm->sec;
+ tm->tm_min = bigtm->min;
+ tm->tm_hour = bigtm->hour;
+ tm->tm_mday = bigtm->mday;
+ tm->tm_mon = bigtm->mon;
+ tm->tm_year = bigtm->year;
+ tm->tm_wday = bigtm->wday;
+ tm->tm_zone = bigtm->zone;
+ tm->tm_gmtoff = bigtm->tzoff;
+}
+
+Tm*
+p9gmtime(long t)
+{
+ struct tm tm;
+
+ tm = *gmtime(&t);
+ tm2Tm(&tm, &bigtm);
+ return &bigtm;
+}
+
+Tm*
+p9localtime(long t)
+{
+ struct tm tm;
+
+ tm = *localtime(&t);
+ tm2Tm(&tm, &bigtm);
+ return &bigtm;
+}
+
+long
+p9tm2sec(Tm *bigtm)
+{
+ struct tm tm;
+
+ Tm2tm(bigtm, &tm);
+ if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
+ return timegm(&tm);
+ return mktime(&tm); /* local time zone */
+}
+