mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
time.c: added in: option to Time.now
* time.c (time_s_now): added in: option to Time.now as well as Time.at. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67613 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fb042e0f0c
commit
4dd1cf3b27
2 changed files with 33 additions and 9 deletions
|
@ -582,6 +582,11 @@ module TestTimeTZ::WithTZ
|
||||||
assert_equal(time_class.utc(2018, 9, 1, 12+h, m, 0).to_i, t.to_i)
|
assert_equal(time_class.utc(2018, 9, 1, 12+h, m, 0).to_i, t.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def subtest_now(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
||||||
|
t = time_class.now(in: tzarg)
|
||||||
|
assert_equal(tz, t.zone)
|
||||||
|
end
|
||||||
|
|
||||||
def subtest_getlocal(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
def subtest_getlocal(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
||||||
t = time_class.utc(2018, 9, 1, 12, 0, 0).getlocal(tzarg)
|
t = time_class.utc(2018, 9, 1, 12, 0, 0).getlocal(tzarg)
|
||||||
h, m = (utc_offset / 60).divmod(60)
|
h, m = (utc_offset / 60).divmod(60)
|
||||||
|
|
37
time.c
37
time.c
|
@ -2671,6 +2671,22 @@ rb_time_timespec(VALUE time)
|
||||||
return time_timespec(time, FALSE);
|
return time_timespec(time, FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum {
|
||||||
|
TMOPT_IN,
|
||||||
|
TMOPT_MAX_
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool
|
||||||
|
get_tmopt(VALUE opts, VALUE vals[TMOPT_MAX_])
|
||||||
|
{
|
||||||
|
ID ids[TMOPT_MAX_];
|
||||||
|
|
||||||
|
if (NIL_P(opts)) return false;
|
||||||
|
CONST_ID(ids[TMOPT_IN], "in");
|
||||||
|
rb_get_kwargs(opts, ids, 0, TMOPT_MAX_, vals);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* Time.now -> time
|
* Time.now -> time
|
||||||
|
@ -2682,9 +2698,16 @@ rb_time_timespec(VALUE time)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
time_s_now(VALUE klass)
|
time_s_now(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
return rb_class_new_instance(0, NULL, klass);
|
VALUE vals[TMOPT_MAX_], opts, t, zone = Qundef;
|
||||||
|
rb_scan_args(argc, argv, ":", &opts);
|
||||||
|
if (get_tmopt(opts, vals)) zone = vals[TMOPT_IN];
|
||||||
|
t = rb_class_new_instance(0, NULL, klass);
|
||||||
|
if (zone != Qundef) {
|
||||||
|
time_zonelocal(t, zone);
|
||||||
|
}
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -2746,15 +2769,11 @@ static VALUE
|
||||||
time_s_at(int argc, VALUE *argv, VALUE klass)
|
time_s_at(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
VALUE time, t, unit = Qundef, zone = Qundef, opts;
|
VALUE time, t, unit = Qundef, zone = Qundef, opts;
|
||||||
|
VALUE vals[TMOPT_MAX_];
|
||||||
wideval_t timew;
|
wideval_t timew;
|
||||||
|
|
||||||
argc = rb_scan_args(argc, argv, "12:", &time, &t, &unit, &opts);
|
argc = rb_scan_args(argc, argv, "12:", &time, &t, &unit, &opts);
|
||||||
if (!NIL_P(opts)) {
|
if (get_tmopt(opts, vals)) {
|
||||||
ID ids[1];
|
|
||||||
VALUE vals[numberof(ids)];
|
|
||||||
|
|
||||||
CONST_ID(ids[0], "in");
|
|
||||||
rb_get_kwargs(opts, ids, 0, 1, vals);
|
|
||||||
zone = vals[0];
|
zone = vals[0];
|
||||||
}
|
}
|
||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
|
@ -5577,7 +5596,7 @@ Init_Time(void)
|
||||||
rb_include_module(rb_cTime, rb_mComparable);
|
rb_include_module(rb_cTime, rb_mComparable);
|
||||||
|
|
||||||
rb_define_alloc_func(rb_cTime, time_s_alloc);
|
rb_define_alloc_func(rb_cTime, time_s_alloc);
|
||||||
rb_define_singleton_method(rb_cTime, "now", time_s_now, 0);
|
rb_define_singleton_method(rb_cTime, "now", time_s_now, -1);
|
||||||
rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
|
rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
|
||||||
rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
|
rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
|
||||||
rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
|
rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
|
||||||
|
|
Loading…
Reference in a new issue