How do I find the current system timezone?

后端 未结 12 455
攒了一身酷
攒了一身酷 2020-11-30 06:22

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 e

12条回答
  •  萌比男神i
    2020-11-30 07:05

    Here's code that works for most versions of Linux.

    #include 
    #include 
    #include 
    #include 
    #include 
    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;
    } 
    

提交回复
热议问题