Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

date_str.c 5.5 KB
Newer Older
1 2 3 4 5 6
/* Synchronet date/time string conversion routines */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

22 23
#include "date_str.h"
#include "datewrap.h"
24

25 26 27 28
const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
const char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
            ,"Jul","Aug","Sep","Oct","Nov","Dec"};

29 30 31
/****************************************************************************/
/* Converts a date string in format MM/DD/YY into unix time format			*/
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
32
time32_t dstrtounix(scfg_t* cfg, const char *instr)
33
{
34 35 36 37
	const char*	p;
	const char*	day;
	char		str[16];
	struct tm	tm;
38

39
	if(!instr[0] || !strncmp(instr,"00/00/00",8))
40
		return(0);
41

42 43 44
	if(IS_DIGIT(instr[0]) && IS_DIGIT(instr[1])
		&& IS_DIGIT(instr[3]) && IS_DIGIT(instr[4])
		&& IS_DIGIT(instr[6]) && IS_DIGIT(instr[7]))
45 46 47
		p=instr;	/* correctly formatted */
	else {
		p=instr;	/* incorrectly formatted */
48
		while(*p && IS_DIGIT(*p)) p++;
49 50 51 52
		if(*p==0)
			return(0);
		p++;
		day=p;
53
		while(*p && IS_DIGIT(*p)) p++;
54 55 56 57 58 59 60 61
		if(*p==0)
			return(0);
		p++;
		sprintf(str,"%02u/%02u/%02u"
			,atoi(instr)%100,atoi(day)%100,atoi(p)%100);
		p=str;
	}

62
	memset(&tm,0,sizeof(tm));
63
	tm.tm_year=((p[6]&0xf)*10)+(p[7]&0xf);
64 65 66
	if (tm.tm_year<Y2K_2DIGIT_WINDOW)
		tm.tm_year+=100;
	if(cfg->sys_misc&SM_EURODATE) {
67
		tm.tm_mon=((p[3]&0xf)*10)+(p[4]&0xf);
68 69
		tm.tm_mday=((p[0]&0xf)*10)+(p[1]&0xf); 
	}
70
	else {
71
		tm.tm_mon=((p[0]&0xf)*10)+(p[1]&0xf);
72 73
		tm.tm_mday=((p[3]&0xf)*10)+(p[4]&0xf); 
	}
74 75
	if (tm.tm_mon)
		tm.tm_mon--;	/* zero-based month field */
76
	tm.tm_isdst=-1;		/* Do not adjust for DST */
77
	return(mktime32(&tm));
78 79 80 81 82
}

/****************************************************************************/
/* Converts unix time format (long - time_t) into a char str MM/DD/YY		*/
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
83
char* unixtodstr(scfg_t* cfg, time32_t t, char *str)
84
{
85
	struct tm tm;
86
	time_t unix_time=t;
87

88
	if(!unix_time)
89 90
		strcpy(str,"00/00/00");
	else {
91
		if(localtime_r(&unix_time,&tm)==NULL) {
92 93 94
			strcpy(str,"00/00/00");
			return(str);
		}
95 96
		if(tm.tm_mon>11) {	  /* DOS leap year bug */
			tm.tm_mon=0;
97 98
			tm.tm_year++; 
		}
99 100
		if(tm.tm_mday>31)
			tm.tm_mday=1;
101
		if(cfg->sys_misc&SM_EURODATE)
102 103
			sprintf(str,"%02u/%02u/%02u",tm.tm_mday,tm.tm_mon+1
				,TM_YEAR(tm.tm_year));
104
		else
105
			sprintf(str,"%02u/%02u/%02u",tm.tm_mon+1,tm.tm_mday
106 107
				,TM_YEAR(tm.tm_year)); 
	}
108 109 110 111 112 113
	return(str);
}

/****************************************************************************/
/* Takes the value 'sec' and makes a string the format HH:MM:SS             */
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
114
char* sectostr(uint sec,char *str)
115 116 117 118 119 120 121 122 123 124
{
    uchar hour,min,sec2;

	hour=(sec/60)/60;
	min=(sec/60)-(hour*60);
	sec2=sec-((min+(hour*60))*60);
	sprintf(str,"%2.2d:%2.2d:%2.2d",hour,min,sec2);
	return(str);
}

rswindell's avatar
rswindell committed
125
/* Returns a shortened version of "HH:MM:SS" formatted seconds value */
Rob Swindell's avatar
Rob Swindell committed
126
char* seconds_to_str(uint seconds, char* str)
rswindell's avatar
rswindell committed
127 128 129 130 131 132 133
{
	char* p = sectostr(seconds, str);
	while(*p=='0' || *p==':')
		p++;
	return p;
}

rswindell's avatar
rswindell committed
134 135
/****************************************************************************/
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
136
char* hhmmtostr(scfg_t* cfg, struct tm* tm, char* str)
rswindell's avatar
rswindell committed
137 138 139 140 141 142 143 144 145 146
{
	if(cfg->sys_misc&SM_MILITARY)
		sprintf(str,"%02d:%02d "
	        ,tm->tm_hour,tm->tm_min);
	else
		sprintf(str,"%02d:%02d%c"
	        ,tm->tm_hour>12 ? tm->tm_hour-12 : tm->tm_hour==0 ? 12 : tm->tm_hour
			,tm->tm_min,tm->tm_hour>=12 ? 'p' : 'a');
	return(str);
}
147

148 149 150 151
/****************************************************************************/
/* Generates a 24 character ASCII string that represents the time_t pointer */
/* Used as a replacement for ctime()                                        */
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
152
char* timestr(scfg_t* cfg, time32_t t, char* str)
153 154 155
{
    char*		mer;
	uchar		hour;
156
    struct tm	tm;
157
	time_t		intime=t;
158

159
	if(localtime_r(&intime,&tm)==NULL) {
160 161 162 163 164
		strcpy(str,"Invalid Time");
		return(str); 
	}
	if(cfg->sys_misc&SM_MILITARY) {
		sprintf(str,"%s %s %02u %4u %02u:%02u:%02u"
165 166
			,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
			,tm.tm_hour,tm.tm_min,tm.tm_sec);
167 168
		return(str); 
	}
169 170
	if(tm.tm_hour>=12) {
		if(tm.tm_hour==12)
171 172
			hour=12;
		else
173
			hour=tm.tm_hour-12;
174 175
		mer="pm"; 
	} else {
176
		if(tm.tm_hour==0)
177 178
			hour=12;
		else
179
			hour=tm.tm_hour;
180 181 182
		mer="am"; 
	}
	sprintf(str,"%s %s %02u %4u %02u:%02u %s"
183 184
		,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
		,hour,tm.tm_min,mer);
185 186 187
	return(str);
}