473,624 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can strftime() product non-locale specific (i.e. UTC) results?

Greetings,

Is there any way to force strftime() to ignore locale settings when
formatting the resulting string? I have a requirement to create a specific
date-time string format in UTC. gmtime() will populate a struct tm with the
correct values but strftime() always assumes that the values are in local
time. I could manually adjust the struct tm values so that strftime() would
generate the desired string but it gets really ugly when the locale is west
of 0 degrees longitude. Furthermore, experimentation has shown that
strftime() can correctly adjust the values when they exceed the normal upper
range but not when they are negative (i.e. after an arbitrary subtraction
which would be required to adjust local time appropriately for locales west
of 0 degrees longitude). Would using putenv("TZ=UTC0 DUT") with tzset()
solve the problem (assuming that I have no need of locally adjusted time)?
Any thoughts/help would be greatly appreciated. Thanks.

Mike
Nov 14 '05 #1
2 3962
"Mike Conmackie" <mc**********@S PAMcogeco.ca> wrote:
Is there any way to force strftime() to ignore locale settings when
formatting the resulting string?


No; the definition of strftime() in the Standard demands that it uses
the current locale. However, you can try to
- use setlocale(LC_TI ME, 0) to determine the current locale, then
either;
- use setlocale(LC_TI ME, "C") to set the locale to the default "C"
locale, or
- use setlocale(LC_TI ME, "UTC") to attempt to set the time locale to
UTC (or whatever other value happens to work for you), then
- use strftime(), then
- use setlocale(LC_TI ME, saved_value) to set the locale back.

Unfortunately, you can _try_ doing this, but there's no guarantee either
that the "C" locale uses UTC, or that other locales such as "UTC" can be
set.

As for tzset(), it is IIRC a POSIX extension; in any case, it isn't ISO
C. It may work, but you'll need to ask in comp.unix.progr ammer (that is,
if it _is_ POSIX).

Richard
Nov 14 '05 #2

In article <Ji************ ****@read1.cgoc able.net>, "Mike Conmackie" <mc**********@S PAMcogeco.ca> writes:

Is there any way to force strftime() to ignore locale settings when
formatting the resulting string?
Yes: don't use the conversion specifiers which are locale-sensitive.
I have a requirement to create a specific
date-time string format in UTC. gmtime() will populate a struct tm with the
correct values but strftime() always assumes that the values are in local
time.
Ah; you're talking about local time, not locale. In C, at least, the
local timezone is not part of the locale. (Whether the timezone can be
considered part of the locale at all is debatable.)

In any case, there appears to be an implementation bug or a misconfigured
environment. strftime should format the time in timeptr; it should not
convert it to local time first. That's what localtime is for. If you
pass strftime a timeptr from gmtime, UTC is what you should get.

I'd lean toward a misconfigured environment rather than a broken
implementation, but without any information about either that's just a
guess.
Would using putenv("TZ=UTC0 DUT") with tzset()
solve the problem (assuming that I have no need of locally adjusted time)?


tzset is not a standard C function.

OT: tzset is a standard POSIX / SUS function. SUSv3 notes that its
strftime behaves "as though strftime() called tzset()", which suggests
that no call to tzset would be necessary. However, SUSv3 strftime
should only use timezone information for the conversion specifiers
which display it (such as %Z). SUSv3 strftime, like the C standard
strftime (to which it defers), should not be converting the data in
the struct tm pointed to by timeptr between timezones.

Have you checked the data returned by gmtime to see that it's correct?
A misconfigured environment could produce incorrect gmtime output.

A code sample would help.

--
Michael Wojcik mi************@ microfocus.com

Do we have boyfriends? We are interested in delicious food and sweets.
And tiny animals like the cat. -- Naoko Yamano
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3405
by: Bryan O'Malley | last post by:
Forgive me if this is an obvious question, but I've done hours of searching, and I can't figure this out. VC++ 6 / Win32 platforms I'm trying to use strftime with the %x option to give me a date in the format specified in the users Control Panel. Though I live in the US, for example, I may have told windows that I want to see dates as dd/mm/yyyy instead of mm/dd/yyyy.
7
3093
by: Eric Slan | last post by:
Hello All: I'm having a problem that's been baffling me for a few days and I seek counsel here. I have an Access 2000 DB from which I want to run several reports. These reports are essentially the same (albeit for minor formatting differences). To produce these reports, I am drawing from three tables:
7
4078
by: Brett Edman | last post by:
I created a UTC clock using this: UTCTime = MyTime.ToUniversalTime() Now that we've turned the clocks ahead 1 hour for daylight savings time, the clock is reporting the wrong UTC time. It is reporting UTC + 1 hour. Is this a bug or is there a way I can querey for daylight time and make the adjustment in my application? Thanks.
7
2673
by: Dick | last post by:
I have a long list of events. I know where each event took place (globally) and when (using the event’s local time). I want to do some comparisons between these date/times. I thought I would convert them all to UTC and thus get them all on a common time-line. But I can’t see how to do this. I can use TimeZone.CurrentTimeZone to get the current TimeZone object and use its ToUniversalTime method to convert dates that occurred in the...
2
1361
by: silverburgh.meryl | last post by:
Hi, I have question about strftime. I am trying to print the current time in this format: date = strftime("%Y%m%d_%H%M%S", gmtime()) print date I run the script at 2:18 pm, but I get this: 20070210_201837
1
4073
by: Matt | last post by:
Hi all, So a lot of digging on doing this and still not a fabulous solution: import time # this takes the last_modified_date naive datetime, converts it to a # UTC timetuple, converts that to a timestamp (seconds since the # epoch), subtracts the timezone offset (in seconds), and then converts
12
4468
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there a way to get the format of the %x directive (it differs across locales)? For ex, default (locale is C), %x gives like %m/%d/%y. Thank you
1
6549
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query specifies for each record... Essentially this is a make shift Ordering Tool and I want to be able to notify the receiver of the order and its specifics. q_Order_Detail_4email consists of ,,, and Example: Hello@mail.com,956,Modem,1000,xyz123...
7
2488
by: eskgwin | last post by:
I have this piece of code that takes a day of the year and converts it to the month and day of month: char start_mon; char start_day; int s_day = 84; time_t daysecs = (s_day * 86400) - 1; struct tm tm_result; struct tm *tm_time = &tm_result; tm_time = gmtime_r(&daysecs,&tm_result);
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.