root/webserver/example/freeRTOSexample/Source/EnergyMeters/timeConversion.c

Revision 22, 3.2 kB (checked in by phil, 15 years ago)

deleted some test files,
gas meter values (unsigned long long) now updated.
added epoch time handling

Line 
1
2 #include <targets/LPC23xx.h>
3 #include "FreeRTOS.h"
4 #include "MetersIncludes.h"
5 #include "task.h"
6 #include "StackMacros.h"
7
8 #define MASKSEC 0x3F // Second 00..59 00000000:00000000:00xxxxxx
9 #define MASKMIN 0x3F00 // Minute 00..59 00000000:00xxxxxx:00000000
10 #define MASKHR 0x1F0000 // Hour 00..23 000xxxxx:00000000:00000000
11
12 extern xTickCount; /* tick counter (milliseconds) from FreeRTOS */
13
14 /* taken from Linux Kernel 2.6.30 */
15 portLONG
16 mktime(const unsigned int year0, const unsigned int mon0,
17        const unsigned int day, const unsigned int hour,
18        const unsigned int min, const unsigned int sec);
19 unsigned long long getEpochTimeWithMs(void)
20 {
21   /* get both registers while in critical section to eliminate the chance of inconsistencies when there are
22    * overflows of the SECONDS value.
23    */
24   unsigned int timeHour;
25   unsigned int timeMin;
26   unsigned int timeSec;
27   unsigned int timeDay;
28   unsigned int timeMonth;
29   unsigned int timeYear;
30   unsigned long long tempTickTime;
31   portLONG cTIME0;
32   portLONG cTIME1;
33
34 //  portENTER_CRITICAL();
35   cTIME0 = RTC_CTIME0;
36   cTIME1 = RTC_CTIME1;
37 //  portEXIT_CRITICAL();
38
39
40   timeHour = (cTIME0 & MASKHR)>>16;     // Hour
41   timeMin = (cTIME0 & MASKMIN)>>8;      // Minute
42   timeSec = cTIME0 & MASKSEC;   // Second
43   timeDay = cTIME1 & 0xf;           // Day
44   timeMonth = (cTIME1 >> 8) & 0xF;  // Month
45   timeYear = (cTIME1 >> 16) & 0xFFF; // Year
46
47   /* we get the epoch time, multiply it by 1000 and then add the milliseconds portion
48      of the Tick timer. This returns a millisecond time since epoch.
49   */
50   tempTickTime = mktime(timeYear, timeMonth, timeDay, timeHour, timeMin, timeSec);
51   tempTickTime -= 3600 * 2; /* time zone adaptation (2 h) */
52   tempTickTime = (tempTickTime * 1000) + (xTickCount % 1000);
53   return tempTickTime;
54 }
55
56
57 // mktime from linux kernel 2.6.30 kernel / time.c
58 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
59  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
60  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
61  *
62  * [For the Julian calendar (which was used in Russia before 1917,
63  * Britain & colonies before 1752, anywhere else before 1582,
64  * and is still in use by some communities) leave out the
65  * -year/100+year/400 terms, and add 10.]
66  *
67  * This algorithm was first published by Gauss (I think).
68  *
69  * WARNING: this function will overflow on 2106-02-07 06:28:16 on
70  * machines where long is 32-bit! (However, as time_t is signed, we
71  * will already get problems at other places on 2038-01-19 03:14:08)
72  */
73 portLONG
74 mktime(const unsigned int year0, const unsigned int mon0,
75        const unsigned int day, const unsigned int hour,
76        const unsigned int min, const unsigned int sec)
77 {
78         unsigned int mon = mon0, year = year0;
79
80         /* 1..12 -> 11,12,1..10 */
81         if (0 >= (int) (mon -= 2)) {
82                 mon += 12;      /* Puts Feb last since it has leap day */
83                 year -= 1;
84         }
85
86         return ((((portLONG)
87                   (year/4 - year/100 + year/400 + 367*mon/12 + day) +
88                   year*365 - 719499
89             )*24 + hour /* now have hours */
90           )*60 + min /* now have minutes */
91         )*60 + sec; /* finally seconds */
92 }
Note: See TracBrowser for help on using the browser.