[Gambas-user] Unix Date conversion
rolf
rolf.frogs at ...221...
Tue Jul 25 10:22:28 CEST 2006
Hi Laurent
> > 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).
> >
>
> Try this, i just going to do it
>
> It's really dirty but it should work
Wouldn't it be better to use/integrate the functions from the C-Library, which
are tested since many years and offer also localtime-Support??
Then the wheel doesn't need to be reinvented.
Fine regards
Rolf
> ' 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
>
> --
>
>
> Regards,
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share
> your opinions on IT & business topics through brief surveys -- and earn
> cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
More information about the User
mailing list