summaryrefslogtreecommitdiffstatshomepage
path: root/windows
diff options
context:
space:
mode:
authorstijn <stinos@zoho.com>2014-05-08 10:56:33 +0200
committerstijn <stinos@zoho.com>2014-05-09 13:58:15 +0200
commit5ed284a15e028e0435f3b6e0773e14225d8f165d (patch)
treeb83683ee2d84e423cc00cf630ebb688430a119c4 /windows
parentd25cba4f642e50e2f713b48f5f079036b352f72d (diff)
downloadmicropython-5ed284a15e028e0435f3b6e0773e14225d8f165d.tar.gz
micropython-5ed284a15e028e0435f3b6e0773e14225d8f165d.zip
windows: Add modtime implementation
Diffstat (limited to 'windows')
-rw-r--r--windows/Makefile2
-rw-r--r--windows/init.c10
-rw-r--r--windows/init.h1
-rw-r--r--windows/mpconfigport.h7
-rw-r--r--windows/msvc/gettimeofday.c58
-rw-r--r--windows/msvc/sources.props2
-rw-r--r--windows/msvc/sys/time.h28
-rw-r--r--windows/sleep.c34
8 files changed, 141 insertions, 1 deletions
diff --git a/windows/Makefile b/windows/Makefile
index aeb105634d..8fcc0b5815 100644
--- a/windows/Makefile
+++ b/windows/Makefile
@@ -31,8 +31,10 @@ SRC_C = \
unix/main.c \
unix/file.c \
unix/input.c \
+ unix/modtime.c \
realpath.c \
init.c \
+ sleep.c \
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
diff --git a/windows/init.c b/windows/init.c
index 5bb29b8f38..69f1026bb2 100644
--- a/windows/init.c
+++ b/windows/init.c
@@ -26,11 +26,21 @@
#include <stdlib.h>
#include <stdio.h>
+#include <Windows.h>
+
+HANDLE hSleepEvent = NULL;
void init() {
+ hSleepEvent = CreateEvent(NULL, TRUE, FALSE, FALSE);
#ifdef __MINGW32__
putenv("PRINTF_EXPONENT_DIGITS=2");
#else
_set_output_format(_TWO_DIGIT_EXPONENT);
#endif
}
+
+void deinit() {
+ if (hSleepEvent != NULL) {
+ CloseHandle(hSleepEvent);
+ }
+}
diff --git a/windows/init.h b/windows/init.h
index a5672840e5..69e577689e 100644
--- a/windows/init.h
+++ b/windows/init.h
@@ -25,3 +25,4 @@
*/
void init(void);
+void deinit(void);
diff --git a/windows/mpconfigport.h b/windows/mpconfigport.h
index 3651a6120f..0a1e313cb8 100644
--- a/windows/mpconfigport.h
+++ b/windows/mpconfigport.h
@@ -44,6 +44,7 @@
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_PORT_INIT_FUNC init()
+#define MICROPY_PORT_DEINIT_FUNC deinit()
// type definitions for the specific machine
@@ -69,9 +70,15 @@ extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
#define MICROPY_EXTRA_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
+extern const struct _mp_obj_module_t mp_module_time;
+#define MICROPY_EXTRA_BUILTIN_MODULES \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \
+
#include "realpath.h"
#include "init.h"
+// sleep for given number of milliseconds
+void msec_sleep(double msec);
// MSVC specifics
#ifdef _MSC_VER
diff --git a/windows/msvc/gettimeofday.c b/windows/msvc/gettimeofday.c
new file mode 100644
index 0000000000..363d59d7bc
--- /dev/null
+++ b/windows/msvc/gettimeofday.c
@@ -0,0 +1,58 @@
+/*
+* This file is part of the Micro Python project, http://micropython.org/
+*
+* The MIT License (MIT)
+*
+* Copyright (c) 2013, 2014 Damien P. George
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+#include <Winsock2.h>
+
+typedef union {
+ unsigned __int64 tm; // time in 100 nanoseconds interval
+ FILETIME ft;
+} FT;
+
+int gettimeofday(struct timeval *tp, struct timezone *tz) {
+ if (tp == NULL) {
+ return 0;
+ }
+
+ // UTC time
+ FT ft;
+ ZeroMemory(&ft, sizeof(ft));
+ GetSystemTimeAsFileTime(&ft.ft);
+
+ // to microseconds
+ ft.tm /= 10;
+
+ // convert to unix format
+ // number of microseconds intervals between the 1st january 1601 and the 1st january 1970 (369 years + 89 leap days)
+ const unsigned __int64 deltaEpoch = 11644473600000000ull;
+ const unsigned __int64 microSecondsToSeconds = 1000000ull;
+ tp->tv_usec = ft.tm % microSecondsToSeconds;
+ tp->tv_sec = (ft.tm - deltaEpoch) / microSecondsToSeconds;
+
+ // see man gettimeofday: timezone is deprecated and expected to be NULL
+ (void)tz;
+
+ return 0;
+}
diff --git a/windows/msvc/sources.props b/windows/msvc/sources.props
index 9af7d6e117..8af03e7563 100644
--- a/windows/msvc/sources.props
+++ b/windows/msvc/sources.props
@@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<ClCompile Include="$(PyBaseDir)py\*.c" />
- <ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\mod*.c" />
+ <ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c" />
<ClCompile Include="$(PyBaseDir)windows\*.c" />
<ClCompile Include="$(PyBaseDir)windows\msvc\*.c" />
</ItemGroup>
diff --git a/windows/msvc/sys/time.h b/windows/msvc/sys/time.h
new file mode 100644
index 0000000000..96bca1ccbe
--- /dev/null
+++ b/windows/msvc/sys/time.h
@@ -0,0 +1,28 @@
+/*
+* This file is part of the Micro Python project, http://micropython.org/
+*
+* The MIT License (MIT)
+*
+* Copyright (c) 2013, 2014 Damien P. George
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+// Get the definitions for timeval etc
+#include <Winsock2.h>
diff --git a/windows/sleep.c b/windows/sleep.c
new file mode 100644
index 0000000000..98387350f0
--- /dev/null
+++ b/windows/sleep.c
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <Windows.h>
+
+extern HANDLE hSleepEvent;
+
+void msec_sleep(double msec) {
+ ResetEvent(hSleepEvent);
+ WaitForSingleObjectEx(hSleepEvent, msec, FALSE);
+}