timecvt.c
Go to the documentation of this file.
1 
23 #include "pgrl.h"
24 #include "time.h"
25 
26 #define DefaultTimeZone -3L
27 
28 bank1 long timezone = DefaultTimeZone * 60L * 60L; /* Set for EST */
29 
30 static const char Days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
31 
40 long dostounix(struct date *d, struct time *t)
41 {
42  long x;
43  int i;
44  int days;
45  int hours;
46 
47  x = 24L * 60L * 60L * 3652L + timezone; /* Convert from 1980 to
48  1970 base date GMT */
49  i = d->da_year - 1980;
50  x += (i >> 2) * (1461L * 24L * 60L * 60L);
51  x += (i & 3) * (24L * 60L * 60L * 365L);
52  if (i & 3)
53  x += 24L * 3600L;
54  days = 0;
55  i = d->da_mon - 1; /* Add in months */
56  while (i > 0)
57  {
58  i--;
59  days += (int)Days[i];
60  }
61  days += d->da_day - 1;
62  if (d->da_mon > 2 && (d->da_year & 3) == 0)
63  days++; /* Currently in leap year */
64  hours = days * 24 + t->ti_hour; /* Find hours */
65 #ifdef _USEDAYLIGHT
66  if (daylight && __isDST( t->ti_hour, days, 0, d->da_year-1970))
67  hours--;
68 #endif
69  x += hours * 3600L;
70  x += 60L * t->ti_min + t->ti_sec;
71  return (x);
72 }
73 
74 
82 void unixtodos(long time, struct date *d, struct time *t)
83 {
84 
85  time -= 24L * 60L * 60L * 3652L + timezone;
86  t->ti_hund = 0;
87  t->ti_sec = time % 60;
88  time /= 60; /* Time in minutes */
89  t->ti_min = time % 60;
90  time /= 60; /* Time in hours */
91  d->da_year = 1980 + (int)((time / (1461L * 24L)) << 2);
92  time %= 1461L * 24L;
93  if (time >= 366 * 24)
94  {
95  time -= 366 * 24;
96  d->da_year++;
97  d->da_year += (int)(time / (365 * 24));
98  time %= 365 * 24;
99  }
100 #ifdef _USEDAYLIGHT
101  if (daylight && __isDST( (int)(time % 24), (int)(time / 24), 0, d->da_year-1970 ))
102  time++;
103 #endif
104  t->ti_hour = time % 24;
105  time /= 24; /* Time in days */
106  time++;
107  if ((d->da_year & 3) == 0)
108  {
109  if (time > 60)
110  time--;
111  else
112  if (time == 60)
113  {
114  d->da_mon = 2;
115  d->da_day = 29;
116  return;
117  }
118  }
119  for (d->da_mon = 0; Days[d->da_mon] < time; d->da_mon++)
120  time -= Days[d->da_mon];
121  d->da_mon++;
122  d->da_day = (char)time;
123 }
124 
125 
126 //*****************************************************************************[ENDL]***************
long dostounix(struct date *d, struct time *t)
Definition: timecvt.c:40
Microchip PIC C Generic, General defines.
bank1 long timezone
Definition: timecvt.c:28
TIME CONVERSION.
unsigned char ti_hour
Hours.
Definition: time.h:50
void unixtodos(long time, struct date *d, struct time *t)
Definition: timecvt.c:82
static const char Days[12]
Definition: timecvt.c:30
unsigned char ti_min
Minutes.
Definition: time.h:49
Definition: time.h:55
#define DefaultTimeZone
Definition: timecvt.c:26
int da_year
Year - 1980.
Definition: time.h:56
char da_day
Day of the month.
Definition: time.h:57
unsigned char ti_hund
Hundredths of seconds.
Definition: time.h:51
Definition: time.h:48
char da_mon
Month (1 = Jan)
Definition: time.h:58
unsigned char ti_sec
Seconds.
Definition: time.h:52