Skip to content

Commit 96c0d1c

Browse files
committed
MLC: Add "the local time" syntax.
Add a new "com.livecode.date" module with a single syntax definition: the local time This evaluates to a list containing the numerical components of the local time, except for the UTC offset. The precision of the time is in seconds. For example, if the current local time was: 2015-01-16 14:45:55 +0100 then "the local time" would evaluate to: [ 2015, 1, 16, 14, 45, 55 ] In the future, this syntax will return a date/time object that is UTC-offset-aware and that can be formatted, converted, compared, etc.
1 parent b9db761 commit 96c0d1c

6 files changed

Lines changed: 124 additions & 0 deletions

File tree

libscript/Makefile.libscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SOURCES += \
1212
module-bitwise.cpp \
1313
module-byte.cpp \
1414
module-char.cpp \
15+
module-date.cpp \
1516
module-encoding.cpp \
1617
module-file.cpp \
1718
module-list.cpp \

libscript/libstdscript-modules.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ binary.mlc
66
bitwise.mlc
77
byte.mlc
88
char.mlc
9+
date.mlc
910
file.mlc
1011
list.mlc
1112
logic.mlc

libscript/src/date.mlc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright (C) 2015 Runtime Revolution Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
/*
19+
This library provides low-level system functionality for modular
20+
LiveCode programs.
21+
*/
22+
23+
module com.livecode.date
24+
25+
public foreign handler MCDateExecGetLocalTime(out DateTime as list) as undefined binds to "<builtin>"
26+
27+
/*
28+
Summary: The local time
29+
30+
Example:
31+
variable tDateTime as list
32+
put the local time into tDateTime
33+
34+
variable tDayOfMonth as number
35+
put tDateTime[3] into tDayOfMonth
36+
37+
Description:
38+
Returns the local time as a list of six numeric components. The
39+
elements of the list are:
40+
41+
* The year
42+
* The month (1-12)
43+
* The day of the month (1-31, depending on the month)
44+
* The hour (0-23)
45+
* The minute (0-59)
46+
* The second (0-59)
47+
48+
Tags: Date and time
49+
*/
50+
syntax LocalTime is expression
51+
"the" "local" "time"
52+
begin
53+
MCDateExecGetLocalTime(output)
54+
end syntax
55+
56+
--
57+
58+
end module

libscript/src/module-date.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* -*-c++-*-
2+
Copyright (C) 2015 Runtime Revolution Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
#include <foundation.h>
19+
#include <foundation-auto.h>
20+
21+
#include <time.h>
22+
23+
/* Windows doesn't have localtime_r(), but it does have an equivalent
24+
* function with the arguments in the opposite order! */
25+
#if defined(__WINDOWS__)
26+
# define localtime_r(s,t) (_localtime_s(t,s))
27+
#endif
28+
29+
extern "C" MC_DLLEXPORT void
30+
MCDateExecGetLocalTime (MCProperListRef & r_datetime)
31+
{
32+
struct tm t_timeinfo;
33+
time_t t_now;
34+
35+
time (&t_now);
36+
if (NULL == localtime_r (&t_now, &t_timeinfo))
37+
return;
38+
39+
MCAutoNumberRef t_year, t_month, t_day, t_hour, t_minute, t_second;
40+
if (!(MCNumberCreateWithInteger (t_timeinfo.tm_year, &t_year) &&
41+
MCNumberCreateWithInteger (t_timeinfo.tm_mon, &t_month) &&
42+
MCNumberCreateWithInteger (t_timeinfo.tm_mday, &t_day) &&
43+
MCNumberCreateWithInteger (t_timeinfo.tm_hour, &t_hour) &&
44+
MCNumberCreateWithInteger (t_timeinfo.tm_min, &t_minute) &&
45+
MCNumberCreateWithInteger (t_timeinfo.tm_sec, &t_second)))
46+
return;
47+
48+
const MCValueRef t_elements[] = {*t_year, *t_month, *t_day,
49+
*t_hour, *t_minute, *t_second};
50+
51+
/* UNCHECKED */ MCProperListCreate (t_elements, 6, r_datetime);
52+
}

toolchain/lc-compile/src/module-helper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern builtin_module_descriptor __com_livecode_binary_module_info;
3030
extern builtin_module_descriptor __com_livecode_bitwise_module_info;
3131
extern builtin_module_descriptor __com_livecode_byte_module_info;
3232
extern builtin_module_descriptor __com_livecode_char_module_info;
33+
extern builtin_module_descriptor __com_livecode_date_module_info;
3334
extern builtin_module_descriptor __com_livecode_encoding_module_info;
3435
extern builtin_module_descriptor __com_livecode_file_module_info;
3536
extern builtin_module_descriptor __com_livecode_item_module_info;
@@ -54,6 +55,7 @@ builtin_module_descriptor* g_builtin_modules[] =
5455
&__com_livecode_bitwise_module_info,
5556
&__com_livecode_byte_module_info,
5657
&__com_livecode_char_module_info,
58+
&__com_livecode_date_module_info,
5759
//&__com_livecode_encoding_module_info,
5860
&__com_livecode_file_module_info,
5961
//&__com_livecode_item_module_info,
@@ -79,6 +81,7 @@ extern void (*MCBinaryEvalConcatenateBytes)();
7981
extern void (*MCBitwiseEvalBitwiseAnd)();
8082
extern void (*MCByteEvalNumberOfBytesIn)();
8183
extern void (*MCCharEvalNumberOfCharsIn)();
84+
extern void (*MCDateExecGetLocalTime)();
8285
extern void (*MCFileExecGetContents)();
8386
extern void (*MCListEvalHeadOf)();
8487
extern void (*MCLogicEvalNot)();
@@ -100,6 +103,7 @@ void *g_builtin_ptrs[] =
100103
&MCBitwiseEvalBitwiseAnd,
101104
&MCByteEvalNumberOfBytesIn,
102105
&MCCharEvalNumberOfCharsIn,
106+
&MCDateExecGetLocalTime,
103107
&MCFileExecGetContents,
104108
&MCListEvalHeadOf,
105109
&MCLogicEvalNot,

toolchain/lc-compile/test.mlc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use com.livecode.math
44
use com.livecode.file
55
use com.livecode.stream
66
use com.livecode.system
7+
use com.livecode.date
78

89
public handler test()
910
variable tResults as list
@@ -72,6 +73,9 @@ public handler test()
7273

7374
--com.livecode.system
7475
testSystem(tResults)
76+
77+
--com.livecode.date
78+
testDate(tResults)
7579

7680
--com.livecode.binary
7781
--com.livecode.byte
@@ -931,4 +935,8 @@ public handler testSystem(inout xResults as list)
931935
testLog("System", "OperatingSystemNonEmpty", the operating system is not empty, xResults)
932936
end handler
933937

938+
public handler testDate(inout xResults as list)
939+
testLog("Date", "LocalTimeListLength", the number of elements in the local time is 6, xResults)
940+
end handler
941+
934942
end module

0 commit comments

Comments
 (0)