[Gambas-user] Unix Date conversion
Jacky
jscops at ...11...
Mon Jul 24 19:33:50 CEST 2006
Le Lundi 24 Juillet 2006 18:29, sbungay a écrit :
> Thanks Laurent.. I have created a class called DateFunctions that has
> the following contained within it...
>
>
> TimeStampToJulianDay(TimeStamp AS Long) as Integer
> TimeStampToJulianYear(TimeStamp AS Long) as Integer
>
> MonthNumber(JulianDay AS Integer) as Integer
> MonthName(MonthNumber AS Integer) as String
>
> I am constrained by time and the code is not pretty/elegant.. in fact
> the term "brute force" comes to mind... :)
> Anyway.. I can instantiate the class and now get what I need.. as
> needs change so too will the class. This is its current state...
>
> ' Gambas class file
> ' Days in each month (Feb changes depending on leap years)
> ' Want the class to be dynamic.. there has to be a better way than
> ' declaring a useless variable at the top. RTFM this...
> PUBLIC NotStatic AS Boolean
>
> CONST cSecondsInADay AS Integer = 86400
> CONST cDaysInAYear AS Float = 364.25
>
> CONST cJan AS Integer = 1
> CONST cFeb AS Integer = 2
> CONST cMar AS Integer = 3
> CONST cApr AS Integer = 4
> CONST cMay AS Integer = 5
> CONST cJun AS Integer = 6
> CONST cJul AS Integer = 7
> CONST cAug AS Integer = 8
> CONST cSep AS Integer = 9
> CONST cOct AS Integer = 10
> CONST cNov AS Integer = 11
> CONST cDecember AS Integer = 12
>
> CONST cJanDays AS Integer = 31
> CONST cFebDays AS Integer = 28
> CONST cMarDays AS Integer = 31
> CONST cAprDays AS Integer = 30
> CONST cMayDays AS Integer = 31
> CONST cJunDays AS Integer = 30
> CONST cJulDays AS Integer = 31
> CONST cAugDays AS Integer = 31
> CONST cSepDays AS Integer = 30
> CONST cOctDays AS Integer = 31
> CONST cNovDays AS Integer = 30
> CONST cDecDays AS Integer = 31
>
> '***********************************************************************
> '* Author: Stephen Bungay
> '* Date: July 23 2006
> '*
> '* Parameters:
> '* Integer
> '* Called By
> '*
> '* Calls
> '*
> '* Returns
> '* Integer
> '***********************************************************************
> PUBLIC FUNCTION MonthNumber(JulianDate AS Integer) AS Integer
>
> DIM Months AS NEW Integer[13]
>
> DIM Scrap AS Integer
> DIM Counter AS Integer
>
> Months[cJan] = cJanDays
> Months[cFeb] = cFebDays
> Months[cMar] = cMarDays
> Months[cApr] = cAprDays
> Months[cMay] = cMayDays
> Months[cJun] = cJunDays
> Months[cJul] = cJulDays
> Months[cAug] = cAugDays
> Months[cSep] = cSepDays
> Months[cOct] = cOctDays
> Months[cNov] = cNovDays
> Months[cDecember] = cDecDays
>
> Scrap = JulianDate
> Counter = 1
>
> REPEAT
> Scrap = Scrap - Months[Counter]
> Counter = Counter + 1
> UNTIL Scrap <= Months[Counter]
>
> RETURN (Counter - 1)
>
> END
>
> '***********************************************************************
> '* Author: Stephen Bungay
> '* Date: July 23 2006
> '*
> '* Parameters:
> '* Long
> '* Called By
> '*
> '* Calls
> '*
> '* Returns
> '* Integer
> '***********************************************************************
> PUBLIC FUNCTION TimeStampToYear(SecondsSinceEpoch AS Long) AS Integer
>
> DIM YearNumber AS Integer
>
> DIM DaysSinceEpoch AS Integer
> DIM YearsSinceEpoch AS Integer
> DIM FractionalYearsSinceEpoch AS Float
>
> DIM JulianDay AS Integer
>
> DaysSinceEpoch = SecondsSinceEpoch / cSecondsInADay
> YearsSinceEpoch = DaysSinceEpoch / cDaysInAYear
> FractionalYearsSinceEpoch = CFloat(Mid$(CStr(DaysSinceEpoch /
> cDaysInAYear), InStr(CStr(DaysSinceEpoch / cDaysInAYear), ".", 1), 5))
> JulianDay = CInt(cDaysInAYear * FractionalYearsSinceEpoch)
>
> RETURN (1970 + YearsSinceEpoch)
>
> END
>
> '***********************************************************************
> '* Author: Stephen Bungay
> '* Date: July 23 2006
> '*
> '* Parameters:
> '* Long
> '* Called By
> '*
> '* Calls
> '*
> '* Returns
> '* Integer
> '***********************************************************************
> PUBLIC FUNCTION TimeStampToJulianDay(SecondsSinceEpoch AS Long) AS Integer
>
> DIM DaysSinceEpoch AS Integer
> DIM FractionalYearsSinceEpoch AS Float
>
> DIM ExtractedYear AS Integer
> DIM JulianDay AS Integer
>
> DaysSinceEpoch = SecondsSinceEpoch / 86400
> FractionalYearsSinceEpoch = CFloat(Mid$(CStr(DaysSinceEpoch /
> 365.24), InStr(CStr(DaysSinceEpoch / 365.24), ".", 1), 5))
> JulianDay = CInt(365 * FractionalYearsSinceEpoch)
>
> RETURN (JulianDay)
>
> END
>
>
> '***********************************************************************
> '* Author: Stephen Bungay
> '* Date: July 23 2006
> '*
> '* Parameters:
> '* Integer
> '*
> '* Called By
> '*
> '* Calls
> '*
> '* Returns
> '* String
> '*
> '***********************************************************************
> PUBLIC FUNCTION MonthName(MonthNumber AS Integer) AS String
> DIM X AS String
>
> SELECT CASE MonthNumber
> CASE 1
> X = "January"
> CASE 2
> X = "February"
> CASE 3
> X = "March"
> CASE 4
> X = "April"
> CASE 5
> X = "May"
> CASE 6
> X = "June"
> CASE 7
> X = "July"
> CASE 8
> X = "August"
> CASE 9
> X = "September"
> CASE 10
> X = "October"
> CASE 11
> X = "November"
> CASE 12
> X = "December"
> END SELECT
>
> RETURN (X)
> END
>
>
>
> Thanks to all for helping out...
>
> Laurent Carlier wrote:
> > Le lundi 24 juillet 2006 06:13, sbungay a écrit :
> >> I've been coding for the last 17 hours and can no longer see straight
> >>let alone think straight... :)
> >> Have a Unix date stored in a table and need to convert it to
> >>something the average human can read... there should be a function for
> >>this... and at the moment I am too tired to write one.. anybody got one
> >>they can part with? I've been looking through the gambas docs and don't
> >>see anything there (although I'm so tired I could walk right past the
> >>answer and not see it).
> >>
> >>Steve....ZZZzzzzzzz...
> >>
> >>Good night all (to those in Europe... good morning!)
> >
> > Try this, i just going to do it
> >
> > It's really dirty but it should work
> >
> > ' Gambas module file
> >
> > PUBLIC SUB Main()
> >
> > PRINT convert(1142123287)
> >
> > END
> >
> > PUBLIC FUNCTION convert(value AS Integer) AS Date
> >
> > DIM $valYear AS Integer
> > DIM $valMonth AS Integer = 1
> > DIM $valDay AS Integer
> > DIM $valHour AS Integer
> > DIM $valMinute AS Integer
> > DIM $valSecond AS Integer
> >
> > DIM $Months AS Integer[] = [31, 27, 31, 30, 31, 30, 31, 31, 30, 31, 30,
> > 31] DIM $i AS Integer
> > DIM $valTmp AS Float
> >
> > $valTmp = (value / (365.25 * 60 * 60 * 24))
> > $valYear = 1970 + $valTmp
> > IF (checkYear($valYear)) THEN INC ($Months[1])
> > $valTmp = Frac($valTmp) * 365.25
> >
> > FOR EACH $i IN $Months
> > IF $valTmp > $i THEN
> > $valTmp = $valTmp - $i
> > INC ($valMonth)
> > ELSE
> > BREAK
> > ENDIF
> > NEXT
> >
> > $valDay = $valTmp
> > $valTmp = ($valTmp - $valDay) * 24
> > $valHour = $valTmp
> > $valTmp = ($valTmp - $valHour) * 60
> > $valMinute = $valTmp
> > $valTmp = ($valTmp - $valMinute) * 60
> > $valSecond = $valTmp
> >
> > RETURN Date($valYear, $valMonth, $valDay, $valHour, $valMinute,
> > $valSecond)
> >
> > END
> >
> > PUBLIC FUNCTION checkYear(value AS Integer) AS Boolean
> >
> > IF ((((value MOD 4) = 0) AND (value MOD 100) <> 0) OR ((value MOD 400)
> > = 0)) RETURN TRUE
> > ELSE
> > RETURN FALSE
> > ENDIF
> >
> > END
Hello !
In this mailing list, Jim Ward in 2005 send this code.
Try it, it's a perfect code.
Jack
-------------- next part --------------
A non-text attachment was scrubbed...
Name: opendlib-1.0.2.tar.gz
Type: application/x-tgz
Size: 3549 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20060724/cbec9dec/attachment.bin>
More information about the User
mailing list