Implemented sub-second W32 filesystem interface by new: CaptiveFileInfoObject
[captive.git] / src / libcaptive / rtl / time.c
1 /* $Id$
2  * reactos time user/kernel memory handling functions emulation of libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captive/rtl-time.h"   /* self */
23 #include <glib/gmessages.h>
24 #include "reactos/ntos/rtl.h"   /* For RtlSecondsSince1970ToTime() */
25
26
27 /* It MUST match those of: reactos/ntoskrnl/rtl/time.c */
28 #define TICKSPERSEC        10000000
29 #define TICKSTO1970         0x019db1ded53e8000LL
30
31
32 /** timespec_to_Time:
33  * @Time: Target %PLARGE_INTEGER type target MS-Windows timestamp.
34  * %NULL value is forbidden.
35  * @timespec: Source %struct %timespec type UNIX timestamp.
36  * %NULL value is forbidden.
37  *
38  * Converts the timestamp format including the sub-second part not handled by
39  * just ReactOS RtlSecondsSince1970ToTime().
40  *
41  * Returns: %GNOME_VFS_OK if everything got converted OK.
42  */
43 GnomeVFSResult timespec_to_Time(PLARGE_INTEGER Time,const struct timespec *timespec)
44 {
45         g_return_val_if_fail(Time!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
46         g_return_val_if_fail(timespec!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
47         g_return_val_if_fail(timespec->tv_nsec<0,GNOME_VFS_ERROR_BAD_PARAMETERS);
48         g_return_val_if_fail(timespec->tv_nsec>1000000000,GNOME_VFS_ERROR_BAD_PARAMETERS);
49
50         RtlSecondsSince1970ToTime(timespec->tv_sec,Time);
51
52         /* #define TICKSPERSEC 10000000 */
53 #if TICKSPERSEC<1000000000 && !(1000000000%TICKSPERSEC)
54         Time->QuadPart+=timespec->tv_nsec / (1000000000/TICKSPERSEC);
55 #else
56 #error "Unsupported: TICKSPERSEC"
57 #endif
58
59         return GNOME_VFS_OK;
60 }
61
62 /** Time_to_timespec:
63  * @timespec: Target %struct %timespec type UNIX timestamp.
64  * %NULL value is forbidden.
65  * @Time: Source %PLARGE_INTEGER type target MS-Windows timestamp.
66  * %NULL value is forbidden.
67  *
68  * Converts the timestamp format including the sub-second part not handled by
69  * just ReactOS RtlTimeToSecondsSince1970().
70  *
71  * Returns: %GNOME_VFS_OK if everything got converted OK.
72  */
73 GnomeVFSResult Time_to_timespec(struct timespec *timespec,const PLARGE_INTEGER Time)
74 {
75 ULONG SecondsSince1970;
76 BOOLEAN errBOOLEAN;
77
78         g_return_val_if_fail(timespec!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
79         g_return_val_if_fail(Time!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
80
81         errBOOLEAN=RtlTimeToSecondsSince1970(Time,&SecondsSince1970);
82         g_assert(errBOOLEAN==TRUE);
83
84         timespec->tv_sec=SecondsSince1970;
85
86         /* #define TICKSPERSEC 10000000 */
87
88         /* Some problem with the remainer? */
89 #if TICKSTO1970%TICKSPERSEC
90 #error "Unsupported: TICKSTO1970
91 #endif
92
93 #if TICKSPERSEC<1000000000 && !(1000000000%TICKSPERSEC)
94         timespec->tv_nsec=(Time->QuadPart%TICKSPERSEC) * (1000000000/TICKSPERSEC);
95 #else
96 #error "Unsupported: TICKSPERSEC"
97 #endif
98
99         return GNOME_VFS_OK;
100 }