forked from ground/ground
Compare commits
9 Commits
c60e53a1a8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c5b0bd26f | |||
| be7db9d837 | |||
| 3bfea76f7d | |||
| a8097c9cf7 | |||
| f9c333a1bc | |||
| ccc8a61f66 | |||
| fd118369d3 | |||
| 2df8d2848d | |||
| fb6dd62a42 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ build
|
|||||||
ground
|
ground
|
||||||
groundc
|
groundc
|
||||||
.idea
|
.idea
|
||||||
|
Makefile
|
||||||
|
|||||||
422
libs/datetime/date_functions.c
Normal file
422
libs/datetime/date_functions.c
Normal file
@@ -0,0 +1,422 @@
|
|||||||
|
#include "date_functions.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
GroundValue tmToGroundValue(struct tm t) {
|
||||||
|
GroundStruct timeStruct = groundCreateStruct();
|
||||||
|
groundAddFieldToStruct(&timeStruct, "year", groundCreateValue(INT, t.tm_year + 1900));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "month", groundCreateValue(INT, t.tm_mon + 1));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "day", groundCreateValue(INT, t.tm_mday));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "hour", groundCreateValue(INT, t.tm_hour));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "minute", groundCreateValue(INT, t.tm_min));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "second", groundCreateValue(INT, t.tm_sec));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "weekDay", groundCreateValue(INT, t.tm_wday));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "yearDay", groundCreateValue(INT, t.tm_yday + 1));
|
||||||
|
groundAddFieldToStruct(&timeStruct, "isDaylightSavingsTime", groundCreateValue(BOOL, t.tm_isdst));
|
||||||
|
|
||||||
|
GroundValue value = groundCreateValue(CUSTOM, &timeStruct);
|
||||||
|
value.type = CUSTOM;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_Now(GroundScope* scope, List args) {
|
||||||
|
time_t now = time(NULL);
|
||||||
|
struct tm t = {0};
|
||||||
|
|
||||||
|
localtime_r(&now, &t);
|
||||||
|
|
||||||
|
// return a -datetime struct
|
||||||
|
return tmToGroundValue(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_FromEpochLocal(GroundScope* scope, List args) {
|
||||||
|
struct tm t = {0};
|
||||||
|
time_t ts = args.values[0].data.doubleVal;
|
||||||
|
|
||||||
|
localtime_r(&ts, &t);
|
||||||
|
|
||||||
|
return tmToGroundValue(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_FromEpochUTC(GroundScope* scope, List args) {
|
||||||
|
struct tm t = {0};
|
||||||
|
time_t ts = args.values[0].data.doubleVal;
|
||||||
|
|
||||||
|
gmtime_r(&ts, &t);
|
||||||
|
|
||||||
|
return tmToGroundValue(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_ToEpochLocal(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj = *args.values[0].data.customVal;
|
||||||
|
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
time_t ts = mktime(&t);
|
||||||
|
return groundCreateValue(INT, (long long)ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_ToEpochUTC(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj = *args.values[0].data.customVal;
|
||||||
|
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
time_t ts = timegm(&t);
|
||||||
|
return groundCreateValue(INT, (long long)ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue formatDatetimeObj(GroundObject obj, char* formatString) {
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
mktime(&t); // normalise time
|
||||||
|
|
||||||
|
// create a buffer for the string
|
||||||
|
char* buffer = (char*)malloc(128);
|
||||||
|
strftime(buffer, 128, formatString, &t);
|
||||||
|
|
||||||
|
// return ground version of string
|
||||||
|
return groundCreateValue(STRING, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_Format(GroundScope* scope, List args) {
|
||||||
|
return formatDatetimeObj(
|
||||||
|
*args.values[0].data.customVal,
|
||||||
|
args.values[1].data.stringVal
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_FromFormatted(GroundScope* scope, List args) {
|
||||||
|
char* timeString = args.values[0].data.stringVal;
|
||||||
|
char* formatString = args.values[1].data.stringVal;
|
||||||
|
|
||||||
|
struct tm t = {0};
|
||||||
|
t.tm_isdst = -1;
|
||||||
|
char* result = strptime(timeString, formatString, &t);
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
ERROR("Time string does not match format!", "ValueError");
|
||||||
|
}
|
||||||
|
|
||||||
|
mktime(&t);
|
||||||
|
|
||||||
|
return tmToGroundValue(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_ToISO8601UTC(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj = *args.values[0].data.customVal;
|
||||||
|
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
time_t ts = mktime(&t);
|
||||||
|
gmtime_r(&ts, &t);
|
||||||
|
|
||||||
|
char* buffer = (char*)malloc(128);
|
||||||
|
strftime(buffer, 128, "%Y-%m-%dT%H:%M:%SZ", &t);
|
||||||
|
|
||||||
|
return groundCreateValue(STRING, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_ToISO8601Local(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj = *args.values[0].data.customVal;
|
||||||
|
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
time_t ts = mktime(&t);
|
||||||
|
localtime_r(&ts, &t);
|
||||||
|
|
||||||
|
char* buffer = (char*)malloc(128);
|
||||||
|
strftime(buffer, 128, "%Y-%m-%dT%H:%M:%S%z", &t);
|
||||||
|
|
||||||
|
return groundCreateValue(STRING, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_Diff(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj1 = *args.values[0].data.customVal;
|
||||||
|
GroundObject obj2 = *args.values[1].data.customVal;
|
||||||
|
|
||||||
|
// check first timedate object
|
||||||
|
GroundObjectField* year = groundFindField(obj1, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object 1 does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj1, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object 1 does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj1, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object 1 does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj1, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object 1 does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj1, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object 1 does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj1, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object 1 does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj1, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object 1 does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj1, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object 1 does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj1, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object 1 does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t1 = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// check second timedate object
|
||||||
|
year = groundFindField(obj2, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
month = groundFindField(obj2, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
day = groundFindField(obj2, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
hour = groundFindField(obj2, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
minute = groundFindField(obj2, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
second = groundFindField(obj2, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
weekDay = groundFindField(obj2, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
yearDay = groundFindField(obj2, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
isDaylightSavingsTime = groundFindField(obj2, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t2 = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
time_t ts1 = mktime(&t1);
|
||||||
|
time_t ts2 = mktime(&t2);
|
||||||
|
|
||||||
|
return groundCreateValue(INT, ts2 - ts1);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue datetime_Add(GroundScope* scope, List args) {
|
||||||
|
GroundObject obj = *args.values[0].data.customVal;
|
||||||
|
|
||||||
|
long long secs = args.values[1].data.intVal;
|
||||||
|
long long mins = args.values[2].data.intVal;
|
||||||
|
long long hours = args.values[3].data.intVal;
|
||||||
|
long long days = args.values[4].data.intVal;
|
||||||
|
|
||||||
|
// check args
|
||||||
|
GroundObjectField* year = groundFindField(obj, "year");
|
||||||
|
if (year == NULL || year->value.type != INT) ERROR("Object does not have year field as int", "ValueError");
|
||||||
|
GroundObjectField* month = groundFindField(obj, "month");
|
||||||
|
if (month == NULL || month->value.type != INT) ERROR("Object does not have month field as int", "ValueError");
|
||||||
|
GroundObjectField* day = groundFindField(obj, "day");
|
||||||
|
if (day == NULL || day->value.type != INT) ERROR("Object does not have day field as int", "ValueError");
|
||||||
|
GroundObjectField* hour = groundFindField(obj, "hour");
|
||||||
|
if (hour == NULL || hour->value.type != INT) ERROR("Object does not have hour field as int", "ValueError");
|
||||||
|
GroundObjectField* minute = groundFindField(obj, "minute");
|
||||||
|
if (minute == NULL || minute->value.type != INT) ERROR("Object does not have minute field as int", "ValueError");
|
||||||
|
GroundObjectField* second = groundFindField(obj, "second");
|
||||||
|
if (second == NULL || second->value.type != INT) ERROR("Object does not have second field as int", "ValueError");
|
||||||
|
GroundObjectField* weekDay = groundFindField(obj, "weekDay");
|
||||||
|
if (weekDay == NULL || weekDay->value.type != INT) ERROR("Object does not have weekDay field as int", "ValueError");
|
||||||
|
GroundObjectField* yearDay = groundFindField(obj, "yearDay");
|
||||||
|
if (yearDay == NULL || yearDay->value.type != INT) ERROR("Object does not have yearDay field as int", "ValueError");
|
||||||
|
GroundObjectField* isDaylightSavingsTime = groundFindField(obj, "isDaylightSavingsTime");
|
||||||
|
if (isDaylightSavingsTime == NULL || isDaylightSavingsTime->value.type != BOOL) ERROR("Object does not have isDaylightSavingsTime field as bool", "ValueError");
|
||||||
|
|
||||||
|
// construct tm struct from our ground struct
|
||||||
|
struct tm t = {
|
||||||
|
.tm_sec = second->value.data.intVal,
|
||||||
|
.tm_min = minute->value.data.intVal,
|
||||||
|
.tm_hour = hour->value.data.intVal,
|
||||||
|
.tm_mday = day->value.data.intVal,
|
||||||
|
.tm_mon = month->value.data.intVal - 1,
|
||||||
|
.tm_year = year->value.data.intVal - 1900,
|
||||||
|
.tm_wday = 0,
|
||||||
|
.tm_yday = 0,
|
||||||
|
.tm_isdst = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
time_t base = mktime(&t);
|
||||||
|
|
||||||
|
long long totalSeconds =
|
||||||
|
secs +
|
||||||
|
mins * 60 +
|
||||||
|
hours * 3600 +
|
||||||
|
days * 86400;
|
||||||
|
|
||||||
|
base += totalSeconds;
|
||||||
|
|
||||||
|
struct tm newT;
|
||||||
|
localtime_r(&base, &newT);
|
||||||
|
|
||||||
|
return tmToGroundValue(newT);
|
||||||
|
|
||||||
|
}
|
||||||
18
libs/datetime/date_functions.h
Normal file
18
libs/datetime/date_functions.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
#define _XOPEN_SOURCE // to make gcc happy lol
|
||||||
|
|
||||||
|
#include <groundext.h>
|
||||||
|
#include <groundvm.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
GroundValue datetime_Now(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_Format(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_FromFormatted(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_ToISO8601UTC(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_ToISO8601Local(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_FromEpochLocal(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_FromEpochUTC(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_ToEpochLocal(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_ToEpochUTC(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_Diff(GroundScope* scope, List args);
|
||||||
|
GroundValue datetime_Add(GroundScope* scope, List args);
|
||||||
18
libs/datetime/datetime.c
Normal file
18
libs/datetime/datetime.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include "time_functions.h"
|
||||||
|
#include "date_functions.h"
|
||||||
|
|
||||||
|
|
||||||
|
void ground_init(GroundScope* scope) {
|
||||||
|
groundAddNativeFunction(scope, "datetime_NowEpoch", datetime_NowEpoch, DOUBLE, 0);
|
||||||
|
groundAddNativeFunction(scope, "datetime_Now", datetime_Now, CUSTOM, 0);
|
||||||
|
groundAddNativeFunction(scope, "datetime_Format", datetime_Format, STRING, 2, CUSTOM, "datetime", STRING, "format");
|
||||||
|
groundAddNativeFunction(scope, "datetime_FromFormatted", datetime_FromFormatted, CUSTOM, 2, STRING, "datetimeString", STRING, "format");
|
||||||
|
groundAddNativeFunction(scope, "datetime_ToISO8601UTC", datetime_ToISO8601UTC, STRING, 1, CUSTOM, "datetime");
|
||||||
|
groundAddNativeFunction(scope, "datetime_ToISO8601Local", datetime_ToISO8601Local, STRING, 1, CUSTOM, "datetime");
|
||||||
|
groundAddNativeFunction(scope, "datetime_FromEpochUTC", datetime_FromEpochUTC, CUSTOM, 1, DOUBLE, "epoch");
|
||||||
|
groundAddNativeFunction(scope, "datetime_FromEpochLocal", datetime_FromEpochLocal, CUSTOM, 1, DOUBLE, "epoch");
|
||||||
|
groundAddNativeFunction(scope, "datetime_ToEpochUTC", datetime_ToEpochUTC, INT, 1, CUSTOM, "datetime");
|
||||||
|
groundAddNativeFunction(scope, "datetime_ToEpochLocal", datetime_ToEpochLocal, INT, 1, CUSTOM, "datetime");
|
||||||
|
groundAddNativeFunction(scope, "datetime_Diff", datetime_Diff, INT, 2, CUSTOM, "datetime1", CUSTOM, "datetime2");
|
||||||
|
groundAddNativeFunction(scope, "datetime_Add", datetime_Add, CUSTOM, 5, CUSTOM, "datetime", INT, "seconds", INT, "minutes", INT, "hours", INT, "days");
|
||||||
|
}
|
||||||
12
libs/datetime/time_functions.c
Normal file
12
libs/datetime/time_functions.c
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "time_functions.h"
|
||||||
|
|
||||||
|
GroundValue datetime_NowEpoch(GroundScope* scope, List args) {
|
||||||
|
// grab time from system clock
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
|
|
||||||
|
// convert it to secs and return it
|
||||||
|
double secsSinceEpoch = (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
|
||||||
|
|
||||||
|
return groundCreateValue(DOUBLE, secsSinceEpoch);
|
||||||
|
}
|
||||||
5
libs/datetime/time_functions.h
Normal file
5
libs/datetime/time_functions.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <groundext.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
GroundValue datetime_NowEpoch(GroundScope* scope, List args);
|
||||||
File diff suppressed because one or more lines are too long
@@ -3,6 +3,7 @@
|
|||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "serialize.h"
|
#include "serialize.h"
|
||||||
|
#include "repl.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -43,9 +44,8 @@ char* getFileContents(const char* filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc < 2) {
|
if (argc == 1) {
|
||||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
exit(repl());
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool compile = false;
|
bool compile = false;
|
||||||
@@ -71,7 +71,7 @@ int main(int argc, char** argv) {
|
|||||||
printf(" -w <output> or --writebytecode <output>: Outputs binary Ground bytecode");
|
printf(" -w <output> or --writebytecode <output>: Outputs binary Ground bytecode");
|
||||||
printf(" -b <output> or --bytecode <output>: Inputs binary Ground bytecode");
|
printf(" -b <output> or --bytecode <output>: Inputs binary Ground bytecode");
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (strcmp("--writebytecode", argv[i]) == 0 || strcmp("-w", argv[i]) == 0) {
|
} else if (strcmp("--writeBytecode", argv[i]) == 0 || strcmp("-w", argv[i]) == 0) {
|
||||||
if (compile) {
|
if (compile) {
|
||||||
printf("Cannot choose both bytecode and compilation");
|
printf("Cannot choose both bytecode and compilation");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@@ -176,6 +176,11 @@ static GroundInstType getInstructionType(const char* inst) {
|
|||||||
if (strcmp(inst, "not") == 0) return NOT;
|
if (strcmp(inst, "not") == 0) return NOT;
|
||||||
if (strcmp(inst, "greater") == 0) return GREATER;
|
if (strcmp(inst, "greater") == 0) return GREATER;
|
||||||
if (strcmp(inst, "lesser") == 0) return LESSER;
|
if (strcmp(inst, "lesser") == 0) return LESSER;
|
||||||
|
if (strcmp(inst, "and") == 0) return AND;
|
||||||
|
if (strcmp(inst, "or") == 0) return OR;
|
||||||
|
if (strcmp(inst, "xor") == 0) return XOR;
|
||||||
|
if (strcmp(inst, "neg") == 0) return NEG;
|
||||||
|
if (strcmp(inst, "shift") == 0) return SHIFT;
|
||||||
if (strcmp(inst, "stoi") == 0) return STOI;
|
if (strcmp(inst, "stoi") == 0) return STOI;
|
||||||
if (strcmp(inst, "stod") == 0) return STOD;
|
if (strcmp(inst, "stod") == 0) return STOD;
|
||||||
if (strcmp(inst, "ctoi") == 0) return CTOI;
|
if (strcmp(inst, "ctoi") == 0) return CTOI;
|
||||||
@@ -194,6 +199,7 @@ static GroundInstType getInstructionType(const char* inst) {
|
|||||||
if (strcmp(inst, "use") == 0) return USE;
|
if (strcmp(inst, "use") == 0) return USE;
|
||||||
if (strcmp(inst, "extern") == 0) return EXTERN;
|
if (strcmp(inst, "extern") == 0) return EXTERN;
|
||||||
if (strcmp(inst, "drop") == 0) return DROP;
|
if (strcmp(inst, "drop") == 0) return DROP;
|
||||||
|
if (strcmp(inst, "license") == 0) return LICENSE;
|
||||||
if (strcmp(inst, "PAUSE") == 0) return PAUSE;
|
if (strcmp(inst, "PAUSE") == 0) return PAUSE;
|
||||||
|
|
||||||
fprintf(stderr, "Error: Unknown instruction: %s\n", inst);
|
fprintf(stderr, "Error: Unknown instruction: %s\n", inst);
|
||||||
|
|||||||
60
src/repl.c
Normal file
60
src/repl.c
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Ground REPL (shows when ground is ran without any arguments)
|
||||||
|
* Copyright (C) 2026 DiamondNether90
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "interpreter.h"
|
||||||
|
#include "include/estr.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
int repl() {
|
||||||
|
printf("Ground REPL\n");
|
||||||
|
printf("Copyright (C) DiamondNether90 2026\n");
|
||||||
|
printf("Distributed under the GNU GPL-3.0 license; type \"license\" for more info\n");
|
||||||
|
char input[BUFFER_SIZE];
|
||||||
|
|
||||||
|
GroundVariable* variables = NULL;
|
||||||
|
GroundLabel* labels = NULL;
|
||||||
|
|
||||||
|
GroundScope scope;
|
||||||
|
scope.variables = &variables;
|
||||||
|
scope.labels = &labels;
|
||||||
|
scope.isMainScope = true;
|
||||||
|
while (true) {
|
||||||
|
Estr programString = CREATE_ESTR("");
|
||||||
|
*scope.labels = NULL;
|
||||||
|
|
||||||
|
// Get program
|
||||||
|
printf(">>> ");
|
||||||
|
while (true) {
|
||||||
|
fgets(input, BUFFER_SIZE, stdin);
|
||||||
|
if (strcmp(input, "\n") == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
APPEND_ESTR(programString, input);
|
||||||
|
printf("...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interpret program
|
||||||
|
GroundProgram program = createGroundProgram();
|
||||||
|
program = parseFile(programString.str);
|
||||||
|
interpretGroundProgram(&program, &scope);
|
||||||
|
freeGroundProgram(&program);
|
||||||
|
|
||||||
|
DESTROY_ESTR(programString);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/repl.h
Normal file
1
src/repl.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
int repl();
|
||||||
21
src/types.c
21
src/types.c
@@ -87,11 +87,14 @@ GroundValue copyGroundValue(const GroundValue* gv) {
|
|||||||
case CHAR: newGv.data.charVal = gv->data.charVal; break;
|
case CHAR: newGv.data.charVal = gv->data.charVal; break;
|
||||||
case BOOL: newGv.data.boolVal = gv->data.boolVal; break;
|
case BOOL: newGv.data.boolVal = gv->data.boolVal; break;
|
||||||
case STRING:
|
case STRING:
|
||||||
|
/*
|
||||||
if (gv->data.stringVal != NULL) {
|
if (gv->data.stringVal != NULL) {
|
||||||
newGv.data.stringVal = strdup(gv->data.stringVal);
|
newGv.data.stringVal = strdup(gv->data.stringVal);
|
||||||
} else {
|
} else {
|
||||||
newGv.data.stringVal = NULL;
|
newGv.data.stringVal = NULL;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
newGv.data.stringVal = gv->data.stringVal;
|
||||||
break;
|
break;
|
||||||
case LIST: {
|
case LIST: {
|
||||||
List newList = createList();
|
List newList = createList();
|
||||||
@@ -258,7 +261,8 @@ void printGroundValue(GroundValue* gv) {
|
|||||||
|
|
||||||
void freeGroundValue(GroundValue* gv) {
|
void freeGroundValue(GroundValue* gv) {
|
||||||
if (gv->type == STRING && gv->data.stringVal != NULL) {
|
if (gv->type == STRING && gv->data.stringVal != NULL) {
|
||||||
free(gv->data.stringVal);
|
// leak some memory for now
|
||||||
|
// free(gv->data.stringVal);
|
||||||
gv->data.stringVal = NULL;
|
gv->data.stringVal = NULL;
|
||||||
}
|
}
|
||||||
if (gv->type == LIST && gv->data.listVal.values != NULL) {
|
if (gv->type == LIST && gv->data.listVal.values != NULL) {
|
||||||
@@ -482,6 +486,21 @@ void printGroundInstruction(GroundInstruction* gi) {
|
|||||||
case LESSER:
|
case LESSER:
|
||||||
printf("lesser");
|
printf("lesser");
|
||||||
break;
|
break;
|
||||||
|
case AND:
|
||||||
|
printf("and");
|
||||||
|
break;
|
||||||
|
case OR:
|
||||||
|
printf("or");
|
||||||
|
break;
|
||||||
|
case XOR:
|
||||||
|
printf("xor");
|
||||||
|
break;
|
||||||
|
case NEG:
|
||||||
|
printf("neg");
|
||||||
|
break;
|
||||||
|
case SHIFT:
|
||||||
|
printf("shift");
|
||||||
|
break;
|
||||||
case STOI:
|
case STOI:
|
||||||
printf("stoi");
|
printf("stoi");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ void wasm_print(const char* str);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum GroundInstType {
|
typedef enum GroundInstType {
|
||||||
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, AND, OR, XOR, NEG, SHIFT, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, LICENSE, ERRORCMD
|
||||||
} GroundInstType;
|
} GroundInstType;
|
||||||
|
|
||||||
typedef enum GroundValueType {
|
typedef enum GroundValueType {
|
||||||
|
|||||||
2
tests/end.grnd
Normal file
2
tests/end.grnd
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
set &x 10
|
||||||
|
end $x
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
[hello, there, general, kenobi][hello, there, general, kenobi][hello, there, general, hello]
|
|
||||||
hello
|
|
||||||
there
|
|
||||||
general
|
|
||||||
Nqb˜V
|
|
||||||
2
tests/pause.grnd
Normal file
2
tests/pause.grnd
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
set &x 5
|
||||||
|
PAUSE
|
||||||
@@ -8,14 +8,15 @@ for f in *.grnd; do
|
|||||||
[[ "$f" == "string.grnd" ]] ||
|
[[ "$f" == "string.grnd" ]] ||
|
||||||
[[ "$f" == "test.grnd" ]] ||
|
[[ "$f" == "test.grnd" ]] ||
|
||||||
[[ "$f" == "to1000.grnd" ]] ||
|
[[ "$f" == "to1000.grnd" ]] ||
|
||||||
[[ "$f" == "uhoh.grnd" ]];
|
[[ "$f" == "uhoh.grnd" ]] ||
|
||||||
|
[[ "$f" == "pause.grnd" ]];
|
||||||
then continue
|
then continue
|
||||||
fi
|
fi
|
||||||
echo "Running $f"
|
echo "Running $f"
|
||||||
ground "$f" > log.txt
|
ground "$f" > log.txt
|
||||||
|
|
||||||
FILE="log.txt"
|
FILE="log.txt"
|
||||||
FAILED="\033[31mFailed:\n\033[0m"
|
FAILED="\033[31mFailed\n\033[0m"
|
||||||
if [[ "$f" == "closure.grnd" ]]; then
|
if [[ "$f" == "closure.grnd" ]]; then
|
||||||
if !(cmp -s "$FILE" <(printf "13 10\n"));
|
if !(cmp -s "$FILE" <(printf "13 10\n"));
|
||||||
then printf $FAILED
|
then printf $FAILED
|
||||||
@@ -76,5 +77,6 @@ for f in *.grnd; do
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
rm log.txt
|
||||||
printf "\033[32mAll tests passed!\n\033[0m"
|
printf "\033[32mAll tests passed!\n\033[0m"
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
Reference in New Issue
Block a user