Solved

Equivalent solution for gettimezoneinformation

Posted on 2009-05-16
12
94 Views
Last Modified: 2016-05-21
Hello I am currently porting my code from windows to linux. I have written function which will give same functionality like GetSystemTime & GetLocalTime. Please suggest a function which will give me same functionality like GetTimeZoneInformation and return all the values in structure TIME_ZONE_INFORMATION.
static TIME_ZONE_INFORMATION  st = { 0,   L" ", 

                                                       { 0xFFFF, 0, 0, 0, 0, 0, 0, 0 },     

                                                       0,  L" ", 

                                                       { 0, 0, 0, 0, 0, 0, 0, 0 }, 

                                                       0 };
 

GetTimeZoneInformation(&st);

Open in new window

0
Comment
Question by:ashish_mangla
12 Comments
 
LVL 4

Expert Comment

by:ewest02
Comment Utility
gmtime(3) might be a starting point..

returns a struct tm

struct tm {
                  int tm_sec;         /* seconds */
                  int tm_min;         /* minutes */
                  int tm_hour;        /* hours */
                  int tm_mday;        /* day of the month */
                  int tm_mon;         /* month */
                  int tm_year;        /* year */
                  int tm_wday;        /* day of the week */
                  int tm_yday;        /* day in the year */
                  int tm_isdst;       /* daylight saving time */
              };

0
 
LVL 39

Accepted Solution

by:
noci earned 250 total points
Comment Utility
I guess from the funtion name stat you want timezone info, this can be found through:

man 3 tzset   - Sets some global variales to values from the TZ or /etc/localtime info sources.
man 5 tzfile   - TZzone file format layout
man 8 zdump - show content of timezone files formatted like tzfile.

kind regards.
0
 
LVL 4

Expert Comment

by:ewest02
Comment Utility
ftime(3)  

int ftime(struct timeb *tp);

DESCRIPTION
       Return current date and time in tp, which is declared as follows:

            struct timeb {
                time_t         time;
                unsigned short millitm;
                short          timezone;
                short          dstflag;
            };

0
 

Author Comment

by:ashish_mangla
Comment Utility
Hello All,

   Thanx for your reply. But i m not satisfied with the answer. GetTimeZoneInformation returns the DayLight Saving time and transition time in TIME_ZONE_STRUCTURE.

TIME_ZONE_INFORMATION will be

typedef struct _TIME_ZONE_INFORMATION {
  LONG       Bias;
  WCHAR      StandardName[32];
  SYSTEMTIME StandardDate;
  LONG       StandardBias;
  WCHAR      DaylightName[32];
  SYSTEMTIME DaylightDate;
  LONG       DaylightBias;
}TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION;

Structure Members
1) Bias
The current bias for local time translation on this computer, in minutes. The bias is the difference, in minutes, between Coordinated Universal Time (UTC) and local time. All translations between UTC and local time are based on the following formula:

UTC = local time + bias

This member is required.

2) StandardName
A description for standard time. For example, "EST" could indicate Eastern Standard Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.

3) StandardDate
A SYSTEMTIME structure that contains a date and local time when the transition from daylight saving time to standard time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SYSTEMTIME structure must be zero. If this date is specified, the DaylightDate member of this structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.

To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurrence of the day of the week within the month (1 to 5, where 5 indicates the final occurrence during the month if that day of the week does not occur 5 times).

Using this notation, specify 02:00 on the first Sunday in April as follows: wHour = 2, wMonth = 4, wDayOfWeek = 0, wDay = 1. Specify 02:00 on the last Thursday in October as follows: wHour = 2, wMonth = 10, wDayOfWeek = 4, wDay = 5.

If the wYear member is not zero, the transition date is absolute; it will only occur one time. Otherwise, it is a relative date that occurs yearly.

4) StandardBias
The bias value to be used during local time translations that occur during standard time. This member is ignored if a value for the StandardDate member is not supplied.

This value is added to the value of the Bias member to form the bias used during standard time. In most time zones, the value of this member is zero.

5) DaylightName
A description for daylight saving time. For example, "PDT" could indicate Pacific Daylight Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.

6) DaylightDate
A SYSTEMTIME structure that contains a date and local time when the transition from standard time to daylight saving time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SYSTEMTIME structure must be zero. If this date is specified, the StandardDate member in this structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.

To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurrence of the day of the week within the month (1 to 5, where 5 indicates the final occurrence during the month if that day of the week does not occur 5 times).

If the wYear member is not zero, the transition date is absolute; it will only occur one time. Otherwise, it is a relative date that occurs yearly.

7) DaylightBias
The bias value to be used during local time translations that occur during daylight saving time. This member is ignored if a value for the DaylightDate member is not supplied.

This value is added to the value of the Bias member to form the bias used during daylight saving time. In most time zones, the value of this member is 60.

While the Structure SYSTEMTIME will be

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
}SYSTEMTIME, *PSYSTEMTIME;

Structure  Members

1) wYear
The year. The valid values for this member are 1601 through 30827.

2) wMonth
The month. This member can be one of the following values.

Value Meaning
1 January
 
2 February
 

3) wDayOfWeek
The day of the week. This member can be one of the following values.

Value Meaning
0 Sunday
 
1 Monday
 
4) wDay
The day of the month. The valid values for this member are 1 through 31.

5) wHour
The hour. The valid values for this member are 0 through 23.

6) wMinute
The minute. The valid values for this member are 0 through 59.

7) wSecond
The second. The valid values for this member are 0 through 59.

8) wMilliseconds
The millisecond. The valid values for this member are 0 through 999.




   I want the information same like this only. Can u suggest me any appropriate function or appropriate code.

   GetTimeZoneInformation returns:

1)  If the function succeeds, it returns one of the following values.

    TIME_ZONE_ID_UNKNOWN   -  Daylight saving time is not used in the current time zone, because there are no transition dates or automatic adjustment for daylight saving time is disabled.
 
    TIME_ZONE_ID_STANDARD -   The system is operating in the range covered by the StandardDate member of the TIME_ZONE_INFORMATION structure.
 
   TIME_ZONE_ID_DAYLIGHT -    The system is operating in the range covered by the DaylightDate member of the TIME_ZONE_INFORMATION structure.
0
 
LVL 4

Assisted Solution

by:ewest02
ewest02 earned 250 total points
Comment Utility
So use the suggested system/library functions to write your own variant for linux/unix. There is no "standard" linux/unix function/API that will return that data exactly like the WinX GetTimeZoneInformation()

You do however have a series of calendar/time related APIs that will provide the information in a way that you can create your own function to approximate the Windows function. These include:

       asctime,   ctime,   gmtime,   localtime,  mktime,  asctime_r,  ctime_r,
       gmtime_r, localtime_r - transform date and time to broken-down time  or
       ASCII

      gettimeofday - get time

       strftime - format date and time

Starter kit:

        struct tm *  tm;
        char        *  tz_str;
        char        *  tz_str;
        time_t tsec = time( NULL);     // get current time

        tm = localtime( tsec);             // get time data struct adjusted for this TZ
        tzset();                                  // set the external time zone,char * tzname[2]
        tz_str = tzname[0];                // ie EST, CST, ...
       
The values returned in the struct tm with localtime are nearly the same as those returned in the SystemTime struct for WinX. The functions mentioned abovet can be used to convert numeric values to strings (ex tm_mon := {0....11} -> "January")


       




0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

by:noci
Comment Utility
zdump as mentioned before can give you the list. It will show the date/time of change & offset for a timezone and if DST is active or not.
btw. it generates the list by itering dates and checking for changes of the dst value.

0
 
LVL 39

Expert Comment

by:noci
Comment Utility
Any further questions?
0
 

Expert Comment

by:EE_AutoDeleter
Comment Utility
I've requested that this question be deleted for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Not enough information to confirm an answer.

If you feel this question should be closed differently, post an objection and a moderator will read all objections and then close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Auto Deleter
0
 
LVL 39

Expert Comment

by:noci
Comment Utility
I disagree with delete...., several time functions have been given along with usage to get what the asker needs.
There is no Exact match between Unix/Linux API's and windows.... so some translation needs to be done.

As Microsoft chose to ignore the existing API's and created it's own ...
0
 
LVL 39

Expert Comment

by:noci
Comment Utility
IMHO All experts have added to the solution...
First answers helped to get the answer straight, later answers give where to look for specific items.
so equal to all?
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now