Re: [Vala] Date format
- From: Reid Thompson <Reid Thompson ateb com>
- To: "raster rastersoft com" <raster rastersoft com>
- Cc: "vala-list gnome org" <vala-list gnome org>
- Subject: Re: [Vala] Date format
- Date: Tue, 25 Oct 2011 20:37:56 +0000
From the bottom of this page
http://www.gnu.org/s/hello/manual/libc/The-Elegant-and-Fast-Way.html
you would just need to query for the default format using
nl_langinfo (D_T_FMT)
check it, and then configure your output to match except with YYYY in
place of YY.
An example of nl_langinfo usage is a function which has to print a given date and time in a locale-specific
way. At first one might think that, since strftime internally uses the locale information, writing something
like the following is enough:
size_t
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
{
return strftime (s, len, "%X %D", tp);
}
The format contains no weekday or month names and therefore is
internationally usable. Wrong! The output produced is something like
"hh:mm:ss MM/DD/YY". This format is only recognizable in the USA. Other
countries use different formats. Therefore the function should be
rewritten like this:
size_t
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
{
return strftime (s, len, nl_langinfo (D_T_FMT), tp);
}
Now it uses the date and time format of the locale selected when the
program runs. If the user selects the locale correctly there should
never be a misunderstanding over the time and date format.
reid
On Tue, 2011-10-25 at 22:06 +0200, rastersoft wrote:
Thanks, but that doesn't solve my problem. I already got access to that,
and tested format("%x"); unfortunately, it puts the year as a two-digit
number.
I want the same than format("%x") but with four-digit years; the problem
is how to know if the current locale mandates to write month/day/year or
day/month/year.
Currently I do a check at program startup, doing format("%x") for date
day 1, month 3, year 2005, and check the output: if there's a 3 as the
first number, then the order is month/day/year; if not, it's
day/month/year. Unfortunately, that's an extremely ugly hack. So I want
to know if is there a straightforward way of doing this.
Thanks.
El 25/10/11 15:35, Reid Thompson escribió:
On Mon, 2011-10-24 at 22:33 +0200, rastersoft wrote:
I want to print a date in the format dd/mm/yyyy or mm/dd/yyyy,
acording
slightly modified from http://live.gnome.org/Vala/TimeSample
void main () {
// A DateTime from a Unix timestamp
int64 timestamp = 1234151912;
var time = new DateTime.from_unix_utc (timestamp);
// convert back to Unix timestamp
assert (time.to_unix () == timestamp);
// A DateTime from year, month, day, hour, minute, second
time = new DateTime.utc (2010, 10, 22, 9, 22, 0);
// The current time in local timezone
var now = new DateTime.now_local ();
print ("Is daylight savings time: %s\n", now.is_daylight_savings () ? "yes" : "no");
print ("The timezone abbreviation is: %s\n", now.get_timezone_abbreviation ());
// returns time in RFC 3339 format: 2010-10-21T23:48:03+0200
string date_string = now.to_string ();
print ("Current time in RFC 3339 format: %s\n", date_string);
// for example, according to the current locale
print ("According to the current locale: %s\n", now.format ("%x %X"));
print ("Day of month: %d\n", now.get_day_of_month ());
print ("Week of year: %d\n", now.get_week_of_year ());
// Add one day, three hours and five minutes to a DateTime:
var future = now.add_days (1).add_hours (3).add_minutes (5);
print (@"Plus one day, three hours and five minutes: $future\n");
print ("According to the current locale: %s\n", now.format ("%d/%m/%Y"));
print ("According to the current locale: %s\n", now.format ("%m/%d/%Y"));
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]