Posts

Showing posts with the label Epoch

Convert Unix Time With PowerShell

Answer : See Convert a Unix timestamp to a .NET DateTime . You can easily reproduce this in PowerShell. $origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0 $whatIWant = $origin.AddSeconds($unixTime) Function Convert-FromUnixDate ($UnixDate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate)) } $niceTime = Convert-FromUnixDate $ctime PS C:\> $niceTime Friday, 18 May 2012 8:24:18 p.m. $date = get-date "1/1/1970" $date.AddSeconds($unixTime).ToLocalTime()

Android Get Current Timestamp?

Answer : The solution is : Long tsLong = System.currentTimeMillis()/1000; String ts = tsLong.toString(); From developers blog: System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis() , consider listening to the ACTION_TIME_TICK , ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes. 1320917972 is Unix timestamp using number of seconds since 00:00:00 UTC on January 1, 1970. You can use TimeUnit class for unit conversion - from System.currentTimeMillis() to s...

Convert Python Datetime To Epoch With Strftime

Answer : If you want to convert a python datetime to seconds since epoch you could do it explicitly: >>> (datetime.datetime(2012,04,01,0,0) - datetime.datetime(1970,1,1)).total_seconds() 1333238400.0 In Python 3.3+ you can use timestamp() instead: >>> datetime.datetime(2012,4,1,0,0).timestamp() 1333234800.0 Why you should not use datetime.strftime('%s') Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone. >>> datetime.datetime(2012,04,01,0,0).strftime('%s') '1333234800' I had serious issues with Timezones and such. The way Python handles all that happen to be pretty confusing (to me). Things seem to be working fine using the calendar module (see links 1, 2, 3 and...