SexScripts : Convert Unix time to normal time - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=189 Page 1 of 1

Convert Unix time to normal time

wiliam [ Thu Nov 03, 2011 9:00 pm ]

Hi

Is there a way to convert the unix time used to a normal date and/or time?

Also, is there a way to enter data in a seperate file, a bit of a log?

Wiliam

Re: Convert Unix time to normal time

doti [ Thu Nov 03, 2011 9:51 pm ]

Quote:
Is there a way to convert the unix time used to a normal date and/or time?

It is quite simple
To show a date/time
Code:
def text = new Date().format( "yyyy-MM-dd HH:mm:ss")
show(text)

To get separated values
Code:
def text = Calendar.getInstance().get(Calendar.YEAR)+"-"+
  Calendar.getInstance().get(Calendar.MONTH)+"-"+
  Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+" "+
  Calendar.getInstance().get(Calendar.HOUR_OF_DAY)+":"+
  Calendar.getInstance().get(Calendar.MINUTE)+":"+
  Calendar.getInstance().get(Calendar.SECOND));
show(text)


Conversion is harder (and the result may suffer from the time zones) :
Code:
def t = getTime()
def text = new Date(t*1000L).format( "yyyy-MM-dd HH:mm:ss"))
show(text)


All those codes will show something like : 2014-11-3 12:14:47


Quote:
Also, is there a way to enter data in a seperate file, a bit of a log?

Yes
Code:
def f= new File('log.txt')
f.append("ok\n")


Sources :
http://groovy.codehaus.org/Getting+Started+Guide
Isn't Groovy lovely ?

Re: Convert Unix time to normal time

wiliam [ Thu Nov 03, 2011 11:39 pm ]

It indeed is, thanks for your help!


Wiliam

Re: Convert Unix time to normal time

sissy666 [ Wed Apr 24, 2013 12:50 am ]

I understand getTime() is seconds since 00:00: 1/1/1970 according to the documentation, but on my machine it is 4 h earlier than new Date().time /1000. new Date() seems correct. getTime isn't. It isn't a time zone issue (I'm in EDT so UTC would be 4 h later than Date().time). The reason I'm interested is to look in data.properties at the launch.lasttime value to see how long user has been away. I can hard code the 4-h difference, but I would like to understand it. Is there something about getTime I don't understand?


Thanks in advance

Re: Convert Unix time to normal time

doti [ Wed Apr 24, 2013 7:01 pm ]

(edit)
It is a time zone issue (the opposite of behaviour you may think about).
Avoid using new Date().time, use getTime() instead, particullary calculating a time lapse.

Re: Convert Unix time to normal time

sissy666 [ Fri Apr 26, 2013 1:21 am ]

You say to use getTime. Fair enough. But getTime doesn't give me the correct time when I convert back to hh:mm:ss. new Date().time does. I guess what I'm looking for is a way to load in launch.lasttime and convert that to a correct (local time) hh:mm:ss. Suggestions?

BTW Doti, thanks for the large code work around. Worked great (or as great as not having external subroutines). I've certainly learned a lot about Groovy.

Re: Convert Unix time to normal time

doti [ Fri Apr 26, 2013 9:05 pm ]

Ok, thank you, there is a bug.

It'll be corrected on the next release of SexScripts. This is very strange.
(edit:ok now)

Page 1 of 1 All times are UTC + 1 hour [ DST ]
https://ss.deviatenow.com:443/
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010, 2011