Sponsored Content
Operating Systems Linux Reading Daylight Saving Time in Linux using C/C++ Post 302383093 by jim mcnamara on Monday 28th of December 2009 10:40:12 AM
Old 12-28-2009
I stayed out of this one for a while. Anytime you make assumptions about timezone or date you get in trouble - of all applications, calendrics and time get mishandled the most, IMO.

The std C runtime plus the environment is the final arbiter on what the system thinks about time and date.

This code is a rather stupid (with no error checking) brute force solution. It has assumptions: 32 bit system, years from 1970-2037, 2 ds time changes per year, and we are N of the equator. That way "start ds" is the first change we will hit if we begin Jan 01. Use at your own risk.

Code:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>

#define FULL_FMT "%m-%d-%Y"
#define DT_LEN 80
#define TZ_LEN 28
#define DAY 86400

typedef
struct
{
  char start[DT_LEN];
  char tz1[TZ_LEN];  
  char end[DT_LEN];
  char tz2[TZ_LEN];
  
} dsavings_t;

char *sec2dt(char *dest, time_t sec, const char *fmt, char *cmp, dsavings_t *ds)
{
	struct tm *p=localtime(&sec);
	*dest=0x0;

	strftime(dest, DT_LEN, fmt, p);
	if(cmp!=NULL && strcmp(cmp, dest))
	{
		strcpy(   (*ds->tz1)?   ds->tz2: ds->tz1, cmp);
		strftime( (*ds->start)? ds->end: ds->start, DT_LEN, FULL_FMT, p);
		strcpy(cmp, dest);
	}
	return dest;
}

int tzshift(dsavings_t *ds, const int year)
{
	  struct tm w={0,0,0,0,0,0,0,0,0};
	  int i=0;
	  char tmp[DT_LEN]={0x0};
	  char tz[DT_LEN]={0x0};
	  time_t lt=0;
	  memset(ds, 0x0, sizeof(dsavings_t));	 	  
    sprintf(tmp,"01-01-%4d-23:00", year);
	  strptime(tmp, "%d-%m-%Y-%H:%M", &w);
	  lt=mktime(&w);
	  sec2dt(tz, lt, "%Z", NULL, NULL);
	  /*lt+=DAY; */
	  for(i=0; *(sec2dt(tmp, lt, "%Z", tz, ds)) && i<366; i++, lt+=DAY);	    
	  return (*ds->start && *ds->end)? 1 : 0;
}

int main(int argc, char **argv)
{
	  int yr=0;
	  dsavings_t ds={ {0x0}, {0x0} };
	  int retval=0;
	  for(yr=1970; yr < 2038; yr++)
	  {
	  		retval=tzshift(&ds, yr);
	  		printf("%d: %s (%s)  %s (%s)\n", yr, 
	  		       (retval) ? ds.start : "None",
	  		       (retval) ? ds.tz1 : "",
	  		       (retval) ? ds.end : "None",
	  		       (retval) ? ds.tz2 : "");
	  }
	  return 0;
}

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Timestamp - confusion with daylight saving time

Hello! I'm currently working on a climatological dataset that uses unix-timestamp and a real date like 28/03/2004 02:45:00 h. in spring the unix-timestamp follows its continuos mode but in the column of the real date the time jumps one hour forward (e.g. from 1:59:59 to 3:00:00). i think this is... (1 Reply)
Discussion started by: softmachine
1 Replies

2. Solaris

disable daylight saving

hi ... i have an E450 sun server that is running solaris 6 . i want to disable daylight savings on my server . My question is : 1) how to know that my server is running daylight savings ? 2) how to disable it ? my zoneinfo file contains the following # @(#)init.dfl 1.2 92/11/26 # #... (1 Reply)
Discussion started by: ppass
1 Replies

3. AIX

Daylight Saving Time patch test

On AIX 5.1, after applying a DST patch or workaround, is there a way to test that the DST changes have taken effect? (1 Reply)
Discussion started by: dangral
1 Replies

4. HP-UX

How to set the Daylight Saving Time (DST) OFF

I have a problem with the time so I need to set the DST to be OFF. is anybody can show me how to set the DST to be OFF? (11 Replies)
Discussion started by: Billy_yuli
11 Replies

5. UNIX for Dummies Questions & Answers

Daylight saving not working properly with linux-2.6. kernel

The daylight saving action is not working Time zone was set for that Australia(parth) and issued reboot. Now for DST, (Daylight Saving Time): For 29 Oct 2006 (sunday) day, if set time is 1:59:00 than after 1 minute it will become 3:00:00 as per DST . This was found to be happening.i.e DST... (1 Reply)
Discussion started by: subratasaharia
1 Replies

6. UNIX for Advanced & Expert Users

Daylight saving not working properly with linux-2.6. kernel

The daylight saving action is not working Time zone was set for that Australia(parth) and issued reboot. Now for DST, (Daylight Saving Time): For 29 Oct 2006 (sunday) day, if set time is 1:59:00 than after 1 minute it will become 3:00:00 as per DST . This was found to be happening.i.e DST... (1 Reply)
Discussion started by: subratasaharia
1 Replies

7. Linux

How to configure daylight saving time

Hi, I have few doubt on daylight saving. Can any one clarify the below points. 1) How to configure daylight saving(DLS) time ? 2) How to query DLS ? 3) If i set DLS(say some how) will it be autometically changed or user has to manually change system time ? I got few info on net about... (3 Replies)
Discussion started by: ashokd001
3 Replies

8. Linux

How does Linux handle DST ( daylight saving time)?

Hello Can sombody please tell me how linux handles DST ( daylight saving time) ? Does the time change instantly , ex: if is is 3'o clock does linux instantly swich to 2'o clock ? plese give more info about this toppic (2 Replies)
Discussion started by: martonlorand
2 Replies

9. Solaris

Solaris 11 Daylight Saving Time

Hello, Quick question: How do I verify if my Solaris 11 hosts/zones, configured with NTP, would change automatically to the DST? Thanks! (1 Reply)
Discussion started by: feroccimx
1 Replies
STRING(3)						     Linux Programmer's Manual							 STRING(3)

NAME
stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strp- brk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex - string operations SYNOPSIS
#include <strings.h> int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n); char *index(const char *s, int c); char *rindex(const char *s, int c); #include <string.h> char *stpcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); char *strchr(const char *s, int c); int strcmp(const char *s1, const char *s2); int strcoll(const char *s1, const char *s2); char *strcpy(char *dest, const char *src); size_t strcspn(const char *s, const char *reject); char *strdup(const char *s); char *strfry(char *string); size_t strlen(const char *s); char *strncat(char *dest, const char *src, size_t n); int strncmp(const char *s1, const char *s2, size_t n); char *strncpy(char *dest, const char *src, size_t n); char *strpbrk(const char *s, const char *accept); char *strrchr(const char *s, int c); char *strsep(char **stringp, const char *delim); size_t strspn(const char *s, const char *accept); char *strstr(const char *haystack, const char *needle); char *strtok(char *s, const char *delim); size_t strxfrm(char *dest, const char *src, size_t n); DESCRIPTION
The string functions perform string operations on null-terminated strings. See the individual man pages for descriptions of each function. SEE ALSO
index(3), rindex(3), strcasecmp(3), stpcpy(3), strcat(3), strchr(3), strcmp(3), strcoll(3), strcpy(3), strcspn(3), strdup(3), strfry(3), strlen(3), strncasecmp(3), strncat(3), strncmp(3), strncpy(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3), strtok(3), strxfrm(3) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2010-02-25 STRING(3)
All times are GMT -4. The time now is 06:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy