summaryrefslogtreecommitdiff
path: root/libc/msdos/time.c
blob: 271ea052feab7639ff3536a1eaef5f96253d5f59 (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

#include <time.h>

static int mdays[13] = { 0,31,31+28,31+28+31,31+28+31+30,
	31+28+31+30+31,31+28+31+30+31+30,31+28+31+30+31+30+31,
	31+28+31+30+31+30+31+31,31+28+31+30+31+30+31+31+30,
	31+28+31+30+31+30+31+31+30+31,31+28+31+30+31+30+31+31+30+31+30,
	365 };

#define SECSPERHOUR	(60*60)
#define SECSPERDAY	(SECSPERHOUR*24L)

/****************************************
 * Return the number of seconds that have elapsed since the start
 * of 1970.
 * Input:
 *	timer	pointer to where to store result (or NULL)
 * Output:
 *	*timer = result (unless timer == NULL)
 * Returns:
 *	time
 */

static long get_time(ah)
{
#asm
#if !__FIRST_ARG_IN_AX__
  mov	bx,sp
  mov	ax,[bx+2]
#endif
  mov	ah,al
  int	$21
  mov	ax,dx
  mov	dx,cx
#endasm
}

time_t time(timer)
time_t *timer;
{
  unsigned day,month,year;
  long rv;
  time_t t;

  rv = get_time(0x2C);
  rv >>= 8; t = (rv & 0xFF);
  rv >>= 8; t += (rv & 0xFF)*60;
  rv >>= 8; t += (rv & 0xFF)*3600;

  rv = get_time(0x2A);
  day = (rv & 0xFF);
  rv >>= 8; month = (rv & 0xFF) -1;
  rv >>= 8; year = (rv & 0xFFFF) - 1970;

  if (month <= 1 || year & 3)	/* if before Feb or not a leap year	*/
	day--;			/* don't add day for leap year		*/
  day += mdays[month];		/* day in year				*/
  day += (year + 3) >> 2;	/* add a day for each leap year		*/
  t += ((year * 365L) + day) * SECSPERDAY;
  if (timer)
	*timer = t;
  return t;
}