summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-10 02:59:47 (GMT)
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-10 02:59:47 (GMT)
commitf1df08e76deadd8a22dc53b86d68cad5f6c11c22 (patch)
tree735c52962da45e645b91a4432b306c527c1af803 /time.c
parent1ed3287af18b47e541b93116f24c08284d79e9e0 (diff)
* time.c (rb_timespec_now): added.
* time.c (rb_time_timespec_new): added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52514 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r--time.c61
1 files changed, 48 insertions, 13 deletions
diff --git a/time.c b/time.c
index 11331a3..aad4cb4 100644
--- a/time.c
+++ b/time.c
@@ -1892,19 +1892,11 @@ timew2timespec_exact(wideval_t timew, struct timespec *ts)
return ts;
}
-static VALUE
-time_init_0(VALUE time)
+void
+rb_timespec_now(struct timespec *ts)
{
- struct time_object *tobj;
- struct timespec ts;
-
- time_modify(time);
- GetNewTimeval(time, tobj);
- tobj->gmt = 0;
- tobj->tm_got=0;
- tobj->timew = WINT2FIXWV(0);
#ifdef HAVE_CLOCK_GETTIME
- if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
+ if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
rb_sys_fail("clock_gettime");
}
#else
@@ -1913,10 +1905,24 @@ time_init_0(VALUE time)
if (gettimeofday(&tv, 0) < 0) {
rb_sys_fail("gettimeofday");
}
- ts.tv_sec = tv.tv_sec;
- ts.tv_nsec = tv.tv_usec * 1000;
+ ts->tv_sec = tv.tv_sec;
+ ts->tv_nsec = tv.tv_usec * 1000;
}
#endif
+}
+
+static VALUE
+time_init_0(VALUE time)
+{
+ struct time_object *tobj;
+ struct timespec ts;
+
+ time_modify(time);
+ GetNewTimeval(time, tobj);
+ tobj->gmt = 0;
+ tobj->tm_got=0;
+ tobj->timew = WINT2FIXWV(0);
+ rb_timespec_now(&ts);
tobj->timew = timespec2timew(&ts);
return time;
@@ -2299,12 +2305,41 @@ rb_time_new(time_t sec, long usec)
return time_new_timew(rb_cTime, timew);
}
+/* returns localtime time object */
VALUE
rb_time_nano_new(time_t sec, long nsec)
{
return time_new_timew(rb_cTime, nsec2timew(sec, nsec));
}
+/**
+ * Returns a time object with UTC/localtime/fixed offset
+ *
+ * offset is -86400 < fixoff < 86400 or INT_MAX (UTC) or INT_MAX-1 (localtime)
+ */
+VALUE
+rb_time_timespec_new(const struct timespec *ts, int offset)
+{
+ struct time_object *tobj;
+ VALUE time = time_new_timew(rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
+
+ if (-86400 < offset && offset < 86400) { /* fixoff */
+ GetTimeval(time, tobj);
+ TIME_SET_FIXOFF(tobj, INT2FIX(offset));
+ }
+ else if (offset == INT_MAX) { /* UTC */
+ GetTimeval(time, tobj);
+ TIME_SET_UTC(tobj);
+ }
+ else if (offset == INT_MAX-1) { /* localtime */
+ }
+ else {
+ rb_raise(rb_eArgError, "utc_offset out of range");
+ }
+
+ return time;
+}
+
VALUE
rb_time_num_new(VALUE timev, VALUE off)
{