// // Copyright: Copyright 2003 Craig Stuart Sapp // Programmer: Craig Stuart Sapp // Creation Date: Mon Feb 3 05:02:47 PST 2003 // Last Modified: Tue Feb 4 06:11:13 PST 2003 // Filename: localtime.c // Web Address: http://peabody.sapp.org/class/dmp2/lab/localtime/localtime.c // Syntax: C; Max4/MSP2 External Object; CodeWarrior 6.0 // OS: Mac OS 9; PPC // // Description: Extract the system clock time. // #include "ext.h" #include /* included for access to localtime() function */ typedef struct { t_object max_data; // Max/MSP data, MUST come first in struct void* outputSec; // output seconds (0-59) void* outputMin; // output minutes (0-59) void* outputHour; // output hours (0-23) void* outputDay; // output day of month (1-31) void* outputMonth; // output month (1-12) void* outputYear; // output year (e.g.: 2003) void* outputWday; // output day of week (0=sunday, 1=monday, etc) void* outputYday; // output day of year (Jan 1 = 0, etc.) } MyObject; void* object_data = NULL; // function declarations: void* create_object (void); void InputEpochTime (MyObject* mo, long timeinsec); void InputBang (MyObject* mo); void OutputData (MyObject* mo, struct tm* timestruct); ///////////////////////////////////////////////////////////////////////// // // Initialization functions // ////////////////////////////// // // main -- called once when the object is created in a patcher window. // void main(void) { setup((t_messlist**)&object_data, (method)create_object, NULL, sizeof(MyObject), NULL, A_NOTHING); addint ((method)InputEpochTime); addbang((method)InputBang); } ////////////////////////////// // // create_object -- create the data storage for the object. // void* create_object(void) { MyObject *mo; mo = (MyObject*)newobject(object_data); mo->outputYday = intout(mo); mo->outputWday = intout(mo); mo->outputYear = intout(mo); mo->outputMonth = intout(mo); mo->outputDay = intout(mo); mo->outputHour = intout(mo); mo->outputMin = intout(mo); mo->outputSec = intout(mo); return mo; } ///////////////////////////////////////////////////////////////////////// // // Behavior functions // ////////////////////////////// // // InputEpochTime -- process the input number as the time in // seconds since the date Jan 1, 1970. // void InputEpochTime(MyObject* mo, long timeinsec) { time_t currenttime = timeinsec; struct tm* timestruct = localtime(¤ttime); OutputData(mo, timestruct); } ////////////////////////////// // // InputBang -- behavior of the object when a "bang" message is received. // void InputBang(MyObject* mo) { time_t currenttime = time(NULL); // get the current time in // seconds since Jan 1, 1970. struct tm* timestruct = localtime(¤ttime); OutputData(mo, timestruct); } ////////////////////////////// // // OutputData -- output the time struct. // void OutputData(MyObject* mo, struct tm* timestruct) { outlet_int(mo->outputSec, timestruct->tm_sec); outlet_int(mo->outputMin, timestruct->tm_min); outlet_int(mo->outputHour, timestruct->tm_hour); outlet_int(mo->outputDay, timestruct->tm_mday); outlet_int(mo->outputMonth, timestruct->tm_mon); outlet_int(mo->outputYear, timestruct->tm_year + 1900); outlet_int(mo->outputWday, timestruct->tm_wday); outlet_int(mo->outputYday, timestruct->tm_yday); }