MUS177/206 – seconds.c

Here is seconds.c, which uses <time.h> functions to send the day and time out as a list (out of a single list outlet).


 

/* seconds.c - 
 this object has 1 inlets and 6 outlets
 it responds the 'bang' message in the left inlet
 it responds to the 'assistance' message sent by Max when the mouse is positioned over an inlet or outlet
 (including an assistance method is optional, but strongly sugggested)
*/
#include <time.h>
#include "m_pd.h"

typedef struct _seconds // defines our object's internal variables for each instance in a patch
{
    t_object x_ob;
    t_outlet *x_outlist;
    t_atom date[6];
} t_seconds;

t_class *seconds_class; // global pointer to the object class - so max can reference the object 
// these are prototypes for the methods that are defined below
void seconds_bang(t_seconds *x);
void *seconds_new(void);
void seconds_setup(void);

//--------------------------------------------------------------------------
void seconds_setup(void)
{
    seconds_class = class_new(gensym("seconds"), (t_newmethod)seconds_new, 0, sizeof(t_seconds), 0, 0);
    class_addbang(seconds_class, seconds_bang);// the method it uses when it gets a bang in the left inlet 
}

//--------------------------------------------------------------------------
void seconds_bang(t_seconds *x) // x = reference to this instance of the object 
{
    time_t rawTime;
        float year, month, date, hour, min, sec;
        struct tm *now;

    time(&rawTime);
    now = gmtime(&rawTime);
    year = (now->tm_year + 1900);
    month = (now->tm_mon + 1);
    date = (now->tm_mday);
    hour = now->tm_hour;
    min = now->tm_min;
    sec = now->tm_sec;
    SETFLOAT(x->date+0, year);
    SETFLOAT(x->date+1, month);
    SETFLOAT(x->date+2, date);
    SETFLOAT(x->date+3, hour);
    SETFLOAT(x->date+4, min);
    SETFLOAT(x->date+5, sec);
    outlet_list(x->x_outlist, gensym("list"), 6, (x->date));
}

//--------------------------------------------------------------------------
void *seconds_new(void) // n = int argument typed into object box (A_DEFLONG) -- defaults to 0 if no args are typed
{
    t_seconds *x;// local variable (pointer to a t_seconds data structure        x = (t_seconds *)pd_new(seconds_class); // create a new instance of this object
       x->x_outlist = outlet_new(&x->x_ob, gensym("list"));
      return(x); // return a reference to the object instance 
}