/** call-seq: * calendar.format(pattern = nil , locale = nil) * * Formats this calendar time using given pattern and locale. Returns UString or nil on failure. * Valid value types for pattern are: * nil - long format for date and time * UString - specification of format, as defined in docs/FORMATTING * Symbol - one of :short, :medium, :long, :full, :none , sets format for both date and time * Hash - {:time => aSymbol, :date => aSymbol} - sets separate formats for date and time, valid symbols see above */ VALUE icu4r_cal_format(int argc, VALUE * argv, VALUE obj) { UErrorCode status = U_ZERO_ERROR; UDateFormat * format; UDate time_to_format; UChar * buf = NULL, * pattern = NULL; long capa = 0, pattern_len = 0; char *locale = NULL; VALUE loc, pat, ret = Qnil; int n , def_d_format = UDAT_FULL, def_t_format = UDAT_FULL; n = rb_scan_args(argc, argv, "02", &pat, &loc); if( n == 2) { Check_Type(loc, T_STRING); locale = RSTRING_PTR(loc); } if (n >= 1 && pat != Qnil) { switch(TYPE(pat)) { case T_SYMBOL: def_d_format = def_t_format = icu4r_get_cal_format_int(pat); break; case T_HASH: def_d_format = icu4r_get_cal_format_int(rb_hash_aref(pat, ID2SYM(rb_intern("date")))); def_t_format = icu4r_get_cal_format_int(rb_hash_aref(pat, ID2SYM(rb_intern("time")))); break; default: Check_Class(pat, rb_cUString); pattern = ICU_PTR(pat); pattern_len = ICU_LEN(pat); break; } } format = udat_open(def_t_format, def_d_format, locale, NULL, 0, NULL, 0, &status); if( pattern ) { udat_applyPattern(format, 0, pattern, pattern_len); } ICU_RAISE(status); udat_setCalendar(format, UCALENDAR(obj)); time_to_format = ucal_getMillis(UCALENDAR(obj), &status); capa = udat_format(format, time_to_format, buf, capa, NULL, &status); if( U_BUFFER_OVERFLOW_ERROR == status) { buf = ALLOC_N(UChar, capa+1); status = U_ZERO_ERROR; capa = udat_format(format, time_to_format, buf, capa, NULL, &status); ret = icu_ustr_new_set(buf, capa, capa+1); } udat_close(format); ICU_RAISE(status); return ret; }
/** * call-seq: * UCalendar.new(zone_id = nil, locale = nil, traditional = false) * * Creates new instance of UCalendar, for given time zone id (UString), * locale (Ruby String), and kind , by default gregorian. * New instance has current time. * */ VALUE icu4r_cal_init (int argc, VALUE * argv, VALUE self) { VALUE zone, loc, cal_type; UChar * zone_id = NULL; char * locale = NULL; UCalendarType c_type = UCAL_GREGORIAN; int32_t n, zone_len =0 , locale_len =0; UCalendar * calendar; UErrorCode status = U_ZERO_ERROR; n = rb_scan_args(argc, argv, "03", &zone, &loc, &cal_type); if( n >= 1) { Check_Class(zone, rb_cUString); zone_id = ICU_PTR(zone); zone_len = ICU_LEN(zone); } if( n >= 2) { Check_Type(loc, T_STRING); locale = RSTRING_PTR(loc); locale_len = RSTRING_LEN(loc); } if( n >= 3) { if( Qtrue == cal_type ) { c_type = UCAL_TRADITIONAL; } } calendar = ucal_open(zone_id, zone_len, locale, c_type, &status); ICU_RAISE(status); DATA_PTR(self) = calendar; return self; }
/** * call-seq: * calendar.in_daylight_time? * * Determine if a UCalendar is currently in daylight savings time. * * Daylight savings time is not used in all parts of the world */ VALUE icu4r_cal_in_daylight(VALUE obj) { UErrorCode status = U_ZERO_ERROR; int32_t answer; answer = ucal_inDaylightTime(UCALENDAR(obj), &status); ICU_RAISE(status); return answer ? Qtrue : Qfalse; }
/** * call-seq: * calendar.millis = new_value * * Sets calendar's value in milliseconds. */ VALUE icu4r_cal_set_millis(VALUE obj,VALUE milli) { UErrorCode status = U_ZERO_ERROR; Check_Type(milli, T_FLOAT); ucal_setMillis(UCALENDAR(obj), rb_num2dbl(milli), &status); ICU_RAISE(status); return Qnil; }
/** * call-seq: * calendar.millis * * return this calendar value in milliseconds. */ VALUE icu4r_cal_millis(VALUE obj) { UErrorCode status = U_ZERO_ERROR; double millis; millis = ucal_getMillis(UCALENDAR(obj), &status); ICU_RAISE(status); return rb_float_new(millis); }
/** * call-seq: * UCalendar.default_tz = ustring * * Set the default time zone. * * UCalendar.default_tz="GMT+00".u * UCalendar.default_tz="Europe/Paris".u */ VALUE icu4r_cal_set_default_tz(VALUE obj, VALUE tz) { UErrorCode status = U_ZERO_ERROR; Check_Class(tz, rb_cUString); ucal_setDefaultTimeZone (ICU_PTR(tz), &status); ICU_RAISE(status); return tz; }
/** * call-seq: * calendar.time_zone = zone_id * * Set the TimeZone used by a UCalendar. */ VALUE icu4r_cal_set_tz(VALUE obj, VALUE zone) { UErrorCode status = U_ZERO_ERROR; Check_Class(zone, rb_cUString); ucal_setTimeZone(UCALENDAR(obj), ICU_PTR(zone), ICU_LEN(zone), &status); ICU_RAISE(status); return Qnil; }
/** * call-seq: * collator.set_attr(attribute, value) * collator[attribute]=value * * Universal attribute setter. See above for valid attributes and their values **/ VALUE icu4r_col_set_attr(VALUE self, VALUE obj, VALUE new_val) { UErrorCode status = U_ZERO_ERROR; Check_Type(obj, T_FIXNUM); Check_Type(new_val, T_FIXNUM); ucol_setAttribute(UCOLLATOR(self), FIX2INT(obj), FIX2INT(new_val), &status); ICU_RAISE(status); return Qnil; }
/** * call-seq: * UCalendar.dst_savings(zone_id) * * Return the amount of time in milliseconds that the clock is advanced * during daylight savings time for the given time zone, or zero if the time * zone does not observe daylight savings time. * * UCalendar.dst_savings("GMT+00".u) # => 3600000 * */ VALUE icu4r_cal_dst_savings(VALUE obj, VALUE zone) { UErrorCode status = U_ZERO_ERROR; int32_t dst; Check_Class(zone, rb_cUString); dst = ucal_getDSTSavings (ICU_PTR(zone), &status); ICU_RAISE(status); return INT2FIX(dst); }
/** * call-seq: * collator.get_attr(attribute) * collator[attribute] * * Universal attribute setter. See above for valid attributes and their values **/ VALUE icu4r_col_get_attr(VALUE self, VALUE obj) { UErrorCode status = U_ZERO_ERROR; UColAttributeValue val; Check_Type(obj, T_FIXNUM); val = ucol_getAttribute(UCOLLATOR(self), FIX2INT(obj), &status); ICU_RAISE(status); return INT2FIX(val); }
/** * call-seq: * calendar.set_date(year, month, date) * * Set a UCalendar's current date. */ VALUE icu4r_cal_set_date(VALUE obj,VALUE year, VALUE mon, VALUE date) { UErrorCode status = U_ZERO_ERROR; Check_Type(year, T_FIXNUM); Check_Type(mon, T_FIXNUM); Check_Type(date, T_FIXNUM); ucal_setDate(UCALENDAR(obj), FIX2INT(year), FIX2INT(mon), FIX2INT(date), &status); ICU_RAISE(status); return Qnil; }
/** * call-seq: * calendar[field] * * Get the current value of a field from a UCalendar. * */ VALUE icu4r_cal_aref(VALUE obj, VALUE field) { UErrorCode status = U_ZERO_ERROR; int date_field; int32_t value; Check_Type(field, T_SYMBOL); date_field = icu4r_get_cal_field_int(field); value = ucal_get(UCALENDAR(obj), date_field, &status); ICU_RAISE(status); return INT2FIX(value); }
/** * call-seq: * calendar.roll(field, int_amount) * * Adds a signed amount to the specified calendar field without changing larger fields. * A negative roll amount means to subtract from field without changing larger fields. * If the specified amount is 0, this method performs nothing. * * Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(:month, 8) * sets the calendar to April 30, 1999. Using a Gregorian Calendar, the :day_of_month field cannot * be 31 in the month April. :day_of_month is set to the closest possible value, 30. The :year field * maintains the value of 1999 because it is a larger field than :month. */ VALUE icu4r_cal_roll(VALUE obj, VALUE field, VALUE amount) { UErrorCode status = U_ZERO_ERROR; int date_field; Check_Type(field, T_SYMBOL); Check_Type(amount, T_FIXNUM); date_field = icu4r_get_cal_field_int(field); ucal_roll(UCALENDAR(obj), date_field, FIX2INT(amount) , &status); ICU_RAISE(status); return Qnil; }
VALUE icu4r_cal_cmp (VALUE c1, VALUE c2) { UErrorCode status = U_ZERO_ERROR; double millis1, millis2; Check_Class(c1, rb_cUCalendar); Check_Class(c2, rb_cUCalendar); millis1 = ucal_getMillis(UCALENDAR(c1), &status); millis2 = ucal_getMillis(UCALENDAR(c2), &status); ICU_RAISE(status); if(millis1 < millis2) return INT2FIX(-1); if(millis1 > millis2) return INT2FIX(1); return INT2FIX(0); }
/** * call-seq: * UCalendar.default_tz => ustring * * Returns the default time zone name as UString. * * UCalendar.default_tz # "EET" * */ VALUE icu4r_cal_get_default_tz(VALUE obj) { UErrorCode status = U_ZERO_ERROR; UChar * buf ; long capa = 0; capa = ucal_getDefaultTimeZone (buf, capa, &status); if( U_BUFFER_OVERFLOW_ERROR == status) { buf = ALLOC_N(UChar, capa+1); status = U_ZERO_ERROR; capa = ucal_getDefaultTimeZone (buf, capa, &status); return icu_ustr_new_set(buf, capa, capa+1); } ICU_RAISE(status); return Qnil; }
/** * call-seq: * UCalendar.time_zones * * Returns array with all time zones (as UString values). */ VALUE icu4r_cal_all_tz (VALUE obj) { UErrorCode status = U_ZERO_ERROR; UEnumeration * zones ; VALUE ret ; UChar * name; int32_t len; zones = ucal_openTimeZones (&status); ICU_RAISE(status); ret = rb_ary_new(); while( (name = (UChar*)uenum_unext(zones, &len, &status))) { rb_ary_push(ret, icu_ustr_new(name, len)); } uenum_close(zones); return ret; }
/** * call-seq: * col = UCollator.new(locale = nil) * * Open a UCollator for comparing strings for the given locale containing the required collation rules. * Special values for locales can be passed in - if +nil+ is passed for the locale, the default locale * collation rules will be used. If empty string ("") or "root" are passed, UCA rules will be used. */ VALUE icu4r_col_init(int argc, VALUE * argv, VALUE self) { UCollator * col; UErrorCode status = U_ZERO_ERROR; VALUE loc; char * locale = NULL; if( rb_scan_args(argc, argv, "01", &loc)) { Check_Type(loc, T_STRING); locale = RSTRING(loc)->ptr; } col = ucol_open(locale, &status); ICU_RAISE(status); DATA_PTR(self)=col; return self; }
/** * call-seq: * UCalendar.tz_for_country(country) * * Returns array with all time zones associated with the given country. * Note: <code>country</code> must be value of type String. * Returned array content is UString's * * UCalendar.tz_for_country("GB") # => [ "Europe/Belfast", "Europe/London", "GB", "GB-Eire"] * */ VALUE icu4r_cal_country_tz (VALUE obj, VALUE ctry) { UErrorCode status = U_ZERO_ERROR; UEnumeration * zones ; VALUE ret ; UChar * name; int32_t len; Check_Type(ctry, T_STRING); zones = ucal_openCountryTimeZones (RSTRING_PTR(ctry), &status) ; ICU_RAISE(status); ret = rb_ary_new(); while( (name = (UChar*)uenum_unext(zones, &len, &status))) { rb_ary_push(ret, icu_ustr_new(name, len)); } uenum_close(zones); return ret; }
/** * call-seq: * calendar.time_zone(locale = nil) * * Returns the TimeZone name used in this UCalendar. Name is returned in requested locale or default, if not set. */ VALUE icu4r_cal_get_tz (int argc, VALUE * argv, VALUE obj) { UErrorCode status = U_ZERO_ERROR; UChar * buf = NULL; long capa = 0; char *locale = NULL; VALUE loc; if( rb_scan_args(argc, argv, "01", &loc) == 1){ Check_Type(loc, T_STRING); locale = RSTRING_PTR(loc); } capa = ucal_getTimeZoneDisplayName(UCALENDAR(obj), UCAL_STANDARD, locale, buf, capa, &status); if( U_BUFFER_OVERFLOW_ERROR == status) { buf = ALLOC_N(UChar, capa+1); status = U_ZERO_ERROR; capa = ucal_getTimeZoneDisplayName(UCALENDAR(obj), UCAL_STANDARD, locale, buf, capa, &status); return icu_ustr_new_set(buf, capa, capa+1); } ICU_RAISE(status); return Qnil; }