From 0c9fc6e1878fb7d56f6cff0109ea546fda7710c8 Mon Sep 17 00:00:00 2001
From: Roman Shaposhnik <roman@shaposhnik.org>
Date: Wed, 18 Aug 2004 08:15:07 +0000
Subject: [PATCH]    * replacing calls to not-always-available gmtime_r with
 our own code.      The new helper function actually differs in semantics from
 gmtime_r,      so if that seems to be a problem I can actually move it to
 dv.c      completely, since only DV muxer uses the code anyway.

Originally committed as revision 3399 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavformat/avformat.h |  1 +
 libavformat/cutils.c   | 34 ++++++++++++++++++++++++++++++++++
 libavformat/dv.c       | 10 +++++-----
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 5f2e56c67b..d286d29bcf 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -620,6 +620,7 @@ do {\
 #endif
 
 time_t mktimegm(struct tm *tm);
+struct tm *brktimegm(time_t secs, struct tm *tm);
 const char *small_strptime(const char *p, const char *fmt, 
                            struct tm *dt);
 
diff --git a/libavformat/cutils.c b/libavformat/cutils.c
index c31e5b5c17..1415e4e392 100644
--- a/libavformat/cutils.c
+++ b/libavformat/cutils.c
@@ -147,6 +147,40 @@ time_t mktimegm(struct tm *tm)
     return t;
 }
 
+#define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
+#define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
+
+/* this is our own gmtime_r. it differs from its POSIX counterpart in a 
+   couple of places, though. */
+struct tm *brktimegm(time_t secs, struct tm *tm)
+{   
+    int days, y, ny, m;
+    int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+    days = secs / 86400;
+    secs %= 86400;
+    tm->tm_hour = secs / 3600;
+    tm->tm_min = (secs % 3600) / 60;
+    tm->tm_sec =  secs % 60;
+
+    /* oh well, may be someone some day will invent a formula for this stuff */
+    y = 1970; /* start "guessing" */
+    while (days >= (ISLEAP(y)?366:365)) {
+	ny = (y + days/366);
+	days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
+	y = ny;
+    }
+    md[1] = ISLEAP(y)?29:28;
+    for (m=0; days >= md[m]; m++)
+	 days -= md[m];
+
+    tm->tm_year = y;  /* unlike gmtime_r we store complete year here */
+    tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
+    tm->tm_mday = days+1;
+
+    return tm;
+}
+
 /* get a positive number between n_min and n_max, for a maximum length
    of len_max. Return -1 if error. */
 static int date_get_num(const char **pp,
diff --git a/libavformat/dv.c b/libavformat/dv.c
index 888dc2d1b8..4baa7a9d6d 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -195,7 +195,7 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu
     case dv_timecode:
           ct = (time_t)(c->frames / ((float)c->sys->frame_rate / 
                                      (float)c->sys->frame_rate_base));
-          gmtime_r(&ct, &tc);
+          brktimegm(ct, &tc);
           /* 
            * LTC drop-frame frame counter drops two frames (0 and 1) every 
            * minute, unless it is exactly divisible by 10
@@ -253,15 +253,15 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu
     case dv_viedo_recdate:  /* VAUX recording date */
           ct = c->start_time + (time_t)(c->frames / 
 	       ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
-          gmtime_r(&ct, &tc);
+          brktimegm(ct, &tc);
 	  buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
 	                 /* 0xff is very likely to be "unknown" */
 	  buf[2] = (3 << 6) | /* reserved -- always 1 */
 		   ((tc.tm_mday / 10) << 4) | /* Tens of day */
 		   (tc.tm_mday % 10);         /* Units of day */
 	  buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
-	           (((tc.tm_mon + 1) / 10) << 4) |    /* Tens of month */
-		   ((tc.tm_mon + 1) % 10);            /* Units of month */
+	           ((tc.tm_mon / 10) << 4) |    /* Tens of month */
+		   (tc.tm_mon  % 10);           /* Units of month */
 	  buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
 		   (tc.tm_year % 10);                 /* Units of year */
           break;
@@ -269,7 +269,7 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu
     case dv_video_rectime:  /* VAUX recording time */
           ct = c->start_time + (time_t)(c->frames / 
 	       ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
-          gmtime_r(&ct, &tc);
+	  brktimegm(ct, &tc);
 	  buf[1] = (3 << 6) | /* reserved -- always 1 */
 		   0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
 	  buf[2] = (1 << 7) | /* reserved -- always 1 */