Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Tobster_
Posts: 260
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK
Accepted Solution

Changing label based on time

[ Edited ]

Hello,

I have a simple label with the text showing a time; I was wondering: if it would be possible to change the time - of the label (which is inputted via XML) to the user's current time zone and if so, how would I go about doing that?

 

Label

property alias fixtureInfo: fixtureText.text
    property alias timeInfo: timeText.text
    property alias stadiumInfo: stadiumText.text


Header {
                title: qsTr("Time (GMT - 3)")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: timeText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }

 

XML

<continent title="Thursday 12 June 2014"> 
        <listItem title="Brazil vs Croatia">
        <fixtureInfo>Brazil vs Croatia</fixtureInfo>
        <timeInfo>17:00PM (Local Time)</timeInfo>
        <stadiumInfo>Arena de Sao Paulo</stadiumInfo>
        </listItem>
	</continent>



Thanks :smileyhappy:

Please use plain text.
Developer
ekke
Posts: 1,474
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Changing label based on time

here are some tips:

 

to get the name of the current timezonbe you can ask the CalendarService

mCalendarService.settings().currentSystemTimezone();

 here's some code to get some useful values from current time

QVariantMap MonitorTime::nowDateTimeInfo() {
	QVariantMap map;
	QDateTime now = QDateTime::currentDateTime();
	QString nowIso = now.toString(Qt::ISODate);
	QString nowUtcIso = now.toUTC().toString(Qt::ISODate);
	// cut the seconds
	nowUtcIso = nowUtcIso.split("T").at(0) + "T"
			+ nowUtcIso.split("T").at(1).left(6) + "00Z";
	QString day = nowIso.left(10);
	QLocale locale = QLocale().system();
	QString dayFormatted = locale.toString(QDate::fromString(day, Qt::ISODate),
			bb::utility::i18n::dateFormat(locale,
					bb::utility::i18n::DateFormat::Medium));
	QString hhmm = nowIso.split("T").at(1).left(5);
	QString timeZone = mConnectedCalendar->currentCalendarTimeZone();
	QString suffix = currentTimeZoneSuffix(now);
	map.insert("weekday", weekdayLocalized(day));
	map.insert("day", day);
	map.insert("dayFormatted", dayFormatted);
	map.insert("hhmm", hhmm);
	map.insert("timeZone", timeZone);
	map.insert("suffix", suffix);
	map.insert("iso", isoDate(day, hhmm, suffix));
	map.insert("isoUTC", nowUtcIso);
	return map;
}

 ---

QString MonitorTime::currentTimeZoneSuffix(const QDateTime& dateTime) {
	QDateTime dt = dateTime;
	dt.setTimeSpec(Qt::UTC);
	int minutesOffset = dt.secsTo(dateTime) / 3600 * -1;
	QString suffix;
	if (minutesOffset > 0) {
		suffix = "+";
	} else {
		suffix = "-";
	}
	int hoursOffset = abs(minutesOffset) / 60;
	if (hoursOffset < 10) {
		suffix.append("0");
	}
	suffix.append(QString::number(abs(minutesOffset))).append(":");
	minutesOffset = abs(minutesOffset) - (hoursOffset * 60);
	if (minutesOffset == 30) {
		suffix.append("30");
	} else {
		suffix.append("00");
	}
	return suffix;
}

 

perhaps you get some ideas from the code

easiest way to see what's in the Map use JsonDataAccess to write the content into a file

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.