SRL Archive - General

[.NET] Have a problem with date and Epoch


double codes

2013-06-27 21:32:10

Hey.

If you wouldn't mind helping me a bit with some code i am struggling with that would be great.


So Zybez Price checker API is pretty much a pain in the ass when it comes to dates.

They use epoch(Linux Time) to calculate the dates of when each item is being added to their database(Dunno why).
To convert the epoch time into a normal date Year/Month/Day/Hour/Minute/Second is no problem at all.
And to get the date to show in my listview is also no problem.
However i want the time to let the user know when the date was added ("7 minutes ago").

Now this is the problem:

The For each won't work by changing the value 2 times.


Main code
    Private Sub txturl_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txturl.KeyPress
Dim li1 As New ListViewItem

If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Dim TempUrl As String
TempUrl = "http://forums.zybez.net/runescape-2007-prices/api/" + txturl.Text
TempUrl.ToString.Replace(" ", "+")
Try


Dim ResultJSON As String = New WebClient().DownloadString(TempUrl)
ResultJSON = ResultJSON.ToString.Replace("date", "date1")

Dim Ritem As RuneItem = jss.Deserialize(Of List(Of RuneItem))(ResultJSON)(0)
lbpropeties.Items.Clear()
PrintOutAllProperties(Ritem)

For Each ROffer As RuneOffer In Ritem.Offers

li1 = ListView1.Items.Add(ROffer.rs_name)
li1.SubItems.Add(ROffer.Price)
li1.SubItems.Add(ROffer.contact)
ROffer.date1 = EpochToDateTime(ROffer.date1) ' <- This only converts the epoch time to a real date (28/6/2013:03:27:15)
ROffer.date1 = GetTimeAgo(ROffer.date1) ' <- This should have converted it to for example "7 minutes ago" but replaced every item in the column with "00:00:00"
li1.SubItems.Add(ROffer.date1)
lbpropeties.Items.Add("______")
PrintOutAllProperties(ROffer)
Next
ChangeText()
Catch ex As Exception
MsgBox("Something went wrong!")
End Try
e.Handled = True
End If
End Sub


The functions i use:
    Public Function EpochToDateTime(ByVal EpochValue As Integer) As Date

If EpochValue >= 0 Then
Return CDate("1.1.1970 00:00:00").AddSeconds(EpochValue).AddHours(2)

Else

Return CDate("1.1.1970 00:00:00")

End If

End Function

Public Function GetTimeAgo(ByVal value As Date) As Date
Dim t As TimeSpan = DateTime.Now - value
Dim display As String
If t.TotalSeconds < 60 Then
display = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
display = t.Minutes.ToString() + " minutes ago"


End If
End Function


Screenshot:
http://gyazo.com/f78a4fa2f3960c05cc66a2974ca1788e.png

Thanks

tealc

2013-06-27 21:51:30

It's been a very long time since I used vb. It looks like you don't have a return statement foe GetTimeAgo, so it's returning the default value for date.

double codes

2013-06-27 22:02:32

From: tealc
It's been a very long time since I used vb. It looks like you don't have a return statement foe GetTimeAgo, so it's returning the default value for date.


Just checked and you're right, but how can that be ?
It should return a value.

tealc

2013-06-27 22:19:52

From: double codes
Just checked and you're right, but how can that be ?
It should return a value.

Are you wanting to return display? Would something like this work?


Public Function GetTimeAgo(ByVal value As Date) As [b]String[/b]
Dim t As TimeSpan = DateTime.Now - value
[s]Dim display As String[/s]
If t.TotalSeconds < 60 Then
GetTimeAgo = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
GetTimeAgo = t.Minutes.ToString() + " minutes ago"


End If
End Function
or

Public Function GetTimeAgo(ByVal value As Date) As [b]String[/b]
Dim t As TimeSpan = DateTime.Now - value
Dim display As String
If t.TotalSeconds < 60 Then
return display = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
return display = t.Minutes.ToString() + " minutes ago"


End If
End Function

double codes

2013-06-27 22:44:10

From: tealc
Are you wanting to return display? Would something like this work?


Public Function GetTimeAgo(ByVal value As Date) As [b]String[/b]
Dim t As TimeSpan = DateTime.Now - value
[s]Dim display As String[/s]
If t.TotalSeconds < 60 Then
GetTimeAgo = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
GetTimeAgo = t.Minutes.ToString() + " minutes ago"


End If
End Function
or

Public Function GetTimeAgo(ByVal value As Date) As [b]String[/b]
Dim t As TimeSpan = DateTime.Now - value
Dim display As String
If t.TotalSeconds < 60 Then
return display = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
return display = t.Minutes.ToString() + " minutes ago"


End If
End Function


The first one worked partly.
It only sets the time value to the first four and ignores the rest.

Screenshot:
http://gyazo.com/fc10d1f4ccffdaec03a9b74eebc57181.png


Edit: It sometimes only sets the value for 3.

tealc

2013-06-27 22:51:11

From: double codes
The first one worked partly.
It only sets the time value to the first four and ignores the rest.

Screenshot:
http://gyazo.com/fc10d1f4ccffdaec03a9b74eebc57181.png


Edit: It sometimes only sets the value for 3.


Your code doesn't do anything if it is older than 1 hour

try some thing like


Public Function GetTimeAgo(ByVal value As Date) As String
Dim t As TimeSpan = DateTime.Now - value
Dim display As String
If t.TotalSeconds < 60 Then
GetTimeAgo = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
GetTimeAgo = t.Minutes.ToString() + " minutes ago"
ElseIf t.TotalMinutes >= 60 Then
GetTimeAgo = "more than 1 hour ago"

End If
End Function

double codes

2013-06-27 22:58:08

God i am stupid -.-

Thanks mate, helped me a lot :)

tealc

2013-06-27 23:02:07

From: double codes
-.-

Thanks mate, helped me a lot :)


Post/pm me if you need more help. This looks like a cool project.

double codes

2013-06-27 23:12:46

I probably will lol.

Again thanks for your help :)