OpenWrt Forum Archive

Topic: How to calculate Date and Time from Unix-Timestamp?

The content of this topic has been archived on 4 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

i have a Unix-Timestamp "1141408532"
how can i convert this to a human-readable date like "Fri Mar  3 17:55:32 CET 2006" ?


Or, that would be much better:

How can i convert the Timestamp to a crontab entry ?

That is epoch seconds, what is returning that value?

STFW, there's dozens of short algorithms for epoch seconds conversion.

i already searched for arithmetcal functions, but i didn't find a founction.....

maybe you can poste the code ?

$ perl -e 'print scalar localtime(time()) . "\n"'

Is the search button broken today?

Funny mine seems to work, examples:
http://en.wikipedia.org/wiki/Unix_time

You seem to have forgotten to mention what language you are working in also.

(Last edited by vincentfox on 3 Mar 2006, 22:33)

42 wrote:

$ perl -e 'print scalar localtime(time()) . "\n"'

Thanks for your reply,

if i try
root@OpenWrt:~# microperl -e 'print scalar localtime(time()) . "\n"'

i get

Fri Mar  3 22:28:31 2006




if i try
root@OpenWrt:~# microperl -e 'print scalar localtime(time(1141408532)) . "\n"'

i get

syntax error at -e line 1, near "(1141408532"
Execution of -e aborted due to compilation errors.

$ date +%s
1141421607
- Works on any Linux including OpenWrt, FreeBSD etc.

$ date -d '1/1/1970 0:0:1141421607 GMT'
Fri Mar  3 23:33:27 EET 2006
- Doesn't work on OpenWrt due busybox date command, doesn't work on FreeBSD either as it requires GNU date command. Fails on Cygwin with GNU date.

$ awk 'BEGIN { print strftime("%c", 1141421607); exit }'
Fri Mar  3 23:33:27 EET 2006
- Works but don't forget to set OpenWrt timezone settings first. Works on Cygwin with GNU awk and FreeBSD with non-GNU awk too.

(Last edited by jr on 3 Mar 2006, 22:48)

I think you'll spot your mistake given a little reflection, eh?

Come on, I know you can do it without any more help!

(Last edited by vincentfox on 3 Mar 2006, 22:50)

@ jr and the other:


Thank you,

awk 'BEGIN { print strftime("%c", 1141421607); exit }'

was exactly what i was searching for smile

THANKS !

And this works also:

microperl -e 'print scalar localtime(1141408532) . "\n"'

Tell the truth now, is this a real question that you could not figure out on your own, or just some homework assignment that you suckered strangers into completing?

(Last edited by vincentfox on 3 Mar 2006, 22:55)

Surely Thombo could've found answer with some googling, but I still think it's not fair to bash him for asking here. Assuming need was for solution that works on stock OpenWrt installation most examples on net fail - just like those posted on this thread. Microperl version is fine, but it's not part of OpenWrt unless you install extra packages that eat precious flash space. However awk is part of OpenWrt and syntax is quite portable across various platforms, not just different OpenWrt versions.

vincentfox wrote:

Tell the truth now, is this a real question that you could not figure out on your own, or just some homework assignment that you suckered strangers into completing?

This was a problem that i could NOT figure out by my own.
I'm trying to write a script which reads a timer from a dbox, an then starts my pc.
The Timer from the dbox is a unix-timestamp.


I have 2 serial Ports on my Wrt54GS.
On S0 i have a Console-Connection to my computer
On S1 i have a relay-card. So i can Switch relays via my router (and so i can switch electric power).

I also have a dbox for watching tv and recording movies.
To save power i want that my computer is completly powered off.

I set a Timer in my dbox, and the router reads that timer and switches then the power on for my PC.
So my PC can record the movie and shut down. After recording, the Router switches power off, so that my PC have no power.



The biggest problem for me was:
All tipps i found with google doesn't work with openwrt. So i decided to ask for help in this forum.


P.S. @ vincentfox

I understand you.
This is not the homework for someone else.
I do this for myself. And I'm glad that i found help here.
Sure i tried google, but after hours of searching i was so sad and asked in this forum.

(Last edited by Thombo on 4 Mar 2006, 01:07)

Next time give more details up front, avoids a lot of trouble.

Like this:
Looked for solutions on Google, didn't find anything but Perl
I want to run natively on OpenWRT without Perl. The builtin
date command does not support the option for it either.
How can I do that?

See that would show you have done some homework and grasp the problem.
Your original question had no trace of those details.

(Last edited by vincentfox on 3 Mar 2006, 23:36)

vincentfox wrote:

Next time give more details up front, avoids a lot of trouble.

Like this:
Looked for solutions on Google, didn't find anything but Perl
I want to run natively on OpenWRT without Perl
How can I do that?

Ok, i'll do that the next time.
thanks.

Mea Culpa. wink

However the support page still references the defunct package-tracker.

(Last edited by vincentfox on 4 Mar 2006, 00:08)

The discussion might have continued from here.