Class: Time

Inherits:
Object show all
Defined in:
mrbgems/mruby-time/src/time.c,
mrbgems/mruby-time/mrblib/time.rb

Overview

ISO 15.2.19.2

Instance Method Summary collapse

Constructor Details

#initializeObject

Initializes a time by setting the amount of milliseconds since the epoch.


651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'mrbgems/mruby-time/src/time.c', line 651

static mrb_value
mrb_time_initialize(mrb_state *mrb, mrb_value self)
{
  mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0,
  amin = 0, asec = 0, ausec = 0;
  mrb_int n;
  struct mrb_time *tm;

  n = mrb_get_args(mrb, "|iiiiiii",
       &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
  tm = (struct mrb_time*)DATA_PTR(self);
  if (tm) {
    mrb_free(mrb, tm);
  }
  mrb_data_init(self, NULL, &mrb_time_type);

  if (n == 0) {
    tm = current_mrb_time(mrb);
  }
  else {
    tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
  }
  mrb_data_init(self, tm, &mrb_time_type);
  return self;
}

Instance Method Details

#+Object

15.2.19.7.2


474
475
476
477
478
479
480
481
482
483
# File 'mrbgems/mruby-time/src/time.c', line 474

static mrb_value
mrb_time_plus(mrb_state *mrb, mrb_value self)
{
  mrb_float f;
  struct mrb_time *tm;

  mrb_get_args(mrb, "f", &f);
  tm = time_get_ptr(mrb, self);
  return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, (double)tm->usec, tm->timezone);
}

#-Object

15.2.19.7.3


485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'mrbgems/mruby-time/src/time.c', line 485

static mrb_value
mrb_time_minus(mrb_state *mrb, mrb_value self)
{
  mrb_float f;
  mrb_value other;
  struct mrb_time *tm, *tm2;

  mrb_get_args(mrb, "o", &other);
  tm = time_get_ptr(mrb, self);
  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
  if (tm2) {
    f = (mrb_float)(tm->sec - tm2->sec)
      + (mrb_float)(tm->usec - tm2->usec) / 1.0e6;
    return mrb_float_value(mrb, f);
  }
  else {
    mrb_get_args(mrb, "f", &f);
    return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, (double)tm->usec, tm->timezone);
  }
}

#<=>Object

15.2.19.7.1


448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'mrbgems/mruby-time/src/time.c', line 448

static mrb_value
mrb_time_cmp(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  struct mrb_time *tm1, *tm2;

  mrb_get_args(mrb, "o", &other);
  tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
  if (!tm1 || !tm2) return mrb_nil_value();
  if (tm1->sec > tm2->sec) {
    return mrb_fixnum_value(1);
  }
  else if (tm1->sec < tm2->sec) {
    return mrb_fixnum_value(-1);
  }
  /* tm1->sec == tm2->sec */
  if (tm1->usec > tm2->usec) {
    return mrb_fixnum_value(1);
  }
  else if (tm1->usec < tm2->usec) {
    return mrb_fixnum_value(-1);
  }
  return mrb_fixnum_value(0);
}

#==Object

15.2.19.6.6


433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'mrbgems/mruby-time/src/time.c', line 433

static mrb_value
mrb_time_eq(mrb_state *mrb, mrb_value self)
{
  mrb_value other;
  struct mrb_time *tm1, *tm2;
  mrb_bool eq_p;

  mrb_get_args(mrb, "o", &other);
  tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
  eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec;

  return mrb_bool_value(eq_p);
}

#asctimeObject

Returns a string that describes the time.


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'mrbgems/mruby-time/src/time.c', line 556

static mrb_value
mrb_time_asctime(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm = time_get_ptr(mrb, self);
  struct tm *d = &tm->datetime;
  int len;

#if defined(MRB_DISABLE_STDIO)
  char *s;
# ifdef NO_ASCTIME_R
  s = asctime(d);
# else
  char buf[32];
  s = asctime_r(d, buf);
# endif
  len = strlen(s)-1;            /* truncate the last newline */
#else
  char buf[256];

  len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#ctimeObject

Returns a string that describes the time.


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'mrbgems/mruby-time/src/time.c', line 556

static mrb_value
mrb_time_asctime(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm = time_get_ptr(mrb, self);
  struct tm *d = &tm->datetime;
  int len;

#if defined(MRB_DISABLE_STDIO)
  char *s;
# ifdef NO_ASCTIME_R
  s = asctime(d);
# else
  char buf[32];
  s = asctime_r(d, buf);
# endif
  len = strlen(s)-1;            /* truncate the last newline */
#else
  char buf[256];

  len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#dayObject

Returns the day in the month of the time.


586
587
588
589
590
591
592
593
# File 'mrbgems/mruby-time/src/time.c', line 586

static mrb_value
mrb_time_day(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_mday);
}

#dst?Boolean

Returns true if daylight saving was applied for this time.

Returns:

  • (Boolean)

598
599
600
601
602
603
604
605
# File 'mrbgems/mruby-time/src/time.c', line 598

static mrb_value
mrb_time_dst_p(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_bool_value(tm->datetime.tm_isdst);
}

#friday?Boolean

Returns:

  • (Boolean)

7
# File 'mrbgems/mruby-time/mrblib/time.rb', line 7

def friday?;  wday == 5 end

#getgmObject

Returns the Time object of the UTC(GMT) timezone.


610
611
612
613
614
615
616
617
618
619
620
621
# File 'mrbgems/mruby-time/src/time.c', line 610

static mrb_value
mrb_time_getutc(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm, *tm2;

  tm = time_get_ptr(mrb, self);
  tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
  *tm2 = *tm;
  tm2->timezone = MRB_TIMEZONE_UTC;
  time_update_datetime(mrb, tm2);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

#getlocalObject

Returns the Time object of the LOCAL timezone.


625
626
627
628
629
630
631
632
633
634
635
636
# File 'mrbgems/mruby-time/src/time.c', line 625

static mrb_value
mrb_time_getlocal(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm, *tm2;

  tm = time_get_ptr(mrb, self);
  tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
  *tm2 = *tm;
  tm2->timezone = MRB_TIMEZONE_LOCAL;
  time_update_datetime(mrb, tm2);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

#getutcObject

Returns the Time object of the UTC(GMT) timezone.


610
611
612
613
614
615
616
617
618
619
620
621
# File 'mrbgems/mruby-time/src/time.c', line 610

static mrb_value
mrb_time_getutc(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm, *tm2;

  tm = time_get_ptr(mrb, self);
  tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
  *tm2 = *tm;
  tm2->timezone = MRB_TIMEZONE_UTC;
  time_update_datetime(mrb, tm2);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

#gmt?Boolean

Returns true if this time is in the UTC timezone false if not.

Returns:

  • (Boolean)

815
816
817
818
819
820
821
822
# File 'mrbgems/mruby-time/src/time.c', line 815

static mrb_value
mrb_time_utc_p(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
}

#gmtimeObject

Sets the timezone attribute of the Time object to UTC.


802
803
804
805
806
807
808
809
810
811
# File 'mrbgems/mruby-time/src/time.c', line 802

static mrb_value
mrb_time_utc(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  tm->timezone = MRB_TIMEZONE_UTC;
  time_update_datetime(mrb, tm);
  return self;
}

#hourObject

Returns hour of time.


640
641
642
643
644
645
646
647
# File 'mrbgems/mruby-time/src/time.c', line 640

static mrb_value
mrb_time_hour(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_hour);
}

#initialize_copyObject

Initializes a copy of this time object.


679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'mrbgems/mruby-time/src/time.c', line 679

static mrb_value
mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
{
  mrb_value src;
  struct mrb_time *t1, *t2;

  mrb_get_args(mrb, "o", &src);
  if (mrb_obj_equal(mrb, copy, src)) return copy;
  if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
    mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
  }
  t1 = (struct mrb_time *)DATA_PTR(copy);
  t2 = (struct mrb_time *)DATA_PTR(src);
  if (!t2) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
  }
  if (!t1) {
    t1 = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
    mrb_data_init(copy, t1, &mrb_time_type);
  }
  *t1 = *t2;
  return copy;
}

#inspectObject

Returns a string that describes the time.


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'mrbgems/mruby-time/src/time.c', line 556

static mrb_value
mrb_time_asctime(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm = time_get_ptr(mrb, self);
  struct tm *d = &tm->datetime;
  int len;

#if defined(MRB_DISABLE_STDIO)
  char *s;
# ifdef NO_ASCTIME_R
  s = asctime(d);
# else
  char buf[32];
  s = asctime_r(d, buf);
# endif
  len = strlen(s)-1;            /* truncate the last newline */
#else
  char buf[256];

  len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#localtimeObject

Sets the timezone attribute of the Time object to LOCAL.


705
706
707
708
709
710
711
712
713
714
# File 'mrbgems/mruby-time/src/time.c', line 705

static mrb_value
mrb_time_localtime(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  tm->timezone = MRB_TIMEZONE_LOCAL;
  time_update_datetime(mrb, tm);
  return self;
}

#mdayObject

Returns day of month of time.


718
719
720
721
722
723
724
725
# File 'mrbgems/mruby-time/src/time.c', line 718

static mrb_value
mrb_time_mday(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_mday);
}

#minObject

Returns minutes of time.


729
730
731
732
733
734
735
736
# File 'mrbgems/mruby-time/src/time.c', line 729

static mrb_value
mrb_time_min(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_min);
}

#monObject

Returns month of time.


740
741
742
743
744
745
746
747
# File 'mrbgems/mruby-time/src/time.c', line 740

static mrb_value
mrb_time_mon(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_mon + 1);
}

#monday?Boolean

Returns:

  • (Boolean)

3
# File 'mrbgems/mruby-time/mrblib/time.rb', line 3

def monday?;  wday == 1 end

#monthObject

Returns month of time.


740
741
742
743
744
745
746
747
# File 'mrbgems/mruby-time/src/time.c', line 740

static mrb_value
mrb_time_mon(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_mon + 1);
}

#saturday?Boolean

Returns:

  • (Boolean)

8
# File 'mrbgems/mruby-time/mrblib/time.rb', line 8

def saturday?;  wday == 6 end

#secObject

Returns seconds in minute of time.


751
752
753
754
755
756
757
758
# File 'mrbgems/mruby-time/src/time.c', line 751

static mrb_value
mrb_time_sec(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_sec);
}

#sunday?Boolean

Returns:

  • (Boolean)

2
# File 'mrbgems/mruby-time/mrblib/time.rb', line 2

def sunday?;  wday == 0 end

#thursday?Boolean

Returns:

  • (Boolean)

6
# File 'mrbgems/mruby-time/mrblib/time.rb', line 6

def thursday?;  wday == 4 end

#to_fObject

Returns a Float with the time since the epoch in seconds.


763
764
765
766
767
768
769
770
# File 'mrbgems/mruby-time/src/time.c', line 763

static mrb_value
mrb_time_to_f(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_float_value(mrb, (mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6);
}

#to_iObject

Returns a Fixnum with the time since the epoch in seconds.


774
775
776
777
778
779
780
781
782
783
784
# File 'mrbgems/mruby-time/src/time.c', line 774

static mrb_value
mrb_time_to_i(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  if (tm->sec > MRB_INT_MAX || tm->sec < MRB_INT_MIN) {
    return mrb_float_value(mrb, (mrb_float)tm->sec);
  }
  return mrb_fixnum_value((mrb_int)tm->sec);
}

#to_sObject

Returns a string that describes the time.


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'mrbgems/mruby-time/src/time.c', line 556

static mrb_value
mrb_time_asctime(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm = time_get_ptr(mrb, self);
  struct tm *d = &tm->datetime;
  int len;

#if defined(MRB_DISABLE_STDIO)
  char *s;
# ifdef NO_ASCTIME_R
  s = asctime(d);
# else
  char buf[32];
  s = asctime_r(d, buf);
# endif
  len = strlen(s)-1;            /* truncate the last newline */
#else
  char buf[256];

  len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#tuesday?Boolean

Returns:

  • (Boolean)

4
# File 'mrbgems/mruby-time/mrblib/time.rb', line 4

def tuesday?;  wday == 2 end

#usecObject

Returns a Float with the time since the epoch in microseconds.


788
789
790
791
792
793
794
795
796
797
798
# File 'mrbgems/mruby-time/src/time.c', line 788

static mrb_value
mrb_time_usec(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  if (tm->usec > MRB_INT_MAX || tm->usec < MRB_INT_MIN) {
    return mrb_float_value(mrb, (mrb_float)tm->usec);
  }
  return mrb_fixnum_value((mrb_int)tm->usec);
}

#utcObject

Sets the timezone attribute of the Time object to UTC.


802
803
804
805
806
807
808
809
810
811
# File 'mrbgems/mruby-time/src/time.c', line 802

static mrb_value
mrb_time_utc(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  tm->timezone = MRB_TIMEZONE_UTC;
  time_update_datetime(mrb, tm);
  return self;
}

#utc?Boolean

Returns true if this time is in the UTC timezone false if not.

Returns:

  • (Boolean)

815
816
817
818
819
820
821
822
# File 'mrbgems/mruby-time/src/time.c', line 815

static mrb_value
mrb_time_utc_p(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
}

#wdayObject

Returns week day number of time.


508
509
510
511
512
513
514
515
# File 'mrbgems/mruby-time/src/time.c', line 508

static mrb_value
mrb_time_wday(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_wday);
}

#wednesday?Boolean

Returns:

  • (Boolean)

5
# File 'mrbgems/mruby-time/mrblib/time.rb', line 5

def wednesday?;  wday == 3 end

#ydayObject

Returns year day number of time.


519
520
521
522
523
524
525
526
# File 'mrbgems/mruby-time/src/time.c', line 519

static mrb_value
mrb_time_yday(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_yday + 1);
}

#yearObject

Returns year of time.


530
531
532
533
534
535
536
537
# File 'mrbgems/mruby-time/src/time.c', line 530

static mrb_value
mrb_time_year(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  return mrb_fixnum_value(tm->datetime.tm_year + 1900);
}

#zoneObject

Returns name of time’s timezone.


541
542
543
544
545
546
547
548
549
550
551
552
# File 'mrbgems/mruby-time/src/time.c', line 541

static mrb_value
mrb_time_zone(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm;

  tm = time_get_ptr(mrb, self);
  if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value();
  if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value();
  return mrb_str_new_static(mrb,
                            timezone_names[tm->timezone].name,
                            timezone_names[tm->timezone].len);
}