示例#1
0
/**
 * 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;
}
示例#2
0
/** 
 * 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;
}
示例#3
0
文件: collator.c 项目: jchris/icu4r
/**
 * call-seq:
 *     collator.sort_key(an_ustring) -> String
 *
 * Get a sort key for a string from a UCollator. Sort keys may be compared using strcmp.
 **/
VALUE icu4r_col_sort_key(VALUE self, VALUE str)
{
    int32_t needed , capa ;
    char * buffer ; 
    VALUE ret;
    Check_Class(str, rb_cUString);
    capa = ICU_LEN(str);
    buffer = ALLOC_N(char, capa);
    needed = ucol_getSortKey(UCOLLATOR(self), ICU_PTR(str), ICU_LEN(str), buffer, capa);
    if(needed > capa){
      REALLOC_N(buffer,char, needed);
      needed = ucol_getSortKey(UCOLLATOR(self), ICU_PTR(str), ICU_LEN(str), buffer, needed);
    }
    ret = rb_str_new(buffer, needed);
    free(buffer);
    return ret;
}
示例#4
0
/** 
 * 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;

}
示例#5
0
/**
 * 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);
}
示例#6
0
/** 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;
}