mbgtools-lx  4.2.8
ctrydttm.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: ctrydttm.c 1.10 2018/12/11 16:01:46 martin EXP $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Functions converting dates and time into strings depending on
10  * language/country settings.
11  *
12  * -----------------------------------------------------------------------
13  * $Log: ctrydttm.c $
14  * Revision 1.10 2018/12/11 16:01:46 martin
15  * Use standard int types for more compatibility.
16  * Revision 1.9 2018/12/11 11:49:00Z martin
17  * Fixed compiler warnings on Windows.
18  * Revision 1.8 2018/01/02 16:13:55Z martin
19  * Changed return type of snprint_() functions from size_t to int.
20  * Revision 1.7 2015/11/09 11:22:12Z martin
21  * Modified snprint_ctry_dt_short() and fixed snprint_ctry_dt().
22  * Revision 1.6 2015/08/31 10:00:46 martin
23  * Use safe string functions from str_util.c.
24  * This required renaming of functions and changing parameters.
25  * Revision 1.5 2008/11/24 16:15:46Z martin
26  * Don't use sprintf() without format string.
27  * Revision 1.4 2000/11/27 10:06:27 MARTIN
28  * Renamed local variable wday_str to lstrs_wday.
29  * If macro USER_LSTR_WDAY is defined, lstrs_wday can be declared
30  * externally to override the defaults.
31  * Revision 1.3 2000/09/14 15:13:25 MARTIN
32  * Renamed sprint_short_ctry_dt() to sprint_ctry_dt_short() to match
33  * other naming conventions.
34  * Revision 1.2 2000/07/21 11:53:42 MARTIN
35  * Initial revision
36  *
37  **************************************************************************/
38 
39 #define _CTRYDTTM
40  #include <ctrydttm.h>
41 #undef _CTRYDTTM
42 
43 #include <ctry.h>
44 #include <str_util.h>
45 
46 #include <stdio.h>
47 
48 
49 #ifndef DAYS_PER_WEEK
50  #define DAYS_PER_WEEK 7
51 #endif
52 
53 #ifdef USER_LSTRS_WDAY
54  extern const char *lstrs_wday[N_LNG][DAYS_PER_WEEK];
55 #else
56  static const char *lstrs_wday[N_LNG][DAYS_PER_WEEK] =
57  {
58  { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" },
59  { "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" }
60  };
61 #endif
62 
63 extern CTRY ctry;
64 extern LANGUAGE language;
65 
66 
67 /*HDR*/
68 int snprint_02u( char *s, size_t max_len, unsigned int uc )
69 {
70  return snprintf_safe( s, max_len, "%02u", uc );
71 
72 } // snprint_02u
73 
74 
75 
76 /*HDR*/
77 int snprint_04u( char *s, size_t max_len, unsigned int us )
78 {
79  return snprintf_safe( s, max_len, "%04u", us );
80 
81 } // snprint_04u
82 
83 
84 
85 /*HDR*/
86 int snprint_ctry_wday( char *s, size_t max_len, int wday, LANGUAGE language )
87 {
88  if ( language >= N_LNG )
89  language = LNG_ENGLISH;
90 
91  return snprintf_safe( s, max_len, "%s", ( wday < DAYS_PER_WEEK ) ?
92  lstrs_wday[language][wday] : "--" );
93 
94 } // snprint_ctry_wday
95 
96 
97 
98 /*HDR*/
99 int snprint_ctry_dt_short( char *s, size_t max_len, int mday, int month )
100 {
101  uchar tmp_1;
102  uchar tmp_2;
103  ushort n = 0;
104 
105  switch ( ctry.dt_fmt )
106  {
107  case DT_FMT_YYYYMMDD:
108  case DT_FMT_MMDDYYYY:
109  tmp_1 = month;
110  tmp_2 = mday;
111  break;
112 
113  default:
114  tmp_1 = mday;
115  tmp_2 = month;
116  break;
117 
118  } // switch
119 
120  n = snprint_02u( s, max_len, tmp_1 );
121  n += sn_cpy_char_safe( &s[n], max_len - n, ctry.dt_sep );
122  n += snprint_02u( &s[n], max_len - n, tmp_2 );
123 
124  return n;
125 
126 } // snprint_ctry_dt_short
127 
128 
129 
130 /*HDR*/
131 int snprint_ctry_dt( char *s, size_t max_len, int mday, int month, int year )
132 {
133  size_t n = 0;
134 
135  if ( ctry.dt_fmt == DT_FMT_YYYYMMDD )
136  {
137  n += snprint_04u( &s[n], max_len - n, year );
138  n += sn_cpy_char_safe( &s[n], max_len - n, ctry.dt_sep );
139  }
140 
141  n += snprint_ctry_dt_short( &s[n], max_len - n, mday, month );
142 
143  if ( ctry.dt_fmt != DT_FMT_YYYYMMDD )
144  {
145  n += sn_cpy_char_safe( &s[n], max_len - n, ctry.dt_sep );
146  n += snprint_04u( &s[n], max_len - n, year );
147  }
148 
149  return _int_from_size_t( n );
150 
151 } // snprint_ctry_dt
152 
153 
154 
155 /*HDR*/
156 int snprint_ctry_tm_short( char *s, size_t max_len, uchar hour, uchar minute )
157 {
158  size_t n = snprint_02u( s, max_len, hour );
159  n += sn_cpy_char_safe( &s[n], max_len - n, ctry.tm_sep );
160  n += snprint_02u( &s[n], max_len - n, minute );
161 
162  return _int_from_size_t( n );
163 
164 } // snprint_ctry_tm_short
165 
166 
167 
168 /*HDR*/
169 int snprint_ctry_tm( char *s, size_t max_len, uchar hour, uchar minute, uchar second )
170 {
171  size_t n = snprint_ctry_tm_short( s, max_len, hour, minute );
172  n += sn_cpy_char_safe( &s[n], max_len - n, ctry.tm_sep );
173  n += snprint_02u( &s[n], max_len - n, second );
174 
175  return _int_from_size_t( n );
176 
177 } // snprint_ctry_tm
178 
179 
180 
181 /*HDR*/
182 int snprint_ctry_tm_long( char *s, size_t max_len, uchar hour, uchar minute,
183  uchar second, long frac, ushort frac_digits )
184 {
185  size_t n = snprint_ctry_tm( s, max_len, hour, minute, second );
186  n += sn_cpy_char_safe( &s[n], max_len - n, '.' );
187  n += snprintf_safe( &s[n], max_len - n, "%0*lu", frac_digits, frac );
188 
189  return _int_from_size_t( n );
190 
191 } // snprint_ctry_tm_long
192 
193 
int snprint_ctry_tm_short(char *s, size_t max_len, uchar hour, uchar minute)
Definition: ctrydttm.c:156
CTRY ctry
Definition: mbgstatus.c:126
int snprint_ctry_tm_long(char *s, size_t max_len, uchar hour, uchar minute, uchar second, long frac, ushort frac_digits)
Definition: ctrydttm.c:182
int snprint_ctry_tm(char *s, size_t max_len, uchar hour, uchar minute, uchar second)
Definition: ctrydttm.c:169
#define _int_from_size_t(_n)
Definition: words.h:662
uint8_t LANGUAGE
Definition: ctry.h:97
char tm_sep
Definition: ctry.h:131
unsigned short ushort
Definition: words.h:282
int snprint_ctry_dt(char *s, size_t max_len, int mday, int month, int year)
Definition: ctrydttm.c:131
unsigned char uchar
Definition: words.h:277
static int frac_digits
Definition: mbgsvcd.c:91
uint8_t dt_fmt
Definition: ctry.h:128
int snprint_ctry_dt_short(char *s, size_t max_len, int mday, int month)
Definition: ctrydttm.c:99
static const char * lstrs_wday[N_LNG][7]
Definition: ctrydttm.c:56
Definition: ctry.h:106
LANGUAGE language
Definition: mbgstatus.c:125
char dt_sep
Definition: ctry.h:130
int snprintf_safe(char *s, size_t max_len, const char *fmt,...)
A portable, safe implementation of snprintf()
Definition: str_util.c:156
Definition: ctry.h:124
int snprint_ctry_wday(char *s, size_t max_len, int wday, LANGUAGE language)
Definition: ctrydttm.c:86
#define DAYS_PER_WEEK
Definition: ctrydttm.c:50
int snprint_02u(char *s, size_t max_len, unsigned int uc)
Definition: ctrydttm.c:68
int snprint_04u(char *s, size_t max_len, unsigned int us)
Definition: ctrydttm.c:77
int sn_cpy_char_safe(char *dst, size_t max_len, char c)
A function to copy a character safely to a string buffer.
Definition: str_util.c:306