summaryrefslogtreecommitdiff
path: root/com32/sysdump/ctime.c
blob: 56c8efb6d98a7ca99197cb5f9308bbf27bdecdb8 (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
#include <com32.h>
#include <string.h>
#include "ctime.h"

static uint8_t frombcd(uint8_t v)
{
    uint8_t a = v & 0x0f;
    uint8_t b = v >> 4;

    return a + b*10;
}

uint32_t posix_time(void)
{
    /* Days from March 1 for a specific month, starting in March */
    static const unsigned int yday[12] =
	{ 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337 };
    com32sys_t ir, d0, d1, t0;
    unsigned int c, y, mo, d, h, m, s;
    uint32_t t;

    memset(&ir, 0, sizeof ir);

    ir.eax.b[1] = 0x04;
    __intcall(0x1A, &ir, &d0);

    ir.eax.b[1] = 0x02;
    __intcall(0x1A, &ir, &t0);

    ir.eax.b[1] = 0x04;
    __intcall(0x1A, &ir, &d1);

    if (t0.ecx.b[1] < 0x12)
	d0 = d1;

    c  = frombcd(d0.ecx.b[1]);
    y  = frombcd(d0.ecx.b[0]);
    mo = frombcd(d0.edx.b[1]);
    d  = frombcd(d0.edx.b[0]);

    h  = frombcd(t0.ecx.b[1]);
    m  = frombcd(t0.ecx.b[0]);
    s  = frombcd(t0.edx.b[1]);

    /* We of course have no idea about the timezone, so ignore it */

    /*
     * Look for impossible dates... this code was written in 2010, so
     * assume any century less than 20 is just broken.
     */
    if (c < 20)
	c = 20;
    y += c*100;

    /* Consider Jan and Feb as the last months of the previous year */
    if (mo < 3) {
	y--;
	mo += 12;
    }

    /*
     * Just in case: if the month is nonsense, don't read off the end
     * of the table...
     */
    if (mo-3 > 11)
	return 0;

    t = y*365 + y/4 - y/100 + y/400 + yday[mo-3] + d - 719469;
    t *= 24;
    t += h;
    t *= 60;
    t += m;
    t *= 60;
    t += s;

    return t;
}