Legolas786
asked on
Convert Ctime to date time in textfile?
Hello,
I have a textfile which only contains values like the following:-
bmw07_1487013008
mercedes17_1487012968
ford49_1487012869
nissan16_1487012839
the last 10 numbers is ctime and what I am trying to achieve is to convert the ctime to date/time to another column and then to sort it, so for example i am hoping to have the following:-
bmw07_1487013008 13/02/2017 19:07:19
mercedes17_1487012968 13/02/2017 19:07:49
ford49_1487012869 13/02/2017 19:09:28
nissan16_1487012839 13/02/2017 19:10:08
Now I've been able to use the following function to convert ctime to date/time but not entirely sure how i can use it to only lookup the last 10 numbers in each line and convert it to a new column?
I have a textfile which only contains values like the following:-
bmw07_1487013008
mercedes17_1487012968
ford49_1487012869
nissan16_1487012839
the last 10 numbers is ctime and what I am trying to achieve is to convert the ctime to date/time to another column and then to sort it, so for example i am hoping to have the following:-
bmw07_1487013008 13/02/2017 19:07:19
mercedes17_1487012968 13/02/2017 19:07:49
ford49_1487012869 13/02/2017 19:09:28
nissan16_1487012839 13/02/2017 19:10:08
Now I've been able to use the following function to convert ctime to date/time but not entirely sure how i can use it to only lookup the last 10 numbers in each line and convert it to a new column?
Private Function CTimeToDate(CTime As Int64) As DateTime
Dim span As TimeSpan = TimeSpan.FromTicks(CTime * TimeSpan.TicksPerSecond)
Dim t As DateTime = New DateTime(1970, 1, 1).Add(span)
Return TimeZone.CurrentTimeZone.ToLocalTime(t)
End Function
ASKER
Thanks but when i try it there are no changes made to the text file? cant see why?
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim lines = File.ReadAllLines("C:\Test\TextFile.txt")
For Each line In lines
Dim data = line.Split(New Char() {"_"}, StringSplitOptions.None)
If data.Length = 2 AndAlso Not String.IsNullOrWhiteSpace(data(1)) Then
Dim ctime As Int64
If Int64.TryParse(data(1), ctime) Then
Console.WriteLine("{0} {1}", data(0), CTimeToDate(ctime).ToString("dd/MM/yyyy hh:mm:ss"))
Else
Console.WriteLine("{0} does not contain a valid CTime portion")
End If
Else
Console.WriteLine("{0} does not contain a valid CTime portion")
End If
Next
Console.ReadLine()
End Sub
Private Function CTimeToDate(CTime As Int64) As DateTime
Dim span As TimeSpan = TimeSpan.FromTicks(CTime * TimeSpan.TicksPerSecond)
Dim t As DateTime = New DateTime(1970, 1, 1).Add(span)
Return TimeZone.CurrentTimeZone.ToLocalTime(t)
End Function
If you want to write the changes back to the file then you want to do something like this; e.g. -
File contents after running:
-saige-
Imports System.IO
Module Module1
Sub Main()
Dim lines = File.ReadAllLines("TextFile1.txt")
Using writer As New StreamWriter("TextFile1.txt", False)
For Each line In lines
Dim data = line.Split(New Char() {"_"}, StringSplitOptions.None)
If data.Length = 2 AndAlso Not String.IsNullOrWhiteSpace(data(1)) Then
Dim ctime As Int64
If Int64.TryParse(data(1), ctime) Then
writer.WriteLine("{0}_{1} {2}", data(0), data(1), CTimeToDate(ctime).ToString("dd/MM/yyyy hh:mm:ss"))
Else
Console.WriteLine("{0} does not contain a valid CTime portion")
End If
Else
Console.WriteLine("{0} does not contain a valid CTime portion")
End If
Next
End Using
Console.WriteLine("Finished writing to the file")
Console.ReadLine()
End Sub
Private Function CTimeToDate(CTime As Int64) As DateTime
Dim span As TimeSpan = TimeSpan.FromTicks(CTime * TimeSpan.TicksPerSecond)
Dim t As DateTime = New DateTime(1970, 1, 1).Add(span)
Return TimeZone.CurrentTimeZone.ToLocalTime(t)
End Function
End Module
File contents after running:
-saige-
ASKER
Thanks Saige, that works! just the last bit about sorting would be great? So I'm looking to sort both columns by Column B so the oldest date is at the top. Not sure how I would implement it in my coding? I have done something very basic when sorting but that was in excel and its not working in the text file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
ASKER
Thanks
Open in new window
Which produces the following output -
-saige-