/*
   File:        timestring.h
   Author(s):   Andrew Moore
   Created:     Wed Oct 22 14:11:15 EDT 2003
   Description: Parse and manipulate times
   Copyright (c) Carnegie Mellon University
*/

#ifndef timestring_H
#define timestring_H

#include "standard.h"
#include "ambs.h"
#include "am_time.h"

typedef struct hourmin
{
  int hour;
  int minute;
} hourmin;

typedef struct dateandtime
{
  date *d;
  hourmin *hm;
} dateandtime;

/* Glossary:

    date    -   A structure for representing dates (year,month,day)
    daycode -   An integer index for dates such that 
                  daycode(tomorrow) = 1 + daycode(today)
               
                The date API is in am_time.h

    hourmin -   A structure for representing times (hour,min)
    hmcode  -   An integer index for hourmins such that 
                  hmcode(next minute) = 1 + hmcode(this minute)
                and hmcode(00:00) = 0 and hmcode(23:59) = 24 * 60 - 1

                The hourmin API is here in timestring.h

    dateandtime - A structure for representing date/time combination
                  (year,month,day,hour,min)
    mincode     - An integer index for minutes of dateandtime such that  
                  mincode(next minute) = 1 + mincode(this minute)
                  and which codes the number of minutes since a fixed time
                  in the early 19th century, so

                   mincode(00:00 tomorrow) = 1 + mincode(23:59 today)
*/


hourmin *mk_hourmin(int hour,int minute);
void free_hourmin(hourmin *x);
void fprintf_hourmin(FILE *s,char *m1,hourmin *x,char *m2);
void phourmin(hourmin *x);
hourmin *mk_copy_hourmin(hourmin *old);

/* Parses a time. A time must be 2 components separated by 
   "ignore characters" where "ignore characters" are :-/. and space

   Times must be in 24-hour clock. If needed someone should update this
   function to survive times such as 3.45pm

   If a parse error returns NULL, and MAKES and RETURNS an error message
   (which must later be free_string()'d in *r_errmess.

   If happy, returns hourmin, and sets *r_errmess to NULL.
*/
hourmin *mk_hourmin_from_string(char *s,char **r_errmess);

int hourmin_hour(hourmin *hm);
int hourmin_minute(hourmin *hm);
char *mk_string_from_hourmin(hourmin *hm);
int hourmin_to_hmcode(hourmin *hm);
hourmin *mk_hourmin_from_hmcode(int hmcode);

char *mk_string_from_hmcode(int hmcode);
int string_to_hmcode(char *string,char **r_errmess);
string_array *mk_string_array_from_hmcodes(ivec *hmcodes);

dateandtime *mk_dateandtime(date *d,hourmin *hm);
void free_dateandtime(dateandtime *x);
void fprintf_dateandtime(FILE *s,char *m1,dateandtime *x,char *m2);
void pdateandtime(dateandtime *x);
dateandtime *mk_copy_dateandtime(dateandtime *old);

/* Parses a date/time. The date must be parseable according to the
   rules described for 

   mk_date_from_string in am_time.h

   The time must be parseable using the rules described for

   mk_hourmin_from_string in timestring.h

   If a parse error returns NULL, and MAKES and RETURNS an error message
   (which must later be free_string()'d in *r_errmess.

   If happy, returns dateandtime, and sets *r_errmess to NULL.
*/
dateandtime *mk_dateandtime_from_strings(char *date_string,char *time_string,
					 char **r_errmess);

/* Just like mk_dateandtime_from_strings except my_error if there's
   a problem (with suitable error message) */
dateandtime *mk_dateandtime_from_strings_simple(char *date_string,
						char *time_string);

date *dateandtime_date(dateandtime *dt);
hourmin *dateandtime_hourmin(dateandtime *dt);
char *mk_string_from_dateandtime_date(dateandtime *dt);
char *mk_string_from_dateandtime_hourmin(dateandtime *dt);
char *mk_string_from_dateandtime(dateandtime *dt);

int daycode_hmcode_to_mincode(int daycode,int hmcode);
int dateandtime_to_mincode(dateandtime *dt);
int mincode_to_daycode(int mincode);
int mincode_to_hmcode(int mincode);
dateandtime *mk_dateandtime_from_mincode(int mincode);
int strings_to_mincode(char *date_string,char *time_string,char **r_errmess);
ivec *mk_daycodes_from_mincodes(ivec *mincodes);
ivec *mk_hmcodes_from_mincodes(ivec *mincodes);

dateandtime *mk_dateandtime_from_args(char *key,int argc,char *argv[],
				      char *default_date_string,
				      char *default_time_string);

ivec *mk_mincodes_from_daycodes_and_hmcodes(ivec *daycodes,ivec *hmcodes);

int dateandtime_to_day_of_week(dateandtime *dt);

char *mk_string_from_mincode(int mincode);

string_array *mk_string_array_from_mincodes(ivec *mincodes);



#endif /* ifndef timestring_H */
