swad-core/swad_date.h

244 lines
8.9 KiB
C
Raw Permalink Normal View History

2014-12-01 23:55:08 +01:00
// swad_date.h: dates
#ifndef _SWAD_DAT
#define _SWAD_DAT
/*
SWAD (Shared Workspace At a Distance in Spanish),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2024 Antonio Ca<EFBFBD>as Vargas
2014-12-01 23:55:08 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
2015-10-16 02:24:29 +02:00
#include <stdbool.h> // For boolean type
#include <stdio.h> // For FILE *
2014-12-01 23:55:08 +01:00
#include <time.h>
2017-05-05 02:03:28 +02:00
#include "swad_constant.h"
#include "swad_HTML.h"
2017-05-05 02:03:28 +02:00
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Public constants ******************************/
/*****************************************************************************/
2017-01-28 15:58:46 +01:00
#define Dat_SECONDS_IN_ONE_MONTH (30UL * 24UL * 60UL * 60UL)
2014-12-01 23:55:08 +01:00
2015-10-28 21:47:42 +01:00
#define Dat_MAX_BYTES_TIME_ZONE 256
2018-10-18 02:02:32 +02:00
#define Dat_MAX_BYTES_TIME (128 - 1)
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/******************************* Public types ********************************/
/*****************************************************************************/
2017-01-13 01:51:23 +01:00
#define Dat_LENGTH_YYYYMMDD (4 + 2 + 2)
struct Dat_Date
2014-12-01 23:55:08 +01:00
{
2016-06-01 19:34:44 +02:00
unsigned Day;
unsigned Month;
unsigned Year;
unsigned Week;
2017-01-13 01:51:23 +01:00
char YYYYMMDD[Dat_LENGTH_YYYYMMDD + 1];
2014-12-01 23:55:08 +01:00
};
struct Dat_Time
2014-12-01 23:55:08 +01:00
{
2016-06-01 19:34:44 +02:00
unsigned Hour;
unsigned Minute;
unsigned Second;
2014-12-01 23:55:08 +01:00
};
struct Dat_Hour
2014-12-01 23:55:08 +01:00
{
2016-06-01 19:34:44 +02:00
unsigned Hour;
unsigned Minute;
2014-12-01 23:55:08 +01:00
};
struct Dat_DateTime
2014-12-01 23:55:08 +01:00
{
struct Dat_Date Date;
struct Dat_Time Time;
2017-01-28 15:58:46 +01:00
char YYYYMMDDHHMMSS[4 + 2 + 2 + 2 + 2 + 2 + 1];
2014-12-01 23:55:08 +01:00
};
2016-12-02 10:45:53 +01:00
#define Dat_NUM_TIME_STATUS 3
typedef enum
{
Dat_PAST = 0,
Dat_PRESENT = 1,
Dat_FUTURE = 2,
} Dat_TimeStatus_t;
2016-12-15 00:39:52 +01:00
#define Dat_NUM_START_END_TIME 2
typedef enum
{
Dat_STR_TIME = 0,
Dat_END_TIME = 1,
2016-12-15 00:39:52 +01:00
} Dat_StartEndTime_t;
#define Dat_NUM_FORM_SECONDS 2
2016-12-03 20:08:01 +01:00
typedef enum
{
Dat_FORM_SECONDS_OFF,
Dat_FORM_SECONDS_ON,
} Dat_FormSeconds;
#define Dat_NUM_SET_HMS 3
2017-02-26 20:09:21 +01:00
typedef enum
{
Dat_HMS_DO_NOT_SET = 0,
Dat_HMS_TO_000000 = 1,
Dat_HMS_TO_235959 = 2,
} Dat_SetHMS;
2017-05-04 02:19:23 +02:00
/***** Date format *****/
#define Dat_NUM_OPTIONS_FORMAT 3
typedef enum
{
Dat_FORMAT_YYYY_MM_DD = 0, // ISO 8601, default
Dat_FORMAT_DD_MONTH_YYYY = 1,
Dat_FORMAT_MONTH_DD_YYYY = 2,
2017-05-04 11:03:44 +02:00
} Dat_Format_t; // Do not change these numbers because they are used in database
2017-05-04 02:19:23 +02:00
#define Dat_FORMAT_DEFAULT Dat_FORMAT_YYYY_MM_DD
#define Dat_NUM_SEPARATORS 3
2019-11-02 12:10:58 +01:00
typedef enum
{
Dat_SEPARATOR_NONE, // No separator
Dat_SEPARATOR_COMMA, // Comma + space
Dat_SEPARATOR_BREAK, // Line break
} Dat_Separator_t;
typedef unsigned Dat_WhatToWrite_t;
#define Dat_WRITE_TODAY ((Dat_WhatToWrite_t) (1 << 5))
#define Dat_WRITE_DATE_ON_SAME_DAY ((Dat_WhatToWrite_t) (1 << 4))
#define Dat_WRITE_WEEK_DAY ((Dat_WhatToWrite_t) (1 << 3))
#define Dat_WRITE_HOUR ((Dat_WhatToWrite_t) (1 << 2))
#define Dat_WRITE_MINUTE ((Dat_WhatToWrite_t) (1 << 1))
#define Dat_WRITE_SECOND ((Dat_WhatToWrite_t) (1 << 0))
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Public prototypes *****************************/
/*****************************************************************************/
void Dat_SetStartExecutionTimeval (void);
void Dat_SetStartExecutionTimeUTC (void);
time_t Dat_GetStartExecutionTimeUTC (void);
struct Dat_Date *Dat_GetCurrentDate (void);
unsigned Dat_GetCurrentDay (void);
unsigned Dat_GetCurrentMonth (void);
unsigned Dat_GetCurrentYear (void);
long Dat_GetTimeGenerationInMicroseconds (void);
long Dat_GetTimeSendInMicroseconds (void);
void Dat_ComputeTimeToGeneratePage (void);
void Dat_ComputeTimeToSendPage (void);
void Dat_WriteTimeToGenerateAndSendPage (void);
void Dat_WriteTime (char Str[Dat_MAX_BYTES_TIME],long TimeInMicroseconds);
void Dat_ResetDate (struct Dat_Date *Date);
void Dat_ResetHour (struct Dat_Hour *Hour);
2020-05-07 19:31:58 +02:00
2017-05-07 18:06:34 +02:00
void Dat_PutBoxToSelectDateFormat (void);
2017-05-04 23:27:51 +02:00
void Dat_PutSpanDateFormat (Dat_Format_t Format);
void Dat_PutScriptDateFormat (Dat_Format_t Format);
2017-05-04 02:19:23 +02:00
void Dat_ChangeDateFormat (void);
2017-05-04 11:03:44 +02:00
Dat_Format_t Dat_GetDateFormatFromStr (const char *Str);
2017-05-04 02:19:23 +02:00
2014-12-01 23:55:08 +01:00
void Dat_GetAndConvertCurrentDateTime (void);
2015-10-22 01:24:43 +02:00
time_t Dat_GetUNIXTimeFromStr (const char *Str);
bool Dat_GetDateFromYYYYMMDD (struct Dat_Date *Date,const char *YYYYMMDDString);
2014-12-01 23:55:08 +01:00
2015-10-22 01:24:43 +02:00
void Dat_ShowClientLocalTime (void);
2014-12-01 23:55:08 +01:00
2016-12-11 19:30:42 +01:00
struct tm *Dat_GetLocalTimeFromClock (const time_t *timep);
void Dat_ConvDateToDateStr (const struct Dat_Date *Date,char StrDate[Cns_MAX_BYTES_DATE + 1]);
2014-12-01 23:55:08 +01:00
time_t Dat_GetRangeTimeUTC (Dat_StartEndTime_t StartEndTime);
struct Dat_Date *Dat_GetRangeDate (Dat_StartEndTime_t StartEndTime);
2020-02-26 19:35:33 +01:00
void Dat_PutFormStartEndClientLocalDateTimesWithYesterdayToday (const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME]);
2020-03-03 00:56:22 +01:00
void Dat_PutFormStartEndClientLocalDateTimes (const time_t TimeUTC[Dat_NUM_START_END_TIME],
2020-02-26 19:35:33 +01:00
Dat_FormSeconds FormSeconds,
const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME]);
2015-10-22 01:24:43 +02:00
2015-10-26 20:02:07 +01:00
void Dat_WriteFormClientLocalDateTimeFromTimeUTC (const char *Id,
Dat_StartEndTime_t StartEndTime,
2015-10-26 20:02:07 +01:00
time_t TimeUTC,
2016-12-03 20:08:01 +01:00
unsigned FirstYear,
unsigned LastYear,
Dat_FormSeconds FormSeconds,
2017-02-26 20:09:21 +01:00
Dat_SetHMS SetHMS,
HTM_SubmitOnChange_t SubmitOnChange);
time_t Dat_GetTimeUTCFromForm (Dat_StartEndTime_t StartEndTime);
2015-10-22 01:24:43 +02:00
void Dat_PutParBrowserTZDiff (void);
2017-01-28 15:58:46 +01:00
void Dat_GetBrowserTimeZone (char BrowserTimeZone[Dat_MAX_BYTES_TIME_ZONE + 1]);
2015-10-27 22:59:49 +01:00
2014-12-01 23:55:08 +01:00
void Dat_WriteFormDate (unsigned FirstYear,unsigned LastYear,
2015-10-23 01:06:32 +02:00
const char *Id,
struct Dat_Date *DateSelected,
HTM_SubmitOnChange_t SubmitOnChange,
Cns_Disabled_t Disabled);
void Dat_GetDateFromForm (const char *ParNameDay,const char *ParNameMonth,const char *ParNameYear,
2014-12-01 23:55:08 +01:00
unsigned *Day,unsigned *Month,unsigned *Year);
void Dat_SetDatesToPastNow (void);
void Dat_SetDatesToRecWeeks (void);
void Dat_WriteParsIniEndDates (void);
2014-12-01 23:55:08 +01:00
void Dat_GetIniEndDatesFromForm (void);
void Dat_WriteRFC822DateFromTM (FILE *File,struct tm *tm);
void Dat_GetDateAfter (struct Dat_Date *Date,struct Dat_Date *SubsequentDate);
void Dat_GetDateBefore (struct Dat_Date *Date,struct Dat_Date *PrecedingDate );
void Dat_GetWeekBefore (struct Dat_Date *Date,struct Dat_Date *PrecedingDate );
void Dat_GetMonthBefore (struct Dat_Date *Date,struct Dat_Date *PrecedingDate );
void Dat_GetYearBefore (struct Dat_Date *Date,struct Dat_Date *PrecedingDate );
unsigned Dat_GetNumDaysBetweenDates (struct Dat_Date *DateStr,
struct Dat_Date *DateEnd);
unsigned Dat_GetNumWeeksBetweenDates (struct Dat_Date *DateStr,
struct Dat_Date *DateEnd);
unsigned Dat_GetNumMonthsBetweenDates (struct Dat_Date *DateStr,
struct Dat_Date *DateEnd);
unsigned Dat_GetNumYearsBetweenDates (struct Dat_Date *DateStr,
struct Dat_Date *DateEnd);
2014-12-01 23:55:08 +01:00
unsigned Dat_GetNumDaysInYear (unsigned Year);
unsigned Dat_GetNumDaysFebruary (unsigned Year);
bool Dat_GetIfLeapYear (unsigned Year);
unsigned Dat_GetNumWeeksInYear (unsigned Year);
unsigned Dat_GetDayOfWeek (unsigned Year,unsigned Month,unsigned Day);
unsigned Dat_GetDayOfYear (struct Dat_Date *Date);
void Dat_CalculateWeekOfYear (struct Dat_Date *Date);
void Dat_AssignDate (struct Dat_Date *DateDst,struct Dat_Date *DateSrc);
2014-12-01 23:55:08 +01:00
2015-10-27 19:44:31 +01:00
void Dat_WriteScriptMonths (void);
2019-01-11 12:32:41 +01:00
void Dat_WriteHoursMinutesSecondsFromSeconds (time_t Seconds);
void Dat_WriteHoursMinutesSeconds (struct Dat_Time *Time);
2019-01-11 12:32:41 +01:00
2019-11-01 23:35:55 +01:00
void Dat_WriteLocalDateHMSFromUTC (const char *Id,time_t TimeUTC,
2019-11-02 12:10:58 +01:00
Dat_Format_t DateFormat,Dat_Separator_t Separator,
Dat_WhatToWrite_t Write);
2019-11-01 17:35:26 +01:00
//-------------------------------- Figures ------------------------------------
void Dat_GetAndShowNumUsrsPerDateFormat (void);
2014-12-01 23:55:08 +01:00
#endif