forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-date.cpp
More file actions
executable file
·83 lines (66 loc) · 2.5 KB
/
module-date.cpp
File metadata and controls
executable file
·83 lines (66 loc) · 2.5 KB
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
/* -*-c++-*-
Copyright (C) 2015 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include <time.h>
#ifndef _WINDOWS
# include <sys/time.h>
#else
# include <windows.h>
#endif
/* Windows doesn't have localtime_r(), but it does have an equivalent
* function with the arguments in the opposite order! */
#if defined(__WINDOWS__)
# define localtime_r(s,t) (localtime_s(t,s))
#endif
extern "C" MC_DLLEXPORT void
MCDateExecGetLocalDate (MCProperListRef & r_datetime)
{
struct tm t_timeinfo;
time_t t_now;
time (&t_now);
if (NULL == localtime_r (&t_now, &t_timeinfo))
return;
MCAutoNumberRef t_year, t_month, t_day, t_hour, t_minute, t_second;
if (!(MCNumberCreateWithInteger (t_timeinfo.tm_year, &t_year) &&
MCNumberCreateWithInteger (t_timeinfo.tm_mon, &t_month) &&
MCNumberCreateWithInteger (t_timeinfo.tm_mday, &t_day) &&
MCNumberCreateWithInteger (t_timeinfo.tm_hour, &t_hour) &&
MCNumberCreateWithInteger (t_timeinfo.tm_min, &t_minute) &&
MCNumberCreateWithInteger (t_timeinfo.tm_sec, &t_second)))
return;
const MCValueRef t_elements[] = {*t_year, *t_month, *t_day,
*t_hour, *t_minute, *t_second};
if (!MCProperListCreate (t_elements, 6, r_datetime))
return;
}
extern "C" MC_DLLEXPORT void
MCDateExecGetUniversalTime (double& r_time)
{
#ifndef _WINDOWS
struct timeval tv;
gettimeofday(&tv, NULL);
r_time = tv.tv_sec + (double)tv.tv_usec / 1000000.0;
#else
SYSTEMTIME t_localtime;
FILETIME t_filetime;
GetLocalTime(&t_localtime);
SystemTimeToFileTime(&t_localtime, &t_filetime);
// The Win32 filetime counts 100ns intervals since 1st Jan 1601
uint64_t t_time_win32;
double t_time_unix;
t_time_win32 = (uint64_t(t_filetime.dwHighDateTime) << 32) | t_filetime.dwLowDateTime;
t_time_unix = (double(t_time_win32) / 10000000.0) - 11644473600.0;
r_time = t_time_unix;
#endif
}