how to get current time zone city name like America/Los_Angeles - c++

I tried strftime, which has %z and %Z, but these are outputs like +0800 and PST. I'd like to get the longer name like America/Los_Angeles. Is there a function call to do this?

There is not a standard way to do this until C++20, and even then only the latest MSVC has implemented it to date (gcc is getting close).
In C++20 there is a type std::chrono::time_zone which has a member function called name() which will return a string such as "America/Los_Angeles".
It might be used like this:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
zoned_time local_now{"America/Los_Angeles", system_clock::now()};
cout << local_now << " " << local_now.get_time_zone()->name() << '\n';
}
Which just output for me:
2022-12-31 07:34:41.482431 PST America/Los_Angeles
Or if your computer's local time zone is currently set to "America/Los_Angeles", then the zoned_time construction could look like this instead:
zoned_time local_now{current_zone(), system_clock::now()};
If all you want is the time zone name, and not the current time, this can be further simplified to just:
cout << current_zone()->name() << '\n';
Prior to C++20 the only way I'm aware of to get functionality like this is to use my free, open-source C++20 chrono preview library which will work with C++11/14/17.

One way — I must admit, a decidedly imperfect way — is to read the symbolic link /etc/localtime:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define LOCALTIME "/etc/localtime"
int main()
{
char buf[100];
int r = readlink(LOCALTIME, buf, sizeof(buf)-1);
if(r < 0) {
fprintf(stderr, "can't read link %s: %s\n", LOCALTIME, strerror(errno));
exit(1);
}
buf[r] = '\0';
char *name = buf;
char *p1 = strstr(buf, "zone");
if(p1 != NULL) {
char *p2;
p2 = strstr(p1 + 1, "zone");
if(p2 != NULL) p1 = p2;
p2 = strchr(p1, '/');
if(p2 != NULL)
name = p2 + 1;
}
printf("%s\n", name);
}
This will work on most Unix-like systems, including Linux and MacOS. It will not work on Windows, which AFAIK does not use the IANA tz database at all. It will not work on systems where /etc/localtime is a file, rather than a symbolic link to one of the zoneinfo files.
If /etc/localtime is a file, there is no good way to determine which zone name it represents. I believe you would have to compare it to all of the files underneath /usr/share/zoneinfo, looking for matching contents.
I'm not sure what magic technique Howard uses in his C++ solution.
(But I mean no disrespect with that word "magic".)

Related

Conversion of date from human-readable format to epoch fails

I'd like to create a program that converts the date of a specific human-readable format to epoch.
So far I have the following code the first part of which creates this human-readable format and the second one converts it to epoch.
#include <time.h>
#include <iostream>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char const *argv[])
{
time_t timeNow;
struct tm *ltm = NULL;
time(&timeNow);
ltm = localtime(&timeNow);
char buffer[100];
strftime(buffer, sizeof(buffer), "%c %Z", ltm);
cout << "human readable timestamp is " << buffer << endl;
std::tm tmNow;
memset(&tmNow, 0, sizeof(tmNow));
strptime(buffer, "%c %Z", &tmNow);
cout << "epoch timestamp is " << mktime(&tmNow) << endl;
return 0;
}
So the printouts I get are the following :
human readable timestamp is Thu Sep 16 10:23:06 2021 EEST
epoch timestamp is 1631780586
My time zone is EEST as one can see but the epoch one is wrong because it is one hour ahead. The correct should have been 1631776986. I assume I'm doing wrong something with the local time. I've found third-party libraries examples like boost or poco that do this conversion, but I'd prefer the above conversion to be done by using native C++.
Does anyone see what I'm missing?
The C timing/calendrical API is very difficult to use correctly (which is why C++ is moving away from it).
From the C standard:
The value of tm_isdst is positive if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and negative if the information is not available.
Set tmNow.tm_isdst = -1; prior to the call to mktime.

Can't Create Dynamic Folder Name

In C++ I want to create a dynamic folder each time I run my program.
#include <direct.h> // mkdir
#include <iostream> // std
#include <iomanip> // put_time
int main(){
time_t rawtime;
struct tm * timeinfo;
char buffer[40];
time(&rawtime);
timeinfo = localtime(&rawtime);
//strftime(buffer, sizeof(buffer), "%d-%m-%Y %I:%M:%S", timeinfo);
strftime(buffer, sizeof(buffer), "%d_%m_%Y_%I_%M_%S", timeinfo);
std::string path = "C:/example/";
path.append(std::string(buffer));
mkdir(path.c_str());
//system("pause");
return 0;
}
I want to create a folder named like "Example/03_03_2016_20_22_26", but the code above will not create the folder I want.
If I remove the path.append(std::string(buffer)); line, it will create the folder named example in my C directory.
However I want a folder named according to the complete date and time.
Where am I wrong or what am I missing?
I use this code for a similar purpose in my project (SAVE_DIR is a macro definition):
#include <time.h>
#include <iomanip>
#include <sstream>
std::ostringstream pathstr; // a convenient way to construct strings
std::time_t now = std::time(nullptr); // get the current time
// insert the required parts into the stream
pathstr << SAVE_DIR
<< std::put_time(std::localtime(&now), "%Y_%m_%d_%H_%M_%S") << ".png";
std::string path = pathstr.str(); // and the result as std::str
Output:
/home/user/prog/render/rt/saves/2016_03_03_23_10_50.png
This has the benefit of being pure C++, though it may look a bit clumsy, depending on your taste.
As for what your code may fail, I'd watch the string values in a debugger first, and then save the return value of mkdir() and check it against the specifications: POSIX mkdir().
I guess the issue is in the slash '/'.
On Windows better use the backslash.
Try
std::string path="C:\\example\\"

C++ - How to check if today's date is an a string?

I have a C++ application I'm developing where I just need to check if the current day's date is in a char array, specifically in the format "2015-05-10". I'm pretty new to C++ coming over from PHP where it is very easy to do, but I'm struggling trying to find a good method in C++. This needs to be automated as the script runs daily on a cron job. So the process is:
If (today's date is in char array) {
do this }
else {
do nothing
}
Edit: I am obviously useless at expressing my problems, sorry!
My main issues are:
How do I get the current day's date in a nice simple string in this format - 2015-05-10
How do I then check if a char array I have stored (which I know contains a date amongst some other text) contains the current day's date (which I will, when I know how, have stored as a string).
If I understood correctly, your first want to convert the current date to the format yyyy-mm-dd and then search for the string in another string.
For the first question, you may refer to How to get current time and date in C++? where there are multiple solutions given.
For the second part of the question, if you are using strings, you should use the find (http://www.cplusplus.com/reference/string/string/find/) method and if you are using char arrays, you could use the C strstr (http://www.cplusplus.com/reference/cstring/strstr/) method.
Here's what I tried:
#include <iostream>
#include <string>
#include <cstdio>
#include <ctime>
#include <cstring>
time_t now = time(0);
struct tm tstruct;
char buf[100];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);
//char arrays used
char ch_array[] = "This is the received string 2015-05-10 from server";
char * pch;
pch = strstr(ch_array, buf);
if (pch != nullptr)
std::cout << "Found";
//string used
std::string str("This is the received string 2015-05-10 from server");
std::size_t found = str.find(buf);
if (found != std::string::npos)
std::cout << "date found at: " << found << '\n';

c++ to_string error with date

I am trying to write a function that returns the date as a string. I went about this like so:
string date() // includes not listed for sake of space, using namespace std
{
tm timeStruct;
int currentMonth = timeStruct.tm_mon + 1;
int currentDay = timeStruct.tm_mday;
int currentYear = timeStruct.tm_year - 100;
string currentDate = to_string(currentMonth) + "/" + to_string(currentDay) + "/" + to_string(currentYear);
return currentDate;
}
this is giving four compile time errors.
1 of these:
to_string was not declared in this scope
and 3 of these:
Function to_string could not be resolved
one for each use of to_string.
According to everywhere else on the internet, this code should work. Can someone please shed some light on the subject?
As has been mentioned in the comments, what you're trying to use requires C++11. This means both a compiler that supports C++11 (E.g. GCC 4.7+), AND possibly manually enabling C++11 (E.g. flag -std=c++11), so check both of those if you believe it should be working for you.
If you're stuck with a compiler that does not support C++11, you can use the following to achieve what you want, with regular C++:
string date()
{
tm timeStruct;
int currentMonth = timeStruct.tm_mon + 1;
int currentDay = timeStruct.tm_mday;
int currentYear = timeStruct.tm_year - 100;
char currentDate[30];
sprintf(currentDate, "%02d/%02d/%d", currentMonth, currentDay, currentYear);
return currentDate; // it will automatically be converted to string
}
Note that for the Day and Month parameters, I used %02d to force it to display at least 2 digits, so 5/1 will actually be represented as 05/01. If you don't want that, you can just use %d instead, which will behave like your original to_string. (I'm not sure what format you're using for currentYear, but you'll probably also want to use either %02d or %04d for that parameter)
std::to_string is part of C++11 and is in the <string> header. The following code works with g++ 4.7, as well as recent versions of clang and VC++. If compiling a file with these contents does not work for you, you are either invoking the compiler incorrectly for C++11 or using a compiler version with insufficient support for C++11.
#include <string>
int main() {
int i;
auto s = std::to_string(i);
}
However there is a better way to print dates in C++11. Here's a program that prints the current date (in ISO 8601 format).
#include <ctime> // time, time_t, tm, localtime
#include <iomanip> // put_time
#include <iostream> // cout
#include <sstream> // stringstream
#include <stdexcept> // runtime_error
#include <string> // string
std::string date() {
static constexpr char const *date_format = "%Y-%m-%d"; // ISO 8601 format
auto t = std::time(nullptr);
if (static_cast<std::time_t>(-1) == t) {
throw std::runtime_error{"std::time failed"};
}
auto *cal = std::localtime(&t);
if (nullptr == cal) {
throw std::runtime_error{"std::localetime failed"};
}
std::stringstream ss;
ss << std::put_time(cal, date_format);
return ss.str();
}
int main() { std::cout << date() << '\n'; }
Unfortunately gcc 4.8 appears to lack put_time (and of course VC++ currently lacks constexpr and universal initializers, but that is easily worked around. VC++ has put_time).

How do I find the current system timezone?

On Linux, I need to find the currently configured timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible.
For example. I live in London, so my current Olson location is "Europe/London". I'm not interested in timezone IDs like "BST", "EST" or whatever.
Debian and Ubuntu have a file /etc/timezone that contains this information, but I don't think I can rely on that file always being there, can I? Gnome has a function oobs_time_config_get_timezone() which also returns the right string, but I want my code to work on systems without Gnome.
So, what's the best general way to get the currently configured timezone as an Olson location, on Linux?
It's hard to get a reliable answer. Relying on things like /etc/timezone may be the best bet.
(The variable tzname and the tm_zone member of struct tm, as suggested in other answers, typically contains an abbreviation such as GMT/BST etc, rather than the Olson time string as requested in the question).
On Debian-based systems (including Ubuntu), /etc/timezone is a file containing the right answer.
On some Redhat-based systems (including at least some versions of CentOS, RHEL, Fedora), you can get the required information using readlink() on /etc/localtime, which is a symlink to (for example) /usr/share/zoneinfo/Europe/London.
OpenBSD seems to use the same scheme as RedHat.
However, there are some issues with the above approaches. The /usr/share/zoneinfo directory also contains files such as GMT and GB, so it's possible the user may configure the symlink to point there.
Also there's nothing to stop the user copying the right timezone file there instead of creating a symlink.
One possibility to get round this (which seems to work on Debian, RedHat and OpenBSD) is to compare the contents of the /etc/localtime file to the files under /usr/share/zoneinfo, and see which ones match:
eta:~% md5sum /etc/localtime
410c65079e6d14f4eedf50c19bd073f8 /etc/localtime
eta:~% find /usr/share/zoneinfo -type f | xargs md5sum | grep 410c65079e6d14f4eedf50c19bd073f8
410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/London
410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Belfast
410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Guernsey
410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Jersey
410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Isle_of_Man
...
...
Of course the disadvantage is that this will tell you all timezones that are identical to the current one. (That means identical in the full sense - not just "currently at the same time", but also "always change their clocks on the same day as far as the system knows".)
Your best bet may be to combine the above methods: use /etc/timezone if it exists; otherwise try parsing /etc/localtime as a symlink; if that fails, search for matching timezone definition files; if that fails - give up and go home ;-)
(And I have no idea whether any of the above applies on AIX...)
I've been working on a free, open source C++11/14 library which addresses this question in a single line of code:
std::cout << date::current_zone()->name() << '\n';
It is meant to be portable across all recent flavors of Linux, macOS and Windows. For me this program outputs:
America/New_York
If you download this library, and it doesn't work you, bug reports are welcome.
There is no standard c or c++ function for this. However, GNU libc has an extention. its struct tm has two extra members:
long tm_gmtoff; /* Seconds east of UTC */
const char *tm_zone; /* Timezone abbreviation */
This means that if you use one of the functions which populates a struct tm (such as localtime or gmtime) you can use these extra fields. This is of course only if you are using GNU libc (and a sufficiently recent version of it).
Also many systems have a int gettimeofday(struct timeval *tv, struct timezone *tz); function (POSIX) which will fill in a struct timezone. This has the following fields:
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
Not exactly what you asked for, but close...
Pretty late in the day, but I was looking for something similar and found that ICU library has the provision to get the Olson timezone ID: http://userguide.icu-project.org/datetime/timezone
It is now installed on most linux distributions (install the libicu-dev package or equivalent). Code:
#include <unicode/timezone.h>
#include <iostream>
using namespace U_ICU_NAMESPACE;
int main() {
TimeZone* tz = TimeZone::createDefault();
UnicodeString us;
tz->getID(us);
std::string s;
us.toUTF8String(s);
std::cout << "Current timezone ID: " << s << '\n';
delete tz;
return 0;
}
And to get the abbreviated/POSIX timezone names (should also work on Windows):
#include <time.h>
int main() {
time_t ts = 0;
struct tm t;
char buf[16];
::localtime_r(&ts, &t);
::strftime(buf, sizeof(buf), "%z", &t);
std::cout << "Current timezone (POSIX): " << buf << std::endl;
::strftime(buf, sizeof(buf), "%Z", &t);
std::cout << "Current timezone: " << buf << std::endl;
I see two major linux cases:
Ubuntu. There should be a /etc/timezone file. This file should only contain the timezone and nothing else.
Red Hat. There should be a /etc/sysconfig/clock that contains something like: ZONE="America/Chicago"
In addition, Solaris should have an /etc/TIMEZONE file that contains a line like: TZ=US/Mountain
So based on the above, here is some straight C that I believe answers the OP's question. I have tested it on Ubuntu, CentOS (Red Hat), and Solaris (bonus).
#include <string.h>
#include <strings.h>
#include <stdio.h>
char *findDefaultTZ(char *tz, size_t tzSize);
char *getValue(char *filename, char *tag, char *value, size_t valueSize);
int main(int argc, char **argv)
{
char tz[128];
if (findDefaultTZ(tz, sizeof(tz)))
printf("Default timezone is %s.\n", tz);
else
printf("Unable to determine default timezone.\n");
return 0;
}
char *findDefaultTZ(char *tz, size_t tzSize)
{
char *ret = NULL;
/* If there is an /etc/timezone file, then we expect it to contain
* nothing except the timezone. */
FILE *fd = fopen("/etc/timezone", "r"); /* Ubuntu. */
if (fd)
{
char buffer[128];
/* There should only be one line, in this case. */
while (fgets(buffer, sizeof(buffer), fd))
{
char *lasts = buffer;
/* We don't want a line feed on the end. */
char *tag = strtok_r(lasts, " \t\n", &lasts);
/* Idiot check. */
if (tag && strlen(tag) > 0 && tag[0] != '#')
{
strncpy(tz, tag, tzSize);
ret = tz;
}
}
fclose(fd);
}
else if (getValue("/etc/sysconfig/clock", "ZONE", tz, tzSize)) /* Redhat. */
ret = tz;
else if (getValue("/etc/TIMEZONE", "TZ", tz, tzSize)) /* Solaris. */
ret = tz;
return ret;
}
/* Look for tag=someValue within filename. When found, return someValue
* in the provided value parameter up to valueSize in length. If someValue
* is enclosed in quotes, remove them. */
char *getValue(char *filename, char *tag, char *value, size_t valueSize)
{
char buffer[128], *lasts;
int foundTag = 0;
FILE *fd = fopen(filename, "r");
if (fd)
{
/* Process the file, line by line. */
while (fgets(buffer, sizeof(buffer), fd))
{
lasts = buffer;
/* Look for lines with tag=value. */
char *token = strtok_r(lasts, "=", &lasts);
/* Is this the tag we are looking for? */
if (token && !strcmp(token, tag))
{
/* Parse out the value. */
char *zone = strtok_r(lasts, " \t\n", &lasts);
/* If everything looks good, copy it to our return var. */
if (zone && strlen(zone) > 0)
{
int i = 0;
int j = 0;
char quote = 0x00;
/* Rather than just simple copy, remove quotes while we copy. */
for (i = 0; i < strlen(zone) && i < valueSize - 1; i++)
{
/* Start quote. */
if (quote == 0x00 && zone[i] == '"')
quote = zone[i];
/* End quote. */
else if (quote != 0x00 && quote == zone[i])
quote = 0x00;
/* Copy bytes. */
else
{
value[j] = zone[i];
j++;
}
}
value[j] = 0x00;
foundTag = 1;
}
break;
}
}
fclose(fd);
}
if (foundTag)
return value;
return NULL;
}
FWIW, RHEL/Fedora/CentOS have /etc/sysconfig/clock:
ZONE="Europe/Brussels"
UTC=true
ARC=false
I liked the post made by psmears and implemented this script to read the first output of the list. Of course there must have more elegant ways of doing this, but there you are...
/**
* Returns the (Linux) server default timezone abbreviation
* To be used when no user is logged in (Ex.: batch job)
* Tested on Fedora 12
*
* #param void
* #return String (Timezone abbreviation Ex.: 'America/Sao_Paulo')
*/
public function getServerTimezone()
{
$shell = 'md5sum /etc/localtime';
$q = shell_exec($shell);
$shell = 'find /usr/share/zoneinfo -type f | xargs md5sum | grep ' . substr($q, 0, strpos($q, '/') - 2);
$q = shell_exec($shell);
$q = substr($q, strpos($q, 'info/') + 5, strpos($q, " "));
return substr($q, 0, strpos($q, chr(10)));
}
In my Brazilian Fedora 12, it returns:
Brazil/East
And does exactly what I need.
Thank you psmears
Here's code that works for most versions of Linux.
#include <iostream>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
using namespace std;
void main()
{
char filename[256];
struct stat fstat;
int status;
status = lstat("/etc/localtime", &fstat);
if (S_ISLNK(fstat.st_mode))
{
cout << "/etc/localtime Is a link" << endl;
int nSize = readlink("/etc/localtime", filename, 256);
if (nSize > 0)
{
filename[nSize] = 0;
cout << " linked filename " << filename << endl;
cout << " Timezone " << filename + 20 << endl;
}
}
else if (S_ISREG(fstat.st_mode))
cout << "/etc/localtime Is a file" << endl;
}
According to this page, it looks like if you #include <time.h> it will declare the following.
void tzset (void);
extern char *tzname[2];
extern long timezone;
extern int daylight;
Does that give you the information that you need?
On Linux, I need to find the current timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible.
If you want to be portable, then use only GMT internally. Due to multi-user heritge, *NIX system clock is normally is in GMT and there is no system wide timezone - because different users connected to the system might be living in different timezones.
The user specific timezone is reflected in TZ environment variable and you might need to use that only when converting internal date/time into the user readable form. Otherwise, localtime() takes care of it automatically for you.
The libc accesses the Olson database when tzset is called, and uses simplified time zones afterwards. tzset looks at the TZ environment variable first, and falls back to parsing the binary data in /etc/localtime.
At first systemd standardised on having the Olson time zone name in /etc/timezone, Debian-style. After systemd 190 and the /usr merge, systemd only reads and updates /etc/localtime, with the extra requirement that the file be a symlink to /usr/share/zoneinfo/${OLSON_NAME}.
Looking at TZ, then readlink("/etc/localtime"), is the most reliable way to match the libc's tzset logic and still keep symbolic Olson names. For systems that don't follow the systemd symlink convention, reading /etc/timezone (and possibly checking that /usr/share/zoneinfo/$(</etc/timezone) is the same as /etc/localtime) is a good fallback.
If you can live without symbolic names, parsing the /etc/localtime tzfile is as portable as it gets, though a lot more complex. Reading just the last field gets you a Posix time zone (for example: CST5CDT,M3.2.0/0,M11.1.0/1), which can interoperate with a few time-handling libraries, but drops some of the metadata (no historical transition info).
Since tzselect was not mentioned by anyone and you do need a nearly goof-proof solution, work with what Olson did. Get the tzcode and tzdata files from elsie, plus tab files.
ftp://elsie.nci.nih.gov
In March 2017, the correct location to download from would be ftp://ftp.iana.org/tz/releases (and download tzcode2017a.tar.gz and tzdata2017a.tar.gz).
Then get tzselect.ksh from the glibc download. Then you can see how to reverse engineer timezones. One sticking point: you WILL sometimes have to ask what country and city the linux box is in. You can serialize that data if you want, and then verify it against the timezone data you can find.
There is no way to do this reliably all the time without the possibility of user intervention, for example, as part of program installation.
Good luck on Arizona in general, and in Western Indiana.... hopefully your code is going to run elsewhere.
The code below was tested successfully in bash shell on
SUSE Linux Enterprise Server 11
Ubuntu 20.04.5 LTS
CentOS Linux release 7.9.2009
Shell code:
echo $(hash=$(md5sum /etc/localtime | cut -d " " -f 1) ; find /usr/share/zoneinfo -type f -print0 | while read -r -d '' f; do md5sum "$f" | grep "$hash" && break ; done) | rev | cut -d "/" -f 2,1 | rev
Result example:
Europe/Vienna