Solved

Equivalent solution for gettimezoneinformation

Posted on 2009-05-16
12
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 4

Expert Comment

by:ewest02
ID: 24403754
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 40

Accepted Solution

by:
noci earned 250 total points
ID: 24403999
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
ID: 24404630
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:ashish_mangla
ID: 24404938
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
ID: 24407248
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
 
LVL 40

Expert Comment

by:noci
ID: 24407826
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 40

Expert Comment

by:noci
ID: 25544161
Any further questions?
0
 

Expert Comment

by:EE_AutoDeleter
ID: 41602755
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 40

Expert Comment

by:noci
ID: 41602756
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 40

Expert Comment

by:noci
ID: 41603254
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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