How to convert UNIX timestamp into a readable Basic4PPC-date

moster67

Expert
Licensed User
Longtime User
Hi,

I am wondering if anyone could give me a helping hand how to convert a UNIX timestamp into a readable Basic4PPC-date. For those wondering what this is, then take a look at this page:

Unix time - Wikipedia, the free encyclopedia

This is an example of UNIX timestamp: 1265976300. If you convert into a readable date-format it becomes: Friday, February 12th 2010, 12:05:00 (GMT). You can find a handy converter online here:

4WebHelp - Online Tools: Unix Timestamp Converter

I didn't know about UNIX timestamps until I ran into it in a project of mine.

I searched the forum to see if anyone had already written a converter but couldn't find any information.

I found the below c#-function which turns a UNIX timestamp into a .NET System.DateTime object and it works well in a desktop VS-project:

B4X:
[SIZE="1"]C#:
   1: static DateTime ConvertTimestamp(double timestamp)
   2: {
   3:   return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(timestamp).ToLocalTime();
   4: }[/SIZE]

Above code-snippet takes into consideration also localtime while normally the UNIX stamptime refers to UTC (GMT).

I tried to write my own in Basic4PPC but frankly I have some problems getting used to Ticks....

Thus, if anyone already has written some conversion-code, could you please share it since it could be useful for others too.

Many thanks in advance.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
You can do this to convert a time_t then format it with Date() and Time(). As I understand it both a .NET DateTime and a time_t ignore leap seconds - I guess this will be of no practical significance to you :)

If you already have the time_t as a number you don't need the "Converter.Int32FromBytes(buf(), posn)" which is just picking a number out of a byte array.
 
Last edited:

moster67

Expert
Licensed User
Longtime User
I am now at the airport waiting for my flight and just saw your reply that through one of your amazing libraries you already had the solution ready.

I will try it when I am home.

Many thanks! What would we do without you?


You can do this to convert a time_t then format it with Date() and Time(). As I understand it both a .NET DateTime and a time_t ignore leap seconds - I guess this will be of no practical significance to you :)

If you already have the time_t as a number you don't need the "Converter.Int32FromBytes(buf(), posn)" which is just picking a number out of a byte array.
 
Last edited:

Museltook

Member
Im testing Basic4PPC now and im using the Unix timestamp a lot im my WebDatabases (MySQL). I convert this databases to sqlite an will use them on my PDA. Is there any way to use a simpler way? I cant test the library yet, because i havent registered my trial of Basic4PPC yet. But using a unix timestamp and converting the to a real date (and back to the timestamp) is a must have feature. i cant live without that. its not an option to convert the timestamp to a readable dateformat in the orgiginal database...

is it an option to convert the "Ticks" to seconds and add this seconds to the seconds of the timestamp (befor 1.1.1970)?

please help to get around theis, without using a library.
 

mjcoon

Well-Known Member
Licensed User
...is it an option to convert the "Ticks" to seconds and add this seconds to the seconds of the timestamp (befor 1.1.1970)?

please help to get around theis, without using a library.

Yes, I think that the reference that Andrew Graham gave in his post #2 shows how to do what you want without needing any library. (Given that the time_t is already available to you as a number which is what the library call cited does.)

HTH, Mike.
 

Museltook

Member
it works, i have written this sub:

B4X:
timestamp = "1268661827" '15.03.2010 14:03
ut_time(timestamp)
 
Sub ut_time(timestamp)
 unix_start = dateparse("01/01/1970") 'ticks zu diesem Tag
 conv_time = (timestamp*10000000) + unix_start
 MsgBox(date(conv_time) & „ “ & time(conv_time))
End Sub
 
Top