summaryrefslogtreecommitdiff
path: root/clock.c
blob: 270edff500945e12bf2e7f18e2210329c653e297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/************************************************************************/
/*									*/
/*			Clock / Calendar				*/
/*                                                                      */
/*              Author: Peter Dannegger                                 */
/*                      danni@specs.de                                  */
/*                                                                      */
/************************************************************************/

#include "main.h"
#include "clock.h"
#include "timebase.h"
#include "dcf77.h"

struct time data time;


const u8 code MDAYS[] = {
    29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

u8 is_cet(void)
{
	return ((time.flags & FLAG_CET) && !(time.flags & FLAG_CEST));
}

u8 is_cest(void)
{
	return ((time.flags & FLAG_CEST) && !(time.flags & FLAG_CET));
}

void clock(void)
{
  u8 i;

  time.second++;
  if( time.second >= 60 ){
    if (time.flags & FLAG_LEAP_SECOND && time.second == 60) {
      return;
    }
    time.second = 0;
    timeflags = 1<<ONE_MINUTE;		// to correct deviation per minute
    time.minute++;
    if( time.minute == 60 ){
      time.minute = 0;
      time.hour++;
      if (time.hour == 24) {
	  time.hour = 0;
	  time.day++;
	  time.wday++;
	  if( time.wday == 8 )
	    time.wday = 1;
	  i = time.month;
	  if( i == 2 && !(time.year % 4) )	// leap year
	    i = 0;
	  if( LPM(MDAYS+i) < time.day ){
	    time.day = 1;
	    time.month++;
	    if( time.month == 13 ){
	      time.month = 1;
	      time.year++;
	      if( time.year == 100 )
	        time.year = 0;			// next century
	    }
	  }
      }
      if (time.flags & FLAG_SUMMERTIME_PENDING) {
		if (is_cet() && time.hour == 2) {
			time.hour = 3;
		}
		if (is_cest() && time.hour == 3) {
			time.hour = 2;
		}
      }
    }
  }
}