Gammu internals  1.38.0
Date and time

Data Structures

struct  GSM_DateTime
 
struct  GSM_DeltaTime
 

Functions

int GetDayOfYear (unsigned int year, unsigned int month, unsigned int day)
 
int GetWeekOfMonth (unsigned int year, unsigned int month, unsigned int day)
 
int GetDayOfWeek (unsigned int year, unsigned int month, unsigned int day)
 
char * DayOfWeek (unsigned int year, unsigned int month, unsigned int day)
 
void GSM_GetCurrentDateTime (GSM_DateTime *Date)
 
time_t Fill_Time_T (GSM_DateTime DT)
 
int GSM_GetLocalTimezoneOffset (void)
 
void Fill_GSM_DateTime (GSM_DateTime *Date, time_t timet)
 
void GSM_DateTimeFromTimestamp (GSM_DateTime *Date, const char *str)
 
void GSM_DateTimeToTimestamp (GSM_DateTime *Date, char *str)
 
char * OSDateTime (GSM_DateTime dt, gboolean TimeZone)
 
char * OSDate (GSM_DateTime dt)
 
gboolean CheckDate (GSM_DateTime *date)
 
gboolean CheckTime (GSM_DateTime *date)
 

Detailed Description

Date and time handling.

Function Documentation

§ CheckDate()

gboolean CheckDate ( GSM_DateTime date)

Checks whether date is valid. This does not check time, see CheckTime for this.

Parameters
dateStructure where to check date.
Returns
True if date is correct.

Definition at line 349 of file misc.c.

References GSM_DateTime::Day, GSM_DateTime::Month, and GSM_DateTime::Year.

Referenced by GSM_DecodeSMSDateTime(), N71_65_DecodePhonebook(), ReadVCALDateTime(), and RecalcDateTime().

350 {
351  const int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
352 
353  if (date->Year != 0 &&
354  ((date->Year % 4 == 0 && date->Year % 100 != 0) || date->Year % 400 == 0) &&
355  date->Month == 2) {
356  return (date->Day <= 29);
357  }
358  return date->Year != 0 &&
359  date->Month >= 1 && date->Month <= 12 &&
360  date->Day >= 1 && date->Day <= days[date->Month-1];
361 }

§ CheckTime()

gboolean CheckTime ( GSM_DateTime date)

Checks whether time is valid. This does not check date, see CheckDate for this.

Parameters
dateStructure where to check time.
Returns
True if time is correct.

Definition at line 363 of file misc.c.

References GSM_DateTime::Hour, GSM_DateTime::Minute, and GSM_DateTime::Second.

Referenced by GSM_DecodeSMSDateTime(), ReadVCALDateTime(), and RecalcDateTime().

364 {
365  return date->Hour <= 23 &&
366  date->Minute <= 59 &&
367  date->Second <= 59;
368 }

§ DayOfWeek()

char* DayOfWeek ( unsigned int  year,
unsigned int  month,
unsigned int  day 
)

Returns string for current day of week.

Parameters
yearYear.
monthMonth.
dayDay.
Returns
Pointer to static buffer containing day of week string.

Return textual representation of day of week;

Definition at line 129 of file misc.c.

References GetDayOfWeek().

Referenced by dbg_vprintf().

130 {
131  static char DayOfWeekChar[10];
132 
133  strcpy(DayOfWeekChar,"");
134  switch (GetDayOfWeek(year, month, day)) {
135  case 0: strcpy(DayOfWeekChar,"Sun"); break;
136  case 1: strcpy(DayOfWeekChar,"Mon"); break;
137  case 2: strcpy(DayOfWeekChar,"Tue"); break;
138  case 3: strcpy(DayOfWeekChar,"Wed"); break;
139  case 4: strcpy(DayOfWeekChar,"Thu"); break;
140  case 5: strcpy(DayOfWeekChar,"Fri"); break;
141  case 6: strcpy(DayOfWeekChar,"Sat"); break;
142  }
143  return DayOfWeekChar;
144 }
int GetDayOfWeek(unsigned int year, unsigned int month, unsigned int day)
Definition: misc.c:117

§ Fill_GSM_DateTime()

void Fill_GSM_DateTime ( GSM_DateTime Date,
time_t  timet 
)

Converts time_t to gammu GSM_DateTime structure.

Parameters
DateStorage for date.
timetInput date.

Definition at line 170 of file misc.c.

References GSM_DateTime::Day, GSM_GetLocalTimezoneOffset(), GSM_DateTime::Hour, GSM_DateTime::Minute, GSM_DateTime::Month, GSM_DateTime::Second, GSM_DateTime::Timezone, and GSM_DateTime::Year.

Referenced by DUMMY_GetFolderListing(), DUMMY_GetNextFileFolder(), GetTimeDifference(), GSM_AddTime(), GSM_DateTimeFromTimestamp(), GSM_DecodeMMSFileToMultiPart(), GSM_GetCurrentDateTime(), GSM_ReadFile(), and ReadVCALDateTime().

171 {
172  struct tm *now;
173 
174  now = localtime(&timet);
175  Date->Year = now->tm_year + 1900;
176  Date->Month = now->tm_mon + 1;
177  Date->Day = now->tm_mday;
178  Date->Hour = now->tm_hour;
179  Date->Minute = now->tm_min;
180  Date->Second = now->tm_sec;
182 }
int GSM_GetLocalTimezoneOffset()
Definition: misc.c:146

§ Fill_Time_T()

time_t Fill_Time_T ( GSM_DateTime  DT)

Converts GSM_DateTime to time_t.

Parameters
DTInput timestamp.
Returns
time_t value.

Definition at line 189 of file misc.c.

References GSM_DateTime::Day, dbgprintf, GSM_DateTime::Hour, GSM_DateTime::Minute, GSM_DateTime::Month, OSDate(), GSM_DateTime::Second, and GSM_DateTime::Year.

Referenced by GetTimeDifference(), GSM_DateTimeToTimestamp(), GSM_SetCalendarRecurranceRepeat(), NOKIA_EncodeDateTime(), and ReadVCALDateTime().

190 {
191  struct tm timestruct;
192  struct tm *now;
193  time_t t;
194 
195 
196  dbgprintf(NULL, "StartTime: %s\n", OSDate(DT));
197 
198  memset(&timestruct, 0, sizeof(timestruct));
199  timestruct.tm_year = DT.Year - 1900;
200  timestruct.tm_mon = DT.Month - 1;
201  timestruct.tm_mday = DT.Day;
202  timestruct.tm_hour = DT.Hour;
203  timestruct.tm_min = DT.Minute;
204  timestruct.tm_sec = DT.Second;
205 
206  time(&t);
207  now = localtime(&t);
208 
209 #ifdef HAVE_DAYLIGHT
210  timestruct.tm_isdst = now->tm_isdst;
211 #else
212  timestruct.tm_isdst = -1;
213 #endif
214 #ifdef HAVE_STRUCT_TM_TM_ZONE
215  /* No time zone information */
216  timestruct.tm_gmtoff = now->tm_gmtoff;
217  timestruct.tm_zone = now->tm_zone;
218 #endif
219 
220  return mktime(&timestruct);
221 }
char * OSDate(GSM_DateTime dt)
Definition: misc.c:305
#define dbgprintf
Definition: debug.h:72

§ GetDayOfWeek()

int GetDayOfWeek ( unsigned int  year,
unsigned int  month,
unsigned int  day 
)

Return day of week index.

Definition at line 117 of file misc.c.

References RecalcDate().

Referenced by DayOfWeek(), GSM_DecodeVCAL_RRULE(), GSM_GetCalendarRecurranceRepeat(), GSM_SetCalendarRecurranceRepeat(), and OSDate().

118 {
119  struct tm st;
120 
121  RecalcDate(&st, year, month, day);
122 
123  return st.tm_wday;
124 }
int RecalcDate(struct tm *st, const int year, const int month, const int day)
Definition: misc.c:84

§ GetDayOfYear()

int GetDayOfYear ( unsigned int  year,
unsigned int  month,
unsigned int  day 
)

Return day of year index.

Definition at line 93 of file misc.c.

References RecalcDate().

Referenced by GSM_DecodeVCAL_RRULE().

94 {
95  struct tm st;
96 
97  RecalcDate(&st, year, month, day);
98 
99  return st.tm_yday;
100 }
int RecalcDate(struct tm *st, const int year, const int month, const int day)
Definition: misc.c:84

§ GetWeekOfMonth()

int GetWeekOfMonth ( unsigned int  year,
unsigned int  month,
unsigned int  day 
)

Return day of week index.

Definition at line 105 of file misc.c.

References RecalcDate().

Referenced by GSM_DecodeVCAL_RRULE().

106 {
107  struct tm st;
108 
109  RecalcDate(&st, year, month, day);
110 
111  return 1 + (day - st.tm_wday) / 7;
112 }
int RecalcDate(struct tm *st, const int year, const int month, const int day)
Definition: misc.c:84

§ GSM_DateTimeFromTimestamp()

void GSM_DateTimeFromTimestamp ( GSM_DateTime Date,
const char *  str 
)

Converts string (seconds since epoch) to gammu GSM_DateTime structure.

Parameters
DateStorage for date.
strInput date.

Definition at line 162 of file misc.c.

References Fill_GSM_DateTime().

163 {
164  time_t timet;
165 
166  timet = atof(str);
167  Fill_GSM_DateTime(Date, timet);
168 }
void Fill_GSM_DateTime(GSM_DateTime *Date, time_t timet)
Definition: misc.c:170

§ GSM_DateTimeToTimestamp()

void GSM_DateTimeToTimestamp ( GSM_DateTime Date,
char *  str 
)

Converts gammu GSM_DateTime structure to string (seconds since epoch).

Parameters
DateDate.
strStrorage for string.

Definition at line 155 of file misc.c.

References Fill_Time_T().

156 {
157  time_t timet;
158  timet = Fill_Time_T(*Date);
159  sprintf(str, "%ld", (long)timet);
160 }
time_t Fill_Time_T(GSM_DateTime DT)
Definition: misc.c:189

§ GSM_GetCurrentDateTime()

void GSM_GetCurrentDateTime ( GSM_DateTime Date)

Returns current timestamp.

Parameters
DateStorage for date time structure.

Definition at line 184 of file misc.c.

References Fill_GSM_DateTime().

Referenced by dbg_vprintf(), DUMMY_GetAlarm(), DUMMY_GetDateTime(), GSM_EncodeEMSMultiPartSMS(), GSM_InitConnection_Log(), GSM_IsCalendarNoteFromThePast(), GSM_MakeMultiPartSMS(), GSM_MakeSMSIDFromTime(), GSM_ReadDevice(), GSM_SetDefaultSMSData(), and NOKIA_EncodeDateTime().

185 {
186  Fill_GSM_DateTime(Date, time(NULL));
187 }
void Fill_GSM_DateTime(GSM_DateTime *Date, time_t timet)
Definition: misc.c:170

§ GSM_GetLocalTimezoneOffset()

int GSM_GetLocalTimezoneOffset ( void  )

Returns the local timezone offset in seconds. For example 7200 for CEST.

Returns
Timezone offset seconds.

Definition at line 146 of file misc.c.

Referenced by Fill_GSM_DateTime().

146  {
147  struct tm tg, tl;
148  time_t now = time(NULL);
149  gmtime_r(&now, &tg);
150  localtime_r(&now, &tl);
151  // Returns offset including daylight saving (found as boolean in tl.tm_isdst).
152  return (int)(mktime(&tl) - mktime(&tg));
153 }

§ OSDate()

char* OSDate ( GSM_DateTime  dt)

Converts date from timestamp to string according to OS settings.

Parameters
dtInput timestamp.
Returns
Pointer to static buffer containing string.

Definition at line 305 of file misc.c.

References GSM_DateTime::Day, GetDayOfWeek(), GSM_DateTime::Hour, GSM_DateTime::Minute, GSM_DateTime::Month, GSM_DateTime::Second, and GSM_DateTime::Year.

Referenced by Fill_Time_T(), GSM_EncodeSMSDateTime(), GSM_ReadFile(), and ReadVCALDate().

306 {
307  struct tm timeptr;
308  static char retval[200],retval2[200];
309 
310 #ifdef WIN32
311  setlocale(LC_ALL, ".OCP");
312 #endif
313 
314  timeptr.tm_yday = 0; /* FIXME */
315  timeptr.tm_isdst = -1; /* FIXME */
316  timeptr.tm_year = dt.Year - 1900;
317  timeptr.tm_mon = dt.Month - 1;
318  timeptr.tm_mday = dt.Day;
319  timeptr.tm_hour = dt.Hour;
320  timeptr.tm_min = dt.Minute;
321  timeptr.tm_sec = dt.Second;
322  timeptr.tm_wday = GetDayOfWeek(dt.Year, dt.Month, dt.Day);
323 #ifdef HAVE_STRUCT_TM_TM_ZONE
324  timeptr.tm_zone = NULL;
325 #endif
326 
327  /* This is not Y2K safe */
328  strftime(retval2, 200, "%x", &timeptr);
329 
330  /* If don't have weekday name, include it */
331  strftime(retval, 200, "%A", &timeptr);
332  if (strstr(retval2,retval)==NULL) {
333  /* Check also for short name */
334  strftime(retval, 200, "%a", &timeptr);
335  if (strstr(retval2,retval)==NULL) {
336  strcat(retval2," (");
337  strcat(retval2,retval);
338  strcat(retval2,")");
339  }
340  }
341 
342 #ifdef WIN32
343  setlocale(LC_ALL, ".ACP");
344 #endif
345 
346  return retval2;
347 }
int GetDayOfWeek(unsigned int year, unsigned int month, unsigned int day)
Definition: misc.c:117

§ OSDateTime()

char* OSDateTime ( GSM_DateTime  dt,
gboolean  TimeZone 
)

Converts timestamp to string according to OS settings.

Parameters
dtInput timestamp.
TimeZoneWhether to include time zone.
Returns
Pointer to static buffer containing string.

Definition at line 264 of file misc.c.

References GSM_DateTime::Day, GSM_DateTime::Hour, GSM_DateTime::Minute, GSM_DateTime::Month, RecalcDateTime(), GSM_DateTime::Second, GSM_DateTime::Timezone, and GSM_DateTime::Year.

Referenced by GSM_DecodeMMSFileToMultiPart(), and GSM_DecodeSMSDateTime().

265 {
266  struct tm timeptr;
267  static char retval[200],retval2[200];
268 
269  if (!RecalcDateTime(&timeptr, dt.Year, dt.Month, dt.Day,
270  dt.Hour, dt.Minute, dt.Second)) {
271  retval2[0] = '\0';
272  return retval2;
273  }
274 
275 #ifdef WIN32
276  setlocale(LC_ALL, ".OCP");
277 #endif
278 
279  /* This is not Y2K safe */
280  strftime(retval2, 200, "%c", &timeptr);
281  if (TimeZone) {
282  snprintf(retval, sizeof(retval) - 1, " %+03i%02i",
283  dt.Timezone / 3600, abs((dt.Timezone % 3600) / 60));
284  strcat(retval2,retval);
285  }
286  /* If don't have weekday name, include it */
287  strftime(retval, 200, "%A", &timeptr);
288  if (strstr(retval2,retval)==NULL) {
289  /* Check for abbreviated weekday */
290  strftime(retval, 200, "%a", &timeptr);
291  if (strstr(retval2,retval)==NULL) {
292  strcat(retval2," (");
293  strcat(retval2,retval);
294  strcat(retval2,")");
295  }
296  }
297 
298 #ifdef WIN32
299  setlocale(LC_ALL, ".ACP");
300 #endif
301 
302  return retval2;
303 }
int RecalcDateTime(struct tm *st, const int year, const int month, const int day, const int hour, const int minute, const int second)
Definition: misc.c:38