4

I have viewed this, and this, on SO, but they deal with non-C variations of the same problem.

Using GNU GCC compiler (Code::Blocks, in Windows) with the following code:

int main(void)
{
    time_t rawtime;
    struct tm *info;
    char timeStr[80];

    time( &rawtime );
    info = localtime( &rawtime );

    strftime(timeStr,80,"%a %b %d %H:%M:%S %Z %Y", info);

    printf("%s", timeStr);
    getchar();

    return 0;
} 

I am getting this time string:

Tue Aug 30 14:55:08 Pacific Daylight Time 2016

Everything is fine except time zone. I prefer it to be abbreviated, such as:

Tue Aug 30 14:55:08 PDT 2016

In every man page, or windows documentation for strftime() I can find the description for the format specifier %Z goes something like: Timezone name or abbreviation. ( eg. here. )

What is the trick to formatting timezone to be abbreviated?

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • "Greenwich Mean Time" *is* abbreviated, to "GMT", but we never see "GMTDT". – Weather Vane Aug 30 '16 at 22:19
  • Have you looked at what Microsoft says about its [`strftime()`](https://msdn.microsoft.com/en-us/library/aa272978(v=vs.60).aspx)? Is your compiler configured to build with the Microsoft C run-time libraries or some other C run-time libraries? – Jonathan Leffler Aug 30 '16 at 22:23
  • @WeatherVane - LOL, good point. I am seeing timestamps coming from a device I am using that is using a Linux like OS, that has an abbreviated timezone. Perhaps it is implementation specific? – ryyker Aug 30 '16 at 22:24
  • @JonathanLeffler - interesting. That is the first time I have seen small z used as an option this way. I do not know. As a side note, I have seen the same behavior using the LabWindows/CVI runtime. which of course is not using MS runtime. The GNU GCC may be. As soon as I find where this is shown in Code::Blocks, I will post another comment. – ryyker Aug 30 '16 at 22:27
  • The POSIX specification of [`strftime()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html) mentions both `%z` and `%Z`. However, that's just a standard; implementations may do things differently (for a multitude of reasons, some good, some less good). I'm not sure whether that helps or not. But it does look to me as if MS and POSIX differ on the meaning of `%z`. Be wary! – Jonathan Leffler Aug 30 '16 at 22:29
  • 1
    http://stackoverflow.com/q/5802753/315052 – jxh Aug 30 '16 at 22:49
  • 1
    @WeatherVane As a matter of fact, in the summertime in Britain, Windows machines will display the string "GMT Daylight Time" (and that's the value `%Z` will expand to, too). – Steve Summit Sep 01 '16 at 00:39
  • @SteveSummit sorry I don't understand your point. That is exactly why I made my first comment, by looking at what I get on a Windows machine. – Weather Vane Sep 01 '16 at 17:15
  • @WeatherVane No worries. I thought you thought you were making a joke (because "GMT Daylight Time" *is* ridiculous), but it turns out -- as I guess you already knew -- Microsoft has a, er, sense of humor when it comes to time zone names, too. – Steve Summit Sep 04 '16 at 20:04

2 Answers2

5

strftime gives no guarantee whether you'll get the full name or the abbreviation. It is implementation dependent. The C99 spec (and Microsoft's documentation) says:

%Z is replaced by the locale’s time zone name or abbreviation, or by no characters if no time zone is determinable.

AFAIK there is no standard C way to guarantee you'll get the time zone abbreviation. That assumes you use the system time zone database. You could ship with your own copy of the time zone database.

In general, time zone abbreviations are a bad idea because they're ambiguous. Skimming the list we find ACT, AMT, BST, ECT, GST, IST, LHST, MST, PST, and SST are all ambiguous. You're better off using the full name, the numeric offset (ie. %z), or eschew time zones altogether and use UTC (particularly useful for logging).

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    "AFAIK there is no standard C way to guarantee you'll get the time zone abbreviation". That's correct. Under Windows you get the full name, under Unix and Linux and other systems which are based on the Arthur David Olson time zone code (including MacOS) you get the abbreviation, and there's no way to ask for one or the other. – Steve Summit Aug 31 '16 at 06:02
  • @SteveSummit - I would have up-clicked this in an answer. It adds to the conversation (and to my understanding) with clear unambiguous statements about strftime()'s timezone behavior. Thank you. – ryyker Aug 31 '16 at 19:42
3

I recently needed the same thing, and determined that there is No Good Answer. I wrote some brute-force code to map Windows timezone strings like "Eastern Standard Time" to "EST", but I knew that (a) this was a Bad Idea and (b) my list of translations would never be complete.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103