Epoch/Unix Timestamp to human readable and vice versa.

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys!

I'm trying to translate a human readable date to timestamp and vice versa to do calculations with date. It is incredible hard to do it with the human readable form, so, I'm asking if there is something already done to make this or I need to do myself.

Where I can get a formula to get an accurate time?

In my project I'm getting the time from the DS1307 and putting it on a struct. I want to convert this to do the calculations. AVR does not implements time.h, so, how can I do the calculations?

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Anyone know a easy way to add, subtract, multiply, divide and compare time in AVR?

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There have been references to a generic header file named time.h (or something close to that) that does much of this. Even though it is not AVR, specifically, it is said to be generic C and usable in any program in C.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to use it but avr-gcc does not implements that header, there is another way to do it without time.h or another compiler that implements time.h to AVR devices?

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry Guys, It is a lot easy to convert dates. I not stopped to think on how to do it before posting ;D

The result is attached.

It have only the function to convert year, month and day to timestamp, the rest I will do latter. But it is a lot easy.

The inverse is more easy than that!

And calculate the weekday, Wikipedia explains better than me ;D

[url]http://en.wikipedia.org/wiki/Zeller's_congruence[/url]

Attachment(s): 

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to import it from anywhere you can find it.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to hear more about this multiplying and dividing time stuff. Is this related to the alternate-universe stuff I saw on Nova?

So that leaves us with adding and subtracting. Unix timestamps are in seconds, so isn't it the same as any other 32-bit signed add/subtract?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see your point! haha!

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is simple enough to calculate the seconds elapsed since 1 Jan 1970 (time_t).

You fill in the Unix tm_t structure with the info from your DS1307. The use the Unix mktime(). (I think)

However you can equally well perform the calculation yourself. You will probably find 1 Jan 2012 is a handy datum point. If you need 1 Jan 1970, you only need to add the required number of seconds (1325419200 I think)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

In my project I'm getting the time from the DS1307 and putting it on a struct. I want to convert this to do the calculations. AVR does not implements time.h, so, how can I do the calculations?

What does that have to do with
Quote:

add, subtract, multiply, divide and compare time in AVR?

I have some code on PC-side apps that process log files from AVR apps, and those indeed do a bit of "date-time arithmetic". But I've never seen the need for more than elapsed seconds in the AVR itself.

Have you gone through this recent thread?
http://www.avrfreaks.net/index.p...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

add/subtract seconds...
what about leap years and leap seconds?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are plenty of occasions when you want to calculate a time in the future or past.

For example, predicting oestrus, gestation, parturition.
Or even when your washing machine cycle will start or finish.

I think you would be unlikely to worry about leap seconds in those examples. You certainly need leap years. Fortunately the DS1307 copes with leap years by itself. (It will go wrong in 2100)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Fortunately the DS1307 copes with leap years by itself. (It will go wrong in 2100)

David.


Oh darn, are you saying that the clock in my "box" will be useless in and after 2100? :shock:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Fortunately the DS1307 copes with leap years by itself. (It will go wrong in 2100)

David.


Quote:

Oh darn, are you saying that the clock in my "box" will be useless in and after 2100?

Well, it did help a bit when working on Y2K.

Re the leap-second(s): It is just this discussion why the US didn't agree to the leap-second that happened recently. http://en.wikipedia.org/wiki/Lea...

However, y'all shooting holes at my straightforward solution: Is your RTC setup so tuned that it never needs correction? I figger my production RTC units should be within a minute a month. It is good enough for my industrial data logging. I have no provisions for "daylight savings"/"summer time", so the units need to be visited several times a year anyway. I'm not tracking celestial bodies so I do not need to calculate many centuries/millennia backwards or forwards. These are AVR8 apps we are making, folks--not space probes. sheesh.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I decided to put all my work on that way tp everyone.

You can see better in a text editor without line breaks.

typedef unsigned long int Timestamp;

typedef enum {
	TIME_OBJECT = 0,
	TIME_STAMP = 1
} TimeFormat;

typedef struct Time {
	unsigned int year;
	unsigned int month;
	unsigned int day;
	unsigned int weekday;
	unsigned int hour;
	unsigned int minute;
	unsigned int second;
}Time;

Timestamp Time_Convert_TO2TS(Time * time) {
	if (time) {
		int mth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 273, 304, 334};
		int mthb[12] = {0, 31, 60, 91, 121, 152, 182, 213, 274, 305, 335};
		Timestamp timestamp =
				( ( (!(time->year % 4)) && (time->year % 100) ) || (!(time->year % 400)) )?
						((((unsigned long int)( time->year - 1970) / 4)) + (time->year - 1970) * 365 + mthb[time->month-1] + (time->day - 1)) * 86400 + time->hour * 3600 + time -> minute * 60 + time -> second:
						((((unsigned long int)( time->year - 1970) / 4)) + (time->year - 1970) * 365 + mth [time->month-1] + (time->day - 1)) * 86400 + time->hour * 3600 + time -> minute * 60 + time -> second;
		return timestamp;
	} else {
		return 0;
	}
}

Time * Time_Convert_TS2TO(Timestamp _time) {
	int i; unsigned long int tmp;
	Time * time = (Time *) malloc(sizeof(Time));
	unsigned long int mth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 273, 304, 334, 365};

	time->second = _time%60; _time -= time->second;
	time->minute = (_time%3600)/60; _time -= (time->minute*60);
	time->hour = (unsigned int) (_time%((unsigned long int) 24*3600))/3600; _time -= (unsigned long int) time->hour*3600;
	time->year = (unsigned int) (1970 + _time/((unsigned long int) 365*24*60*60));

	for (i=0, tmp=1970;tmpyear;( ( (!(tmp % 4)) && (tmp % 100) ) || (!(tmp % 400)) )?i++:0,tmp++);
	_time -= (unsigned long int) i*24*60*60; _time -= (unsigned long int) (time->year-1970)*365*24*60*60;
	for (i=1;i<=12;i++) if (mth[i]>(_time/((unsigned long int) 24*60*60))) break;
	time->month = i;
	_time -= mth[time->month-1]*24*60*60;
	time->day = _time/((unsigned long int) 24*60*60);

	return time;
}

Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, Arthur

I know it was awhile ago, but did you ever get this going?

-Andy

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR-LibC 2.0 has <time.h>

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah, thanx! I'll see if I can get my 'beginner brain' round it.

Must admit, Arthur's code had me very puzzled indeed...

-Andy

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not that puzzling. If you delve into it you are going to see something like that supporting the <time.h> API. The point with <time.h>, like things like printf(),  strcat()  and so on is that it doesn't really matter how any of them are actually implemented internally as long as they provide you the documented API. Just use them,  don't stare too deeply INTO them! 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are missing 212, 243, 273 in mth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 273, 304, 334, 365}

‘The problem is not the problem. The problem is your attitude about the problem. Do you understand’ – Captain Jack Sparrow