Android Question Working with Times

Status
Not open for further replies.

daveinhull

Member
Licensed User
Hello,

Hope someone can help as I'm going around in circles with dates, times and time zones.
I'd like to show the sum of durations of certain events in the form of hhh:mm

I have a list (DB) of event names and when they happened and the events could cross time zones, but I've stored the event time as a Tick without the time zone, i.e. when I stored the time I adjusted the time by adding DateTime.TicksPerHour * TimeZoneOffset

When I want to display the totals for each event I go through the list starting at the newest and subtracting the previous list value, this I think should give me the duration of the event in Ticks. I then add this to a total for each event. So I think I have the total number of Ticks (or time) for each event.

I just then need to display in hhh:mm.

I think I'm asking weather it is possible to subtract 2 Tick values to get the difference and then add these differences up? And then if this is OK how would I display the total in hhh:mm format?

An help would be appreciated.
Thanks
Dave
 

npsonic

Active Member
Licensed User
You can easily show hours and minutes like this
B4X:
'Format hhh:mm
Sub GetTime (Ticks As Long) As String
    Dim hours As Int = Floor(Ticks / DateTime.TicksPerHour)
    Dim minutes As Int = Floor((Ticks - hours * DateTime.TicksPerHour) / DateTime.TicksPerMinute)
    Return hours & ":" & minutes
End Sub
 
Upvote 0

daveinhull

Member
Licensed User
Thanks for all the replies.

I have another query, which I can't seem to work out what is going on. With the following code, the log shows
31 Dec 1969 16:00:00 I don't understand where the 16 hours comes from?

B4X:
    For Each myKey In Map1.Keys
        lSet = 0
        Map1.Put(myKey,lSet)
        Log (DateTime.Date(Map1.Get(myKey)) & " " & DateTime.time(Map1.Get(myKey)))
    Next

Any thoughts?
Dave
 
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
Thanks for all the replies.

I have another query, which I can't seem to work out what is going on. With the following code, the log shows
31 Dec 1969 16:00:00 I don't understand where the 16 hours comes from?

B4X:
    For Each myKey In Map1.Keys
        lSet = 0
        Map1.Put(myKey,lSet)
        Log (DateTime.Date(Map1.Get(myKey)) & " " & DateTime.time(Map1.Get(myKey)))
    Next

Any thoughts?
Dave

Looks like Map1.Get(myKey) returns a negative value (-28800?). Tick is usually measured in seconds/milisseconds from 1.1.1970 00:00:00
 
Upvote 0

daveinhull

Member
Licensed User
Yes, think that must be it as I'm currently in California. So I guess it is actually being stored as zero but just change to 8 hours behind when I displays time.
Thanks
 
Upvote 0
Status
Not open for further replies.
Top