get-universal-time-ms added

This commit is contained in:
vaclavt 2022-05-27 17:34:57 +02:00
parent 8e142dc7ea
commit fb552633c2
5 changed files with 32 additions and 5 deletions

View File

@ -77,7 +77,6 @@ utils/local_install.sh
- multiline editing (see kilocpp editor)
#### Doc
- add doc for macros
- add functions from stdlib into doc
- doc for set!

View File

@ -2,8 +2,8 @@
|option|Description|
|:-|-|
|-h|Prints help|
|-b|Skips loadin of std lib|
|-c code|Runs given code|
|-b|Skips loading of std lib|
|-c code|Runs passed code|
|-f source_file ..|Executes code in files|
|-run source_file ..|Executes code in file, if first line of file is sheebang, it is skipped|
|-i|Runs REPL|
@ -23,7 +23,7 @@
## Syntax and Special Forms
|Special Form|Argument Evaluations|Purpose|Section|
|:-|-|-|-|
|`(if cond a b)`|`if` only evaluates its `cond` argument. If `cond` is truthy (non-zero), then `a` is evaluated. Otherwise, `b` is evaluated.|This special form is the main method of control flow.|Language|
|`(if cond a b)`|`if` only evaluates its `cond` argument. If `cond` is truthy, then `a` is evaluated. Otherwise, `b` is evaluated.|This special form is the main method of control flow.|Language|
|`(cond (test1 action1 ..) (test2 action2 ..) ... (testn ..))`|The first clause whose test evaluates to non-nil is selected; all other clauses are ignored, and the consequents of the selected clause are evaluated in order. If none of the test conditions are evaluated to be true, then the cond statement returns nil.|This special form is method of control flow.|Language|
|`(do a b c ...)`|`do` takes a list of s-expressions and evaluates them in the order they were given (in the current scope), and then returns the result of the last s-expression.|This special form allows lambda functions to have multi-step bodies.|Language|
|`(scope a b c ...)`|`scope` takes a list of s-expressions and evaluates them in the order they were given _in a new scope_, and then returns the result of the last s-expression.|This special form allows the user to evaluate blocks of code in new scopes.|Language|
@ -39,6 +39,13 @@
|`(benchmark msg_string ...)`|`benchmark` takes a list of s-expressions and evaluates them in the order they were given (in the current scope), and then returns the result of the last s-expression and prints msg_string and timing info.||Language|
|`(set! x)`|`set!` ...|....|Language|
## Macros
|Signature|Description|Return values|Section|
|:-|-|-|-|
|`(unles cond body)`|`unless` only evaluates its `cond` argument. If `cond` is not truthy, then `body` is evaluated.|`>>> (unless #t (print "it is #f")) => nil`|Language|
|`(dotimes v n body)`|Iterates over `v` from 0 below `n` and evaluated `body`. Returns the last value of v|`>>> (dotimes i 5 i) => 4`|Language|
|`(until cond body)`|Evaluated `body` as long as `cond` is not truthy|`>>> (def i 0)(until (> i 3) (set! i (inc i))) => 4`|Language|
## Library
|Signature|Description|Return values|Section|
|:-|-|-|-|
@ -101,7 +108,8 @@
|`(parse-csv string)`|Parse CSV string|`>>> (parse-csv "A,B\n1,1\n2,2") => ((1 1) (2 2))`|String manipulation|
|`(parse-json json_string)`|Parse JSON string|`>>> (parse-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}") => (("k1" "v1") ("k2" 42) ("k3" ("a" 123 #t nil nil)))`|String manipulation|
|`(make-csv ..)`|Returns list as csv string|`>>> (make-csv '(("A" "B")(1 1)(2 2))) => "A,B\n1,1\n2,2"`|String manipulation|
|`(get-universal-time)`|Get current time as secs from epoch|`>>> (get-universal-time) => 1642152733`|Date and time|
|`(get-universal-time)`|Get current time as number of seconds from epoch|`>>> (get-universal-time) => 1642152733`|Date and time|
|`(get-universal-time-ms)`|Get current time as number of miliseconds from epoch|`>>> (get-universal-time-ms) => 1653575251097`|Date and time|
|`(get-localtime-offset)`|Offset in seconds between local time and gmt time|`>>> (get-localtime-offset) => 3600`|Date and time|
|`(date-to-str date format)`|Converts date to formated string. Format is strftime format (https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm)|`>>> (date-to-str (get-universal-time) "%Y-%m-%d %H:%M:%S") => "2021-03-13 19:53:01"`|Date and time|
|`(str-to-date string format)`|Converst string to time of secs since epoch. |`>>> (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") => 1615665181`|Date and time|

11
ml.cpp
View File

@ -1298,6 +1298,16 @@ MlValue get_universal_time(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue(now());
}
// Get current time as miliseconds from epoch
MlValue get_universal_time_ms(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (!args.empty())
throw MlError(MlValue("get-universal-time-ms", get_universal_time_ms), env, TOO_MANY_ARGS);
return MlValue(now_ms());
}
// Get offsets in secs between local timezone and gmt
MlValue get_localtime_offset(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@ -2294,6 +2304,7 @@ std::map<const std::string, Builtin> builtin_funcs
// Datetime operations
std::make_pair("get-universal-time", builtin::get_universal_time),
std::make_pair("get-universal-time-ms", builtin::get_universal_time_ms),
std::make_pair("get-localtime-offset", builtin::get_localtime_offset),
std::make_pair("date-to-str", builtin::date_to_str),
std::make_pair("str-to-date", builtin::str_to_date),

View File

@ -2,6 +2,7 @@
#include "ml_date.h"
#include <cstring>
#include <stdexcept>
#include <chrono>
long now() { // get-universal-time
time_t t = std::time(nullptr);
@ -10,6 +11,13 @@ long now() { // get-universal-time
return now;
}
long now_ms() { // get-universal-time-ms
using namespace std::chrono;
milliseconds ms = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
return ms.count();
}
long get_gmt_localtime_offset() {
std::time_t current_time;
std::time(&current_time);

View File

@ -6,6 +6,7 @@
long now();
long now_ms();
long get_gmt_localtime_offset();