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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/*
* month.c
*
* Copyright (c) Chris Putnam 2021
*
* Source code released under the GPL version 2
*/
#include <string.h>
#include "month.h"
typedef struct {
const char *text, *number;
} month_convert;
static month_convert full_months[] = {
{ "January", "01" },
{ "February", "02" },
{ "March", "03" },
{ "April", "04" },
{ "May", "05" },
{ "June", "06" },
{ "July", "07" },
{ "August", "08" },
{ "September", "09" },
{ "October", "10" },
{ "November", "11" },
{ "December", "12" },
};
static int nfull_months = sizeof( full_months ) / sizeof( full_months[0] );
static month_convert abbr_months[] = {
{ "Jan", "01" }, { "Jan.", "01" },
{ "Feb", "02" }, { "Feb.", "02" },
{ "Mar", "03" }, { "Mar.", "03" },
{ "Apr", "04" }, { "Apr.", "04" },
{ "May", "05" },
{ "Jun", "06" }, { "Jun.", "06" },
{ "Jul", "07" }, { "Jul.", "07" },
{ "Aug", "08" }, { "Aug.", "08" },
{ "Sep", "09" }, { "Sep.", "09" },
{ "Oct", "10" }, { "Oct.", "10" },
{ "Nov", "11" }, { "Nov.", "11" },
{ "Dec", "12" }, { "Dec.", "12" },
};
static int nabbr_months = sizeof( abbr_months ) / sizeof( abbr_months[0] );
/* month_to_number()
* convert month name to number in format MM, e.g. "January" -> "01"
* if output is the format "01"->"12", return 1
* otherwise return 0
*/
int
month_to_number( const char *in, const char **out )
{
int i;
for ( i=0; i<nfull_months; ++i ) {
if ( !strcasecmp( in, full_months[i].text ) ) {
*out = full_months[i].number;
return 1;
}
}
for ( i=0; i<nabbr_months; ++i ) {
if ( !strcasecmp( in, abbr_months[i].text ) ) {
*out = abbr_months[i].number;
return 1;
}
}
*out = in;
/* check to see if it's already in the format "01"->"12" */
for ( i=0; i<nfull_months; ++i ) {
if ( !strcmp( in, full_months[i].number ) ) return 1;
}
return 0;
}
int
number_to_full_month( const char *in, const char **out )
{
int i;
for ( i=0; i<nfull_months; ++i ) {
if ( !strcasecmp( in, full_months[i].number ) ) {
*out = full_months[i].text;
return 1;
}
}
*out = in;
return 0;
}
int
number_to_abbr_month( const char *in, const char **out )
{
int i;
for ( i=0; i<nabbr_months; ++i ) {
if ( !strcasecmp( in, abbr_months[i].number ) ) {
*out = abbr_months[i].text;
return 1;
}
}
*out = in;
return 0;
}
int
month_is_number( const char *in )
{
int i;
for ( i=0; i<nfull_months; ++i )
if ( !strcmp( in, full_months[i].number ) ) return 1;
return 0;
}
|