1 /* 2 * Copyright 2010, Adrien Destugues, pulkomandy@gmail.com 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <TimeFormat.h> 7 8 #include <unicode/format.h> 9 #include <unicode/tmutfmt.h> 10 #include <unicode/utypes.h> 11 #include <ICUWrapper.h> 12 13 status_t BTimeFormat::Format(int64 number, BString* buffer) const 14 { 15 // create time unit amount instance - a combination of Number and time unit 16 UErrorCode status = U_ZERO_ERROR; 17 18 int64 days, hours, minutes, seconds, remainder; 19 20 days = number / (24 * 3600); 21 remainder = number % (24 * 3600); 22 23 hours = remainder / 3600; 24 remainder %= 3600; 25 26 minutes = remainder / 60; 27 remainder %= 60; 28 29 seconds = remainder; 30 31 TimeUnitFormat* format = new TimeUnitFormat(status); 32 UnicodeString formatted; 33 Formattable formattable; 34 BStringByteSink bbs(buffer); 35 36 if (!U_SUCCESS(status)) { 37 delete format; 38 return B_ERROR; 39 } 40 41 if (days) { 42 TimeUnitAmount* daysAmount = new TimeUnitAmount(days, 43 TimeUnit::UTIMEUNIT_DAY, status); 44 45 formattable.adoptObject(daysAmount); 46 formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, 47 status); 48 } 49 50 if (hours) { 51 TimeUnitAmount* hoursAmount = new TimeUnitAmount(hours, 52 TimeUnit::UTIMEUNIT_HOUR, status); 53 54 formattable.adoptObject(hoursAmount); 55 if (days) 56 formatted.append(", "); 57 formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, 58 status); 59 } 60 61 if (minutes) { 62 TimeUnitAmount* minutesAmount = new TimeUnitAmount(minutes, 63 TimeUnit::UTIMEUNIT_MINUTE, status); 64 65 formattable.adoptObject(minutesAmount); 66 if (days || hours) 67 formatted.append(", "); 68 formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, 69 status); 70 } 71 72 73 if (seconds || (minutes == 0 && hours == 0 && days == 0)) { 74 TimeUnitAmount* secondsAmount = new TimeUnitAmount(seconds, 75 TimeUnit::UTIMEUNIT_SECOND, status); 76 77 formattable.adoptObject(secondsAmount); 78 if (days || hours || minutes) 79 formatted.append(", "); 80 formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, 81 status); 82 } 83 formatted.toUTF8(bbs); 84 85 86 delete format; 87 return B_OK; 88 } 89