Skip to content

Initial implementation of the time module #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <limits.h>
#include <ctype.h>

#if defined(_MSC_VER)
# include <winsock2.h>
#endif

#include <libasr/runtime/lfortran_intrinsics.h>
#include <libasr/config.h>

Expand Down Expand Up @@ -1187,6 +1191,22 @@ LFORTRAN_API void _lfortran_i64sys_clock(
#endif
}

LFORTRAN_API double _lfortran_time()
{
#if defined(_MSC_VER)
FILETIME ft;
ULARGE_INTEGER uli;
GetSystemTimeAsFileTime(&ft);
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return (double)uli.QuadPart / 10000000.0 - 11644473600.0;
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
#endif
}

LFORTRAN_API void _lfortran_sp_rand_num(float *x) {
srand(time(0));
*x = rand() / (float) RAND_MAX;
Expand Down
1 change: 1 addition & 0 deletions src/libasr/runtime/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ LFORTRAN_API void _lfortran_i32sys_clock(
int32_t *count, int32_t *rate, int32_t *max);
LFORTRAN_API void _lfortran_i64sys_clock(
uint64_t *count, int64_t *rate, int64_t *max);
LFORTRAN_API double _lfortran_time();
LFORTRAN_API void _lfortran_sp_rand_num(float *x);
LFORTRAN_API void _lfortran_dp_rand_num(double *x);
LFORTRAN_API int64_t _lpython_open(char *path, char *flags);
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import f64, ccall

def time() -> f64:
return _lfortran_time()

@ccall
def _lfortran_time() -> f64:
pass