Jump to content
43oh

UTC time to calendar time


Recommended Posts

I'm trying to figure out to convert UTC time, which the time in seconds since midnight January 1, 1970, to calendar time and vice versa, but I can't quite figure out the algorithm. I'm using an MSP430F149 with a 32.768kHz ACLK to count seconds.

 

I'm sure someone has figured this out before. Know where I can find it?

 

edit: When i say calendar time i mean in this format: Year, Month, Day of the week, date, hour, minutes, seconds.

Link to post
Share on other sites

You might want to look at time.h(if available, if not i would try including it somehow), specifically time_t(cplusplus[dot]com/reference/clibrary/ctime/time_t/).

 

You will have to count that variable manually, but look at the functions that consume that type, there's a few conversion ones that could help.

Link to post
Share on other sites

I see how time.h can turn unix time to local time, but can it do local time to unix time, where send it the day year min hours etc.. and it returns the unix time.

 

This is how how I count seconds using the 32.768kHz ACLK. Timer B is set up to trigger an interrupt every 1 second and it increments the unix timer. I have a gui that the user will enter the current local time in and the microprocessor would convert it to unix time to make triggering timed events easier.

 

I already know the easy part of find the current seconds of the day just by doing the code below:

seconds = UTC_count % 60;

 

It counts seconds using the code below:

//variable declaration
unsigned long UTC_Count=0;

//Timer B Interrupt Routine
{
UTC_Count++;
return;
}

Link to post
Share on other sites
struct tm bt;
bt.tm_hour = 12;	// Hour
bt.tm_min = 0;		// Minute
bt.tm_sec = 0;		// Second
bt.tm_mon = 7;		// Month
bt.tm_mday = 17;	// Day
bt.tm_year = 2011;	// Year
bt.tm_isdst = 0;	// DST
bt.tm_wday = 0;		// Day of week
bt.tm_yday = 0;		// Day of year

time_t seconds = mktime(&bt);  // Convert time/date to seconds

Link to post
Share on other sites
struct tm bt;
bt.tm_hour = 12;	// Hour
bt.tm_min = 0;		// Minute
bt.tm_sec = 0;		// Second
bt.tm_mon = 7;		// Month
bt.tm_mday = 17;	// Day
bt.tm_year = 2011;	// Year
bt.tm_isdst = 0;	// DST
bt.tm_wday = 0;		// Day of week
bt.tm_yday = 0;		// Day of year

time_t seconds = mktime(&bt);  // Convert time/date to seconds

 

ok, sorry, but I'm new the C language, but what does the bt. and struct tm bt mean?

Is time_t unix time?

 

I found the header file in CSS.

 

/*****************************************************************************/
/* time.h     v5.2.10                                                         */
/* Copyright (c) 1990-2011  Texas Instruments Incorporated                   */
/*****************************************************************************/

#ifndef _TIME
#define _TIME

#include 


 #define CLOCKS_PER_SEC 1000

#ifndef NULL
#define NULL      0
#endif

#ifdef __cplusplus
extern "C" namespace std {
#endif

typedef unsigned long clock_t;
typedef unsigned long time_t;

#ifndef _SIZE_T
#define _SIZE_T
typedef __SIZE_T_TYPE__ size_t;
#endif

struct tm 
{
   int tm_sec;      /* seconds after the minute   - [0,59]  */
   int tm_min;      /* minutes after the hour     - [0,59]  */
   int tm_hour;     /* hours after the midnight   - [0,23]  */
   int tm_mday;     /* day of the month           - [1,31]  */
   int tm_mon;      /* months since January       - [0,11]  */
   int tm_year;     /* years since 1900                     */
   int tm_wday;     /* days since Sunday          - [0,6]   */
   int tm_yday;     /* days since Jan 1st         - [0,365] */
   int tm_isdst;    /* Daylight Savings Time flag           */

};

/*************************************************************************/
/* TIME ZONE STRUCTURE DEFINITION                                        */
/*************************************************************************/
typedef struct 
{
   short daylight;
   long  timezone;
   char  tzname[4];
   char  dstname[4];
} TZ;

extern _DATA_ACCESS TZ _tz;

/****************************************************************************/
/* FUNCTION DECLARATIONS.  (NOTE : clock AND time ARE SYSTEM SPECIFIC)      */
/****************************************************************************/
_CODE_ACCESS clock_t    clock(void);             
_CODE_ACCESS time_t     time(time_t *_timer);   

_CODE_ACCESS time_t     mktime(struct tm *_tptr);
_CODE_ACCESS double     difftime(time_t _time1, time_t _time0);
     _IDECL char      *ctime(const time_t *_timer);
_CODE_ACCESS char      *asctime(const struct tm *_timeptr);
_CODE_ACCESS struct tm *gmtime(const time_t *_timer);
_CODE_ACCESS struct tm *localtime(const time_t *_timer);
_CODE_ACCESS size_t     strftime(char *_out, size_t _maxsize, 
			 const char *_format,
			 const struct tm *_timeptr);

#if defined(_INLINE) || defined(_CTIME_IMPLEMENTATION)

_IDEFN char *ctime(const time_t *timer)
{
  return(asctime(localtime(timer)));
}

#endif /* _INLINE || _CTIME_IMPLEMENTATION */

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif /* _TIME */

#if defined(__cplusplus) && !defined(_CPP_STYLE_HEADER)
using std::clock_t;
using std::time_t;
using std::size_t;
using std::tm;
using std::TZ;
using std::_tz;
using std::clock;
using std::time;
using std::mktime;
using std::difftime;
using std::ctime;
using std::asctime;
using std::gmtime;
using std::localtime;
using std::strftime;
#endif /* ! _CPP_STYLE_HEADER */

Link to post
Share on other sites
Use the functions in time.h

 

#include "time.h"

const time_t time = 1234;	// Time in seconds

struct tm *lt = localtime(&time);	// Convert to date & time for local time zone

 

your "const time_t time = 1234; // Time in seconds" doesn't work

 

It says "Severity and Description Path Resource Location Creation Time Id

declaration is incompatible with "time_t time(time_t *)" (declared at line 61 of "C:/Program Files (x86)/Texas Instruments/ccsv4/tools/compiler/msp430/include/time.h")"

Link to post
Share on other sites

ok, after trying out time.h, i realized that my computer is sending the time to the MSP430, which isn't something that I want. I need the MSP430 to independently keep track of time. I have a keypad and character LCD screen that can be used to input the current time when the msp is powered on. Is there anyway to write to time_t and just increment it every second.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...