MHEG5  18.9.0
MHEG5 Documentation
mh5date.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2000 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
26 /*---includes for this file--------------------------------------------------*/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "mh5base.h"
32 #include "mh5date.h"
33 #include "mh5memory.h"
34 #include "mh5debug.h"
35 #include "mherrors.h"
36 #include "dvb_misc.h"
37 
38 /*---constant definitions for this file--------------------------------------*/
39 #define DATE_BUFFER_SIZE (1024)
40 
41 
42 /*---local typedef structs for this file-------------------------------------*/
43 
44 /*---local (static) variable declarations for this file----------------------*/
45 
46 /*---local function definitions----------------------------------------------*/
47 
48 /*---global function definitions---------------------------------------------*/
49 
50 U32BIT JulianDate(S32BIT day, S32BIT month, S32BIT year);
51 static void CalendarDate(U32BIT jdate, S32BIT *day, S32BIT *month, S32BIT *year);
52 
63 {
64  S32BIT m1 = (month - 14) / 12;
65  S32BIT y1 = year + 4800;
66 
67  return 1461 * (y1 + m1) / 4 + 367 * (month - 2 - 12 * m1) / 12 - (3 * ((y1 + m1 + 100) / 100)) / 4 +
68  day - 2432076;
69 }
70 
81 static void CalendarDate(U32BIT jdate, S32BIT *day, S32BIT *month, S32BIT *year)
82 {
83  S32BIT p, q, r, s, t, u, v;
84 
85  /* Check output variables. */
86  if ((day == 0) || (month == 0) || (year == 0))
87  {
88  DEBUG_PRINT(("ERROR: CalendarDate invalid function parameters.\n"));
89  return; /* RETURN */
90  }
91 
92  p = jdate + 2468570;
93  q = 4 * p / 146097;
94  r = p - (146097 * q + 3) / 4;
95  s = 4000 * (r + 1) / 1461001;
96  t = r - 1461 * s / 4 + 31;
97  u = 80 * t / 2447;
98  v = u / 11;
99 
100  *year = 100 * (q - 49) + s + v;
101  *month = u + 2 - 12 * v;
102  *day = t - 2447 * u / 80;
103 }
104 
111 void MHEG5getDate(S32BIT *day, S32BIT *sec)
112 {
113  /* Local variables */
114  S_DateTime t;
115  E_MhegErr err;
116 
117  assert(day);
118  assert(sec);
119 
120  /* Get current date and time. */
121  err = DVB_MhegGetLocalTime(&t);
122  if (err != MHERR_OK)
123  {
124  *day = 0;
125  *sec = 0;
126  DEBUG_PRINT(("ERROR: MHEG5getDate unable to get current time. GetLocalTime returned %d\n", err));
127  return; /* RETURN */
128  }
129 
130  /* Calculate seconds since midnight. */
131  *sec = t.second + (t.minute * 60) + (t.hour * 3600);
132 
133  /* Calculate days since Nov 17, 1858. */
134  *day = JulianDate(t.day, t.month, t.year);
135 }
136 
145 {
146  /* Local variables. */
147  MHEG5String rc;
148  S32BIT d, m, y, i = 0, h, min, s, h2;
149  char buf[DATE_BUFFER_SIZE], buf2[100];
150 
151  /* Get calendar date format. */
152  CalendarDate(day, &d, &m, &y);
153 
154  /* Calculate seconds, minutes and hours. */
155  h2 = h = sec / 3600;
156  min = (sec - (h * 3600)) / 60;
157  s = sec - h * 3600 - min * 60;
158  if (h2 > 12)
159  {
160  h2 -= 12;
161  }
162 
163  /* Initialise string buffer. */
164  buf[0] = '\0';
165 
166  /* Step through format string. */
167  while (i < format.len)
168  {
169  /* Get first formatting instruction. */
170  if ((format.data[i] == '%') && (i + 1 < format.len))
171  {
172  switch (format.data[i + 1])
173  {
174  case 'Y': /* Use 4 digit year. */
175  sprintf(buf2, "%04ld", (long)y);
176  i += 2;
177  break;
178  case 'y': /* Use 2 digit year. */
179  sprintf(buf2, "%02ld", (long)(y % 100));
180  i += 2;
181  break;
182  case 'X': /* Month with padding. */
183  sprintf(buf2, "%02ld", (long)m);
184  i += 2;
185  break;
186  case 'x': /* Month without padding. */
187  sprintf(buf2, "%ld", (long)m);
188  i += 2;
189  break;
190  case 'D': /* Day with padding. */
191  sprintf(buf2, "%02ld", (long)d);
192  i += 2;
193  break;
194  case 'd': /* Day without padding. */
195  sprintf(buf2, "%ld", (long)d);
196  i += 2;
197  break;
198  case 'H': /* 24 hour with padding. */
199  sprintf(buf2, "%02ld", (long)h);
200  i += 2;
201  break;
202  case 'h': /* 24 hour without padding. */
203  sprintf(buf2, "%ld", (long)h);
204  i += 2;
205  break;
206  case 'I': /* 12 hour with padding. */
207  sprintf(buf2, "%02ld", (long)h2);
208  i += 2;
209  break;
210  case 'i': /* 12 hour without padding. */
211  sprintf(buf2, "%ld", (long)h2);
212  i += 2;
213  break;
214  case 'M': /* Minutes with padding. */
215  sprintf(buf2, "%02ld", (long)min);
216  i += 2;
217  break;
218  case 'm': /* Minutes without padding. */
219  sprintf(buf2, "%ld", (long)min);
220  i += 2;
221  break;
222  case 'S': /* Seconds with padding. */
223  sprintf(buf2, "%02ld", (long)s);
224  i += 2;
225  break;
226  case 's': /* Seconfds without padding. */
227  sprintf(buf2, "%ld", (long)s);
228  i += 2;
229  break;
230  case 'A': /* AM/PM style. */
231  if (h < 12)
232  sprintf(buf2, "AM");
233  else
234  sprintf(buf2, "PM");
235  i += 2;
236  break;
237  case 'a': /* am/pm style. */
238  if (h < 12)
239  sprintf(buf2, "am");
240  else
241  sprintf(buf2, "pm");
242  i += 2;
243  break;
244  case '%': /* Use % char. */
245  buf2[0] = '%';
246  buf2[1] = '\0';
247  i += 2;
248  break;
249  default: /* Copy char to output string. */
250  buf2[0] = format.data[i];
251  buf2[1] = format.data[i + 1];
252  buf2[2] = '\0';
253  i += 2;
254  }
255  }
256  else
257  {
258  buf2[0] = format.data[i++];
259  buf2[1] = '\0';
260  }
261 
262  /* Check for buffer overflow */
263  if (strlen(buf2) >= DATE_BUFFER_SIZE - strlen(buf))
264  {
265  /* End MHEG5formatdate function */
266  break;
267  }
268  else
269  {
270  strcat(buf, buf2);
271  }
272  }
273 
274  rc = MHEG5stringCopyChr(buf);
275 
276  return rc; /* RETURN */
277 }
278 
286 {
287  /* Calculate and return day of the week */
288  return (day + 3) % 7;
289 }
290 
Date functions.
Basis MHEG5 data types.
E_MhegErr DVB_MhegGetLocalTime(S_DateTime *pDateAndTime)
Provide the current local time and date, normally from the system real time clock, with any local time conversions (if necessary). The returned time should take into account local timezone and daylight saving settings.
U32BIT month
Definition: dvb_misc.h:55
U32BIT day
Definition: dvb_misc.h:60
U32BIT hour
Definition: dvb_misc.h:65
U32BIT year
Definition: dvb_misc.h:50
E_MhegErr
Definition: mherrors.h:28
MHEG5 engine interface error codes.
U32BIT minute
Definition: dvb_misc.h:70
U32BIT second
Definition: dvb_misc.h:75
#define DATE_BUFFER_SIZE
Definition: mh5date.c:39
MHEG5Byte * data
Definition: mh5base.h:85
int32_t S32BIT
Definition: techtype.h:87
U32BIT JulianDate(S32BIT day, S32BIT month, S32BIT year)
The function JulianDate() calculates the julian day number for the specified day, month and year...
Definition: mh5date.c:62
Mheg5 logging and debug printing.
MHEG5String MHEG5stringCopyChr(const char *source)
Copy the C-String source to a MHEG5String.
Definition: mh5base.c:612
redirection include
void MHEG5getDate(S32BIT *day, S32BIT *sec)
Modified Julian Date - see Davic 9.2.12.1.
Definition: mh5date.c:111
MHEG5Int len
Definition: mh5base.h:84
MHEG5String MHEG5formatDate(MHEG5String format, S32BIT day, S32BIT sec)
Format date - See Davic part 9.0 - 9.2.12.1.
Definition: mh5date.c:144
S32BIT MHEG5getDayOfWeek(S32BIT day)
Get Day of Week for Davic-Day 0 is Sunday....
Definition: mh5date.c:285
References: [1] UK1 Profile - Digital Terrestrial Television - Requirements for interoperability (The...
uint32_t U32BIT
Definition: techtype.h:86
#define DEBUG_PRINT(x)
Definition: mh5debug.h:70