Merge pull request #2125 from erickt/master

fleshing out time.rs
This commit is contained in:
Brian Anderson 2012-04-04 11:49:14 -07:00
commit 3aed498842
6 changed files with 1256 additions and 27 deletions

View File

@ -108,7 +108,15 @@ fn chain_err<T: copy, U: copy, V: copy>(
}
}
impl methods<T:copy,E:copy> for result<T,E> {
impl extensions<T:copy, E:copy> for result<T,E> {
fn get() -> T { get(self) }
fn get_err() -> E { get_err(self) }
fn success() -> bool { success(self) }
fn failure() -> bool { failure(self) }
fn chain<U:copy>(op: fn(T) -> result<U,E>) -> result<U,E> {
chain(self, op)
}

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,8 @@
#include "rust_abi.h"
#include "rust_port.h"
#include <time.h>
#ifdef __APPLE__
#include <crt_externs.h>
#endif
@ -79,7 +81,7 @@ rust_getcwd() {
return NULL;
}
return make_str(task->kernel, cbuf, strlen(cbuf), "rust_str(getcwd");
return make_str(task->kernel, cbuf, strlen(cbuf), "rust_str(getcwd)");
}
#if defined(__WIN32__)
@ -408,7 +410,7 @@ rust_ptr_eq(type_desc *t, rust_box *a, rust_box *b) {
#if defined(__WIN32__)
extern "C" CDECL void
get_time(uint32_t *sec, uint32_t *usec) {
get_time(int64_t *sec, int32_t *nsec) {
FILETIME fileTime;
GetSystemTimeAsFileTime(&fileTime);
@ -423,15 +425,22 @@ get_time(uint32_t *sec, uint32_t *usec) {
const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000u;
uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
*sec = ns_since_1970 / 1000000;
*usec = ns_since_1970 % 1000000;
*nsec = (ns_since_1970 % 1000000) * 1000;
}
#else
extern "C" CDECL void
get_time(uint32_t *sec, uint32_t *usec) {
get_time(int64_t *sec, int32_t *nsec) {
#ifdef __APPLE__
struct timeval tv;
gettimeofday(&tv, NULL);
*sec = tv.tv_sec;
*usec = tv.tv_usec;
*nsec = tv.tv_usec * 1000;
#else
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
*sec = ts.tv_sec;
*nsec = ts.tv_nsec;
#endif
}
#endif
@ -441,6 +450,127 @@ precise_time_ns(uint64_t *ns) {
*ns = t.time_ns();
}
struct rust_tm {
int32_t tm_sec;
int32_t tm_min;
int32_t tm_hour;
int32_t tm_mday;
int32_t tm_mon;
int32_t tm_year;
int32_t tm_wday;
int32_t tm_yday;
int32_t tm_isdst;
int32_t tm_gmtoff;
rust_str *tm_zone;
int32_t tm_nsec;
};
void rust_tm_to_tm(rust_tm* in_tm, tm* out_tm) {
memset(out_tm, 0, sizeof(tm));
out_tm->tm_sec = in_tm->tm_sec;
out_tm->tm_min = in_tm->tm_min;
out_tm->tm_hour = in_tm->tm_hour;
out_tm->tm_mday = in_tm->tm_mday;
out_tm->tm_mon = in_tm->tm_mon;
out_tm->tm_year = in_tm->tm_year;
out_tm->tm_wday = in_tm->tm_wday;
out_tm->tm_yday = in_tm->tm_yday;
out_tm->tm_isdst = in_tm->tm_isdst;
}
void tm_to_rust_tm(tm* in_tm, rust_tm* out_tm, int32_t gmtoff,
const char *zone, int32_t nsec) {
out_tm->tm_sec = in_tm->tm_sec;
out_tm->tm_min = in_tm->tm_min;
out_tm->tm_hour = in_tm->tm_hour;
out_tm->tm_mday = in_tm->tm_mday;
out_tm->tm_mon = in_tm->tm_mon;
out_tm->tm_year = in_tm->tm_year;
out_tm->tm_wday = in_tm->tm_wday;
out_tm->tm_yday = in_tm->tm_yday;
out_tm->tm_isdst = in_tm->tm_isdst;
out_tm->tm_gmtoff = gmtoff;
out_tm->tm_nsec = nsec;
if (zone != NULL) {
size_t size = strlen(zone);
str_reserve_shared(&out_tm->tm_zone, size);
memcpy(out_tm->tm_zone->data, zone, size);
out_tm->tm_zone->fill = size + 1;
out_tm->tm_zone->data[size] = '\0';
}
}
#if defined(__WIN32__)
#define TZSET() _tzset()
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#define GMTIME(clock, result) gmtime_s((result), (clock))
#define LOCALTIME(clock, result) localtime_s((result), (clock))
#define TIMEGM(result) _mkgmtime64(result)
#else
struct tm* GMTIME(const time_t *clock, tm *result) {
struct tm* t = gmtime(clock);
if (t == NULL || result == NULL) { return NULL; }
*result = *t;
return result;
}
struct tm* LOCALTIME(const time_t *clock, tm *result) {
struct tm* t = localtime(clock);
if (t == NULL || result == NULL) { return NULL; }
*result = *t;
return result;
}
#define TIMEGM(result) mktime((result)) - _timezone
#endif
#else
#define TZSET() tzset()
#define GMTIME(clock, result) gmtime_r((clock), (result))
#define LOCALTIME(clock, result) localtime_r((clock), (result))
#define TIMEGM(result) timegm(result)
#endif
extern "C" CDECL void
rust_gmtime(int64_t *sec, int32_t *nsec, rust_tm *timeptr) {
tm tm;
time_t s = *sec;
GMTIME(&s, &tm);
tm_to_rust_tm(&tm, timeptr, 0, "UTC", *nsec);
}
extern "C" CDECL void
rust_localtime(int64_t *sec, int32_t *nsec, rust_tm *timeptr) {
tm tm;
TZSET();
time_t s = *sec;
LOCALTIME(&s, &tm);
#if defined(__WIN32__)
int32_t gmtoff = -timezone;
char zone[64];
strftime(zone, sizeof(zone), "%Z", &tm);
#else
int32_t gmtoff = tm.tm_gmtoff;
const char *zone = tm.tm_zone;
#endif
tm_to_rust_tm(&tm, timeptr, gmtoff, zone, *nsec);
}
extern "C" CDECL void
rust_timegm(rust_tm* timeptr, int64_t *out) {
tm t;
rust_tm_to_tm(timeptr, &t);
*out = TIMEGM(&t);
}
extern "C" CDECL void
rust_mktime(rust_tm* timeptr, int64_t *out) {
tm t;
rust_tm_to_tm(timeptr, &t);
*out = mktime(&t);
}
extern "C" CDECL rust_sched_id
rust_get_sched_id() {
rust_task *task = rust_get_current_task();

View File

@ -12,6 +12,10 @@ debug_abi_2
get_port_id
get_task_id
get_time
rust_gmtime
rust_localtime
rust_timegm
rust_mktime
last_os_error
new_port
new_task

View File

@ -5,7 +5,7 @@ import middle::ty;
import syntax::ast;
import syntax::ast::{ret_style};
import util::ppaux::{ty_to_str, mt_to_str};
import result::{result, methods, chain, chain_err, ok, err, map, map2, iter2};
import result::{result, extensions, ok, err, map, map2, iter2};
import ty::type_is_bot;
export infer_ctxt;
@ -85,7 +85,7 @@ fn fixup_vars(cx: infer_ctxt, a: ty::t) -> fres<ty::t> {
impl methods for ures {
fn then<T:copy>(f: fn() -> result<T,ty::type_err>)
-> result<T,ty::type_err> {
chain(self) {|_i| f() }
self.chain() {|_i| f() }
}
}

View File

@ -155,10 +155,10 @@ fn get_dest_addr(dest: dest) -> ValueRef {
}
}
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timeval,
end: time::timeval) {
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timespec,
end: time::timespec) {
let elapsed = 1000 * ((end.sec - start.sec) as int) +
((end.usec as int) - (start.usec as int)) / 1000;
((end.nsec as int) - (start.nsec as int)) / 1000000;
*ccx.stats.fn_times += [{ident: name, time: elapsed}];
}
@ -4056,7 +4056,7 @@ fn trans_fn(ccx: @crate_ctxt,
id: ast::node_id) {
let do_time = ccx.sess.opts.stats;
let start = if do_time { time::get_time() }
else { {sec: 0u32, usec: 0u32} };
else { {sec: 0i64, nsec: 0i32} };
let _icx = ccx.insn_ctxt("trans_fn");
trans_closure(ccx, path, decl, body, llfndecl, ty_self,
param_substs, id, {|fcx|