Class: Time

Inherits:
Object
  • 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.



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'mrbgems/mruby-time/src/time.c', line 739

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



553
554
555
556
557
558
559
560
561
562
563
564
# File 'mrbgems/mruby-time/src/time.c', line 553

static mrb_value
mrb_time_plus(mrb_state *mrb, mrb_value self)
{
  mrb_value o;
  struct mrb_time *tm;
  time_t sec, usec;

  mrb_get_args(mrb, "o", &o);
  tm = time_get_ptr(mrb, self);
  sec = mrb_to_time_t(mrb, o, &usec);
  return mrb_time_make_time(mrb, mrb_obj_class(mrb, self), tm->sec+sec, tm->usec+usec, tm->timezone);
}

#-Object

15.2.19.7.3



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'mrbgems/mruby-time/src/time.c', line 566

static mrb_value
mrb_time_minus(mrb_state *mrb, mrb_value self)
{
  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) {
#ifndef MRB_WITHOUT_FLOAT
    mrb_float f;
    f = (mrb_sec)(tm->sec - tm2->sec)
      + (mrb_sec)(tm->usec - tm2->usec) / 1.0e6;
    return mrb_float_value(mrb, f);
#else
    mrb_int f;
    f = tm->sec - tm2->sec;
    if (tm->usec < tm2->usec) f--;
    return mrb_fixnum_value(f);
#endif
  }
  else {
    time_t sec, usec;
    sec = mrb_to_time_t(mrb, other, &usec);
    return mrb_time_make_time(mrb, mrb_obj_class(mrb, self), tm->sec-sec, tm->usec-usec, tm->timezone);
  }
}

#<=>Object

15.2.19.7.1



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'mrbgems/mruby-time/src/time.c', line 527

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



512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'mrbgems/mruby-time/src/time.c', line 512

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.



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'mrbgems/mruby-time/src/time.c', line 645

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 %2d %02d:%02d:%02d %.4d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#ctimeObject

Returns a string that describes the time.



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'mrbgems/mruby-time/src/time.c', line 645

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 %2d %02d:%02d:%02d %.4d",
    wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
    d->tm_hour, d->tm_min, d->tm_sec,
    d->tm_year + 1900);
#endif
  return mrb_str_new(mrb, buf, len);
}

#dayObject

Returns the day in the month of the time.



674
675
676
677
678
679
680
681
# File 'mrbgems/mruby-time/src/time.c', line 674

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)


686
687
688
689
690
691
692
693
# File 'mrbgems/mruby-time/src/time.c', line 686

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.



698
699
700
701
702
703
704
705
706
707
708
709
# File 'mrbgems/mruby-time/src/time.c', line 698

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, TRUE);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

#getlocalObject

Returns the Time object of the LOCAL timezone.



713
714
715
716
717
718
719
720
721
722
723
724
# File 'mrbgems/mruby-time/src/time.c', line 713

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, TRUE);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

#getutcObject

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



698
699
700
701
702
703
704
705
706
707
708
709
# File 'mrbgems/mruby-time/src/time.c', line 698

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, TRUE);
  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)


908
909
910
911
912
913
914
915
# File 'mrbgems/mruby-time/src/time.c', line 908

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.



895
896
897
898
899
900
901
902
903
904
# File 'mrbgems/mruby-time/src/time.c', line 895

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, FALSE);
  return self;
}

#hourObject

Returns hour of time.



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

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.



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'mrbgems/mruby-time/src/time.c', line 767

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



948
949
950
951
952
953
954
955
956
# File 'mrbgems/mruby-time/src/time.c', line 948

static mrb_value
mrb_time_to_s(mrb_state *mrb, mrb_value self)
{
  char buf[64];
  struct mrb_time *tm = time_get_ptr(mrb, self);
  mrb_bool utc = tm->timezone == MRB_TIMEZONE_UTC;
  size_t len = (utc ? time_to_s_utc : time_to_s_local)(mrb, tm, buf, sizeof(buf));
  return mrb_str_new(mrb, buf, len);
}

#localtimeObject

Sets the timezone attribute of the Time object to LOCAL.



793
794
795
796
797
798
799
800
801
802
# File 'mrbgems/mruby-time/src/time.c', line 793

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, FALSE);
  return self;
}

#mdayObject

Returns day of month of time.



806
807
808
809
810
811
812
813
# File 'mrbgems/mruby-time/src/time.c', line 806

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.



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

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.



828
829
830
831
832
833
834
835
# File 'mrbgems/mruby-time/src/time.c', line 828

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.



828
829
830
831
832
833
834
835
# File 'mrbgems/mruby-time/src/time.c', line 828

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.



839
840
841
842
843
844
845
846
# File 'mrbgems/mruby-time/src/time.c', line 839

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.



851
852
853
854
855
856
857
858
# File 'mrbgems/mruby-time/src/time.c', line 851

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 an Integer with the time since the epoch in seconds.



863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'mrbgems/mruby-time/src/time.c', line 863

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

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

#to_sObject

15.2.19.7.3



948
949
950
951
952
953
954
955
956
# File 'mrbgems/mruby-time/src/time.c', line 948

static mrb_value
mrb_time_to_s(mrb_state *mrb, mrb_value self)
{
  char buf[64];
  struct mrb_time *tm = time_get_ptr(mrb, self);
  mrb_bool utc = tm->timezone == MRB_TIMEZONE_UTC;
  size_t len = (utc ? time_to_s_utc : time_to_s_local)(mrb, tm, buf, sizeof(buf));
  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 an Integer with the time since the epoch in microseconds.



879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'mrbgems/mruby-time/src/time.c', line 879

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

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

#utcObject

Sets the timezone attribute of the Time object to UTC.



895
896
897
898
899
900
901
902
903
904
# File 'mrbgems/mruby-time/src/time.c', line 895

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, FALSE);
  return self;
}

#utc?Boolean

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

Returns:

  • (Boolean)


908
909
910
911
912
913
914
915
# File 'mrbgems/mruby-time/src/time.c', line 908

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.



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

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.



608
609
610
611
612
613
614
615
# File 'mrbgems/mruby-time/src/time.c', line 608

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.



619
620
621
622
623
624
625
626
# File 'mrbgems/mruby-time/src/time.c', line 619

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.



630
631
632
633
634
635
636
637
638
639
640
641
# File 'mrbgems/mruby-time/src/time.c', line 630

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);
}