mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 08:46:42 +08:00
224 lines
4.8 KiB
Markdown
224 lines
4.8 KiB
Markdown
## 介绍
|
||
`date`是一个用于C语言的简单日期计算模块,它为开发者提供了诸多实用的功能,涵盖了日期合法性验证、闰年判断、日期差值计算、基于日期获取对应星期数以及展示指定年月的日历等多方面的日期相关操作,方便在C语言项目中高效处理各种与日期有关的业务逻辑。
|
||
|
||
## 接口
|
||
|
||
### 函数
|
||
|
||
#### `date_isleap`函数
|
||
```c
|
||
uint8_t date_isleap(uint16_t year);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint16_t year = 2024;
|
||
uint8_t result = date_isleap(year);
|
||
if (result == 1)
|
||
{
|
||
printf("%d is a leap year.\n", year);
|
||
}
|
||
else
|
||
{
|
||
printf("%d is not a leap year.\n", year);
|
||
}
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_isvalid`函数
|
||
```c
|
||
uint8_t date_isvalid(DATE date);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
DATE date = DATE(2024, 2, 29); // 假设一个日期示例
|
||
uint8_t result = date_isvalid(date);
|
||
if (result == 1) {
|
||
printf("The date is valid.\n");
|
||
} else {
|
||
printf("The date is invalid.\n");
|
||
}
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_century_days`函数
|
||
```c
|
||
uint32_t date_century_days(uint16_t century);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint16_t century = 21;
|
||
uint32_t result = date_century_days(century);
|
||
printf("The number of days in the %d century is: %u\n", century, result);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_year_days`函数
|
||
```c
|
||
uint32_t date_year_days(uint16_t year);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint16_t year = 2024;
|
||
uint32_t result = date_year_days(year);
|
||
printf("The number of days in the year %d is: %u\n", year, result);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_month_days`函数
|
||
```c
|
||
uint32_t date_month_days(uint16_t year, uint8_t month);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint16_t year = 2024;
|
||
uint8_t month = 2;
|
||
uint32_t result = date_month_days(year, month);
|
||
printf("The number of days in month %d of year %d is: %u\n", month, year, result);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_get_dow`函数
|
||
```c
|
||
uint32_t date_get_dow(DATE date);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
DATE date = DATE(2024, 12, 31);
|
||
uint32_t result = date_get_dow(date);
|
||
switch (result) {
|
||
case 1:
|
||
printf("The day of week is Monday.\n");
|
||
break;
|
||
case 2:
|
||
printf("The day of week is Tuesday.\n");
|
||
break;
|
||
case 3:
|
||
printf("The day of week is Wednesday.\n");
|
||
break;
|
||
case 4:
|
||
printf("The day of week is Thursday.\n");
|
||
break;
|
||
case 5:
|
||
printf("The day of week is Friday.\n");
|
||
break;
|
||
case 6:
|
||
printf("The day of week is Saturday.\n");
|
||
break;
|
||
case 7:
|
||
printf("The day of week is Sunday.\n");
|
||
break;
|
||
default:
|
||
printf("The input date is invalid.\n");
|
||
}
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_current_days`函数
|
||
```c
|
||
uint32_t date_current_days(DATE date);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
DATE date = DATE(2024, 12, 31);
|
||
uint32_t result = date_current_days(date);
|
||
printf("The total number of days from the base date to the given date is: %u\n", result);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_diff_days`函数
|
||
```c
|
||
int32_t date_diff_days(DATE date1, DATE date2);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
DATE date1 = DATE(2024, 1, 1);
|
||
DATE date2 = DATE(2024, 12, 31);
|
||
int32_t result = date_diff_days(date1, date2);
|
||
printf("The difference in days between the two dates is: %d\n", result);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_from_days`函数
|
||
```c
|
||
DATE date_from_days(uint32_t days);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint32_t days = 366; // 假设对应天数
|
||
DATE result = date_from_days(days);
|
||
printf("The date converted from %u days is: %d-%d-%d\n", days, result.year, result.month, result.day);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_offset`函数
|
||
```c
|
||
DATE date_offset(DATE date, int32_t days);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
DATE date = DATE(2024, 1, 1);
|
||
int32_t days = 365;
|
||
DATE result = date_offset(date, days);
|
||
printf("The offset date is: %d-%d-%d\n", result.year, result.month, result.day);
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
#### `date_calendar`函数
|
||
```c
|
||
void date_calendar(uint16_t year, uint8_t month);
|
||
```
|
||
使用例子:
|
||
```c
|
||
int test()
|
||
{
|
||
uint16_t year = 2024;
|
||
uint8_t month = 12;
|
||
date_calendar(year, month);
|
||
return 0;
|
||
}
|
||
```
|
||
结果:
|
||
```
|
||
+-----------------------------------------+
|
||
| 2024/12 |
|
||
+-----------------------------------------+
|
||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|
||
+-----------------------------------------+
|
||
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
||
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|
||
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
|
||
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
|
||
| 29 | 30 | 31 | | | | |
|
||
+-----------------------------------------+
|
||
```
|