How do I convert to and from UNIX standard epoch date numbers?
-
This is my first time here, so: hi guys! Hope your day does not suck. Mine is OK, but I'm banging my fragile head against a problem with converting UNIX standard epoch numbers to something fairly regular, date-wise. I receive a timecode from a Perl program, written as seconds since 1970, the standard way of talking about time in the UNIX world. Then my VB component wants to log this thing, and well, I'd like to be able to record it like YYYY-MM-DD HH:MM, but so far I am stumped. Some gotchas (add to this list if you can come up with more difficulties that could crop up during the writing of such a function): * leap years A few examples of timecodes from the last few weeks: 1054746806 1054555725 1056715898 OK, that is my question. Have you done this before? Have a nice day, folks. Olle Lund, Sweden
-
This is my first time here, so: hi guys! Hope your day does not suck. Mine is OK, but I'm banging my fragile head against a problem with converting UNIX standard epoch numbers to something fairly regular, date-wise. I receive a timecode from a Perl program, written as seconds since 1970, the standard way of talking about time in the UNIX world. Then my VB component wants to log this thing, and well, I'd like to be able to record it like YYYY-MM-DD HH:MM, but so far I am stumped. Some gotchas (add to this list if you can come up with more difficulties that could crop up during the writing of such a function): * leap years A few examples of timecodes from the last few weeks: 1054746806 1054555725 1056715898 OK, that is my question. Have you done this before? Have a nice day, folks. Olle Lund, Sweden
After doing a nice function to calculate how many seconds have elapsed since 1970-01-01 00:00:00 a workmate came by and whistled that I should do a DateDiff()
DateDiff("s", CDate("1970-01-01 00:00:00"), Now)
Which was correct. Sigh. I'll try and make the other conversion, now. From seconds into days and years and months and... See you. Olle, Shamed in a corner, Lund, Sweden -
This is my first time here, so: hi guys! Hope your day does not suck. Mine is OK, but I'm banging my fragile head against a problem with converting UNIX standard epoch numbers to something fairly regular, date-wise. I receive a timecode from a Perl program, written as seconds since 1970, the standard way of talking about time in the UNIX world. Then my VB component wants to log this thing, and well, I'd like to be able to record it like YYYY-MM-DD HH:MM, but so far I am stumped. Some gotchas (add to this list if you can come up with more difficulties that could crop up during the writing of such a function): * leap years A few examples of timecodes from the last few weeks: 1054746806 1054555725 1056715898 OK, that is my question. Have you done this before? Have a nice day, folks. Olle Lund, Sweden
I believe this will work, but I didn't actually test it against any known Epoch date values.
Private Function GetEpochDate(ByVal seconds As Long) As DateTime Dim BeginningOfTime As New DateTime(1970, 1, 1) Return BeginningOfTime.AddSeconds(seconds) End Function
-
I believe this will work, but I didn't actually test it against any known Epoch date values.
Private Function GetEpochDate(ByVal seconds As Long) As DateTime Dim BeginningOfTime As New DateTime(1970, 1, 1) Return BeginningOfTime.AddSeconds(seconds) End Function
I regret that I am now working with VB6, and I'm also sorry I didn't include that into my first posting. I bow my head in shame.
DateAdd()
is my next stop, I guess. Yep, it was!Public Function GetEpochToDate(ByVal seconds As Long) As Date GetEpochDate = DateAdd("s", seconds, CDate("1970-01-01 00:00:00")) End Function Public Function GetDateToEpoch(ByVal theDate As Date) As Long GetDateToEpoch = DateDiff("s", CDate("1970-01-01 00:00:00"), theDate) End Function
Thank you, Matt! -
I regret that I am now working with VB6, and I'm also sorry I didn't include that into my first posting. I bow my head in shame.
DateAdd()
is my next stop, I guess. Yep, it was!Public Function GetEpochToDate(ByVal seconds As Long) As Date GetEpochDate = DateAdd("s", seconds, CDate("1970-01-01 00:00:00")) End Function Public Function GetDateToEpoch(ByVal theDate As Date) As Long GetDateToEpoch = DateDiff("s", CDate("1970-01-01 00:00:00"), theDate) End Function
Thank you, Matt!