File: filetime.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (104 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "wv.h"

void wvGetFILETIME(FILETIME *ft,FILE *fd)
	{
	ft->dwLowDateTime = read_32ubit(fd);
	ft->dwHighDateTime = read_32ubit(fd);
	}

void wvInitFILETIME(FILETIME *ft)
	{
	ft->dwLowDateTime = 0;
	ft->dwHighDateTime = 0;
	}

int wvFileTimeToDosDateTime(const FILETIME *ft, U16 *fatdate, U16 *fattime )
	{
    time_t unixtime = wvDOSFS_FileTimeToUnixTime( ft, NULL );
    struct tm *tm = localtime( &unixtime );
    if (fattime)
        *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
    if (fatdate)
        *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
                   + tm->tm_mday;
    return(1);
	}


/***********************************************************************
 *           DOSFS_FileTimeToUnixTime
 *
 * Convert a FILETIME format to Unix time.
 * If not NULL, 'remainder' contains the fractional part of the filetime,
 * in the range of [0..9999999] (even if time_t is negative).
 */
time_t wvDOSFS_FileTimeToUnixTime( const FILETIME *filetime, U32 *remainder )
	{
	U32 a0;            /* 16 bit, low    bits */
    U32 a1;            /* 16 bit, medium bits */
    U32 a2;            /* 32 bit, high   bits */
    U32 r;         /* remainder of division */
    unsigned int carry;     /* carry bit for subtraction */
    int negative;       /* whether a represents a negative value */

    /* Copy the time values to a2/a1/a0 */
    a2 =  filetime->dwHighDateTime;
    a1 = ((U32)filetime->dwLowDateTime ) >> 16;
    a0 = ((U32)filetime->dwLowDateTime ) & 0xffff;

    /* Subtract the time difference */
    if (a0 >= 32768           ) a0 -=             32768        , carry = 0;
    else                        a0 += (1 << 16) - 32768        , carry = 1;

    if (a1 >= 54590    + carry) a1 -=             54590 + carry, carry = 0;
    else                        a1 += (1 << 16) - 54590 - carry, carry = 1;

    a2 -= 27111902 + carry;

    /* If a is negative, replace a by (-1-a) */
    negative = (a2 >= ((U32)1) << 31);
    if (negative)
    {
    /* Set a to -a - 1 (a is a2/a1/a0) */
    a0 = 0xffff - a0;
    a1 = 0xffff - a1;
    a2 = ~a2;
    }

    /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
       Split the divisor into 10000 * 1000 which are both less than 0xffff. */
    a1 += (a2 % 10000) << 16;
    a2 /=       10000;
    a0 += (a1 % 10000) << 16;
    a1 /=       10000;
    r   =  a0 % 10000;
    a0 /=       10000;

    a1 += (a2 % 1000) << 16;
    a2 /=       1000;
    a0 += (a1 % 1000) << 16;
    a1 /=       1000;
    r  += (a0 % 1000) * 10000;
    a0 /=       1000;

    /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
    if (negative)
    {
    /* Set a to -a - 1 (a is a2/a1/a0) */
    a0 = 0xffff - a0;
    a1 = 0xffff - a1;
    a2 = ~a2;

        r  = 9999999 - r;
    }

    if (remainder) *remainder = r;
/* Do not replace this by << 32, it gives a compiler warning and it does
       not work. */
    return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;

	}