Class: Time

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_zoneObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'ext/icu_time.c', line 266

VALUE rb_Time_singleton_get_default_zone(VALUE self)
{
	UChar * timeZone = ALLOC_N(UChar, 64);
	int32_t timeZoneLen;
	UErrorCode status = U_ZERO_ERROR;
	
	timeZoneLen = ucal_getDefaultTimeZone(timeZone, 2, &status);
	if (status == U_BUFFER_OVERFLOW_ERROR) {
		status = U_ZERO_ERROR;
		REALLOC_N(timeZone, UChar, timeZoneLen);
		ucal_getDefaultTimeZone(timeZone, timeZoneLen, &status);
	}
	RAISE_ON_ERROR(status);
	
	return u_strToRString(timeZone, timeZoneLen);
}

.default_zone=(zone) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'ext/icu_time.c', line 252

VALUE rb_Time_singleton_set_default_zone(VALUE self, VALUE zone)
{
	UErrorCode status = U_ZERO_ERROR;

	ucal_setDefaultTimeZone((const UChar *)u_strFromRString(zone, NULL), &status);
	RAISE_ON_ERROR(status);
	
	return zone;
}

.localize(: date) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/icu_time.c', line 287

VALUE rb_Time_singleton_pattern(int argc, VALUE *argv, VALUE self)
{
	VALUE options;
	UDateFormatStyle dateStyle = UDAT_DEFAULT, timeStyle = UDAT_DEFAULT;
	char *locale = NULL;
	
	UDateFormat *format;
	UErrorCode status = U_ZERO_ERROR;
	
	UChar * pattern = ALLOC_N(UChar, 64);
	int32_t patternLen;

	/* arguments */
	rb_scan_args(argc, argv, "01", &options);
	if (options != Qnil) {
		VALUE rb_locale;
		
		Check_Type(options, T_HASH);
		/* date and time style */
		dateStyle = getStyle(options, "date");
		timeStyle = getStyle(options, "time");
		/* locale */
		rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
		if (rb_locale != Qnil) {
			locale = StringValuePtr(rb_locale);
		}
	}
	/* formatter */
	status = U_ZERO_ERROR;
    format = udat_open(timeStyle, dateStyle, locale, NULL, 0, NULL, 0, &status);
    RAISE_ON_ERROR(status);
    /* pattern */
	patternLen = udat_toPattern(format, 0, pattern, 64, &status);
	if (status == U_BUFFER_OVERFLOW_ERROR) {
		status = U_ZERO_ERROR;
		REALLOC_N(pattern, UChar, patternLen);
		udat_toPattern(format, 0, pattern, 64, &status);
	}
	RAISE_ON_ERROR(status);
	/* free */
	udat_close(format);
    
    return u_strToRString(pattern, patternLen);
}

.localize(: date) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ext/icu_time.c', line 336

VALUE rb_Time_singleton_week_start(int argc, VALUE *argv, VALUE self)
{
	VALUE options;
	char * locale = NULL;
	UErrorCode status = U_ZERO_ERROR;
	UCalendar * calendar;
	int32_t result;
	
	/* arguments */
	rb_scan_args(argc, argv, "01", &options);
	if (options != Qnil) {
		VALUE rb_locale;
		
		Check_Type(options, T_HASH);
		/* locale */
		rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
		if (rb_locale != Qnil) {
			locale = StringValuePtr(rb_locale);
		}
	}
	/* calendar */
	calendar = ucal_open(NULL, 0, locale, UCAL_GREGORIAN, &status);
	RAISE_ON_ERROR(status);
	/* attribute */
	result = ucal_getAttribute(calendar, UCAL_FIRST_DAY_OF_WEEK);
	ucal_close(calendar);
	
	return INT2NUM(result);
}

.zones(: countries) ⇒ Array .zones(: countries) ⇒ Object

Overloads:

  • .zones(: countries) ⇒ Array

    Returns:

    • (Array)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/icu_time.c', line 137

VALUE rb_Time_singleton_zones(int argc, VALUE *argv, VALUE self)
{
	VALUE options, countries = Qfalse, group = Qfalse;
	char *locale = NULL;
	UCalendarDisplayNameType type = -1;
	
	UCalendar *calendar;
	UErrorCode status = U_ZERO_ERROR;
	UEnumeration *enumeration;
	VALUE select = rb_ary_new();
	UCollator *collator;
	
	//arguments
	rb_scan_args(argc, argv, "01", &options);
	if (options != Qnil) {
		VALUE rb_locale, rb_type;
		
		Check_Type(options, T_HASH);
		//locale
		rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
		if (rb_locale != Qnil) {
			locale = StringValuePtr(rb_locale);
		}
		//countries
		countries = rb_hash_aref(options, ID2SYM(rb_intern("countries")));
		if (countries == Qnil) {
			countries = Qfalse;
		} else {
			
		}
		//group
		group = rb_hash_aref(options, ID2SYM(rb_intern("group")));
		group = group && group != Qnil ? Qtrue : Qfalse;
		//type
		rb_type = rb_hash_aref(options, ID2SYM(rb_intern("type")));
		if (rb_type != Qnil) {
			ID rb_type_ID;
	
			Check_Type(rb_type, T_SYMBOL);
			rb_type_ID = SYM2ID(rb_type);
			if (rb_type_ID == rb_intern("standard")) {
				type = UCAL_STANDARD;
			} else if (rb_type_ID == rb_intern("short_standard")) {
				type = UCAL_SHORT_STANDARD;
			} else if (rb_type_ID == rb_intern("dst")) {
				type = UCAL_DST;
			} else if (rb_type_ID == rb_intern("short_dst")) {
				type = UCAL_SHORT_DST;
			} else {
				rb_raise(rb_eArgError, "unsupported time zone type %s", rb_id2name(rb_type_ID));
			}
		}
	}
	//calendar
	calendar = ucal_open(NULL, -1, locale, UCAL_GREGORIAN, &status);
	RAISE_ON_ERROR(status);
	
	collator = ucol_open(locale, &status);
	RAISE_ON_ERROR(status);
	
	if (countries) {
		const char* const* isoCountries = uloc_getISOCountries();
		
		while (*isoCountries != NULL) {
			if (TYPE(countries) == T_SYMBOL || (TYPE(countries) == T_ARRAY && rb_ary_includes(countries, rb_str_new2(*isoCountries)))) {
				if (group) {
					char fakeLocale[4] = "_";
					UChar *country = ALLOC_N(UChar, 32);
					int32_t countryLen = 32;
					VALUE optgroup = rb_ary_new2(2);
					
					strcat(fakeLocale, *isoCountries);
					countryLen = uloc_getDisplayCountry(fakeLocale, locale, country, countryLen, &status);
					if (status == U_BUFFER_OVERFLOW_ERROR) {
						status = U_ZERO_ERROR;
						REALLOC_N(country, UChar, countryLen);
						uloc_getDisplayCountry(fakeLocale, locale, country, countryLen, &status);
					}
					rb_ary_push(optgroup, u_strToRString(country, countryLen));
					enumeration = ucal_openCountryTimeZones(*isoCountries, &status);
					RAISE_ON_ERROR(status);
					rb_ary_push(optgroup, enumToOptions(enumeration, rb_ary_new(), calendar, type, locale));
					uenum_close(enumeration);
					if (RARRAY(RARRAY(optgroup)->ptr[1])->len) {
						ruby_qsort(RARRAY(RARRAY(optgroup)->ptr[1])->ptr, RARRAY(RARRAY(optgroup)->ptr[1])->len, sizeof(VALUE), collateByDisplayName, collator);
						rb_ary_push(select, optgroup);
					}
				} else {
					enumeration = ucal_openCountryTimeZones(*isoCountries, &status);
					RAISE_ON_ERROR(status);
					enumToOptions(enumeration, select, calendar, type, locale);
					uenum_close(enumeration);
				}
			}
			*isoCountries++;
		}
	} else {
		enumeration = ucal_openTimeZones(&status);
		RAISE_ON_ERROR(status);
		enumToOptions(enumeration, select, calendar, type, locale);
		uenum_close(enumeration);
	}
	//sort
	ruby_qsort(RARRAY(select)->ptr, RARRAY(select)->len, sizeof(VALUE), collateByDisplayName, collator);
	//free
	ucal_close(calendar);
	ucol_close(collator);
	
	return select;
}

Instance Method Details

#localize(: date) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/icu_time.c', line 12

VALUE rb_Time_localize(int argc, VALUE *argv, VALUE self)
{
	VALUE options;
	UDateFormatStyle dateStyle = UDAT_DEFAULT, timeStyle = UDAT_DEFAULT;
	char *locale = NULL;
	const UChar *timeZone = 0;
	int32_t timeZoneLen = -1;
	
	UDateFormat *format;
	UErrorCode status;
	UChar result[256];
	/* arguments */
	rb_scan_args(argc, argv, "01", &options);
	if (options != Qnil) {
		VALUE rb_locale, rb_timeZone;
		
		Check_Type(options, T_HASH);
		/* date and time style */
		dateStyle = getStyle(options, "date");
		timeStyle = getStyle(options, "time");
		/* locale */
		rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
		if (rb_locale != Qnil) {
			locale = StringValuePtr(rb_locale);
		}
		/* time zone */
		rb_timeZone = rb_hash_aref(options, ID2SYM(rb_intern("zone")));
		if (rb_timeZone != Qnil) {
			Check_Type(rb_timeZone, T_STRING);
			timeZone = u_strFromRString(rb_timeZone, &timeZoneLen);
		}
	}
	
	/* formatter */
	status = U_ZERO_ERROR;
    format = udat_open(timeStyle, dateStyle, locale, timeZone, timeZoneLen, NULL, -1, &status);
    RAISE_ON_ERROR(status);
	/* format */
	status = U_ZERO_ERROR;
	{
		extern struct timeval rb_time_timeval(VALUE time);
		struct timeval tv;
		UDate dateToFormat;
		UChar result[256];
		
		tv = rb_time_timeval(self);
    	dateToFormat = ((double)tv.tv_sec + (double)tv.tv_usec / 1e6) * 1e3;
    	udat_format(format, dateToFormat, result, 256, NULL, &status);
	    RAISE_ON_ERROR(status);
	    /* free resources */
	    unum_close(format);
	   
	    return u_strToRString(result, -1);
	}
}