I am writing a python script which mainly involves decoration of console output.
I have some value called expiration_time which is in milliseconds. When I display expiration_time on console it is hard to find how much time exactly left to expire. User needs to do some calculation to know how much time is left.
So I decided to print epoch time instead. I want to do something like this:
epoch_time_at_which_expiration_will_happen = current_epoch_time + expiration_time_in_milliseconds
I want to output epoch_time_at_which_expiration_will_happen. How can I do it?
Not really sure what you are trying to output, the time at which expiration happens or the time until the expiration?
Either way, I would use a datetime object instead of the epoch time and convert the milliseconds to microseconds. The datetime objects allow you to do the math you want and to display the output formatted nicely.
https://docs.python.org/2/library/datetime.html
Related
I was testing a feature where I had to write a record with created_at field which is of type time.Time into a Mysql table. When I read the same record back I get following diff.
Time: (time.Time) {
- wall: (uint64) 454722000,
+ wall: (uint64) 0,
What is wall in golang Time and why are they different?
The Monotonic Clocks section of the time package documentation describes monatomic and wall clock time in detail. The section covers the scenario of reading a time from a database.
See Equal for information on how to compare time values.
The real reason for me has to do with nano seconds. Apparently when I write to db nano seconds are stripped off. So what I did is created a function that would return me current time but without nano seconds.
I've got Time objects that I'm writing to an Excel file. I'm using the axlsx library. The class that converts dates to the cell data is DateTimeConverter, which turns it into a float timestamp.
The times are displayed as mm/DD/YYYY HH:MM:SS as expected, but the values are in GMT time.
Is there a way to make Excel format the times for a particular time zone, or the reader's local timezone? My current solution is to export the formatted time as a string, and I am dissatisfied with this.
Is there a way to do this without adding a VBA macro? (Please note that I'm not trying to convert the local time to GMT with a VBA macro as per the linked "duplicate" question, but rather display the GMT time to a local time - preferably without a VBA macro, if possible.)
Short answer
Excel timestamps don't know anything about time zones. If you want to export a value and display it in local time, you need to add the utc_offset to the time before you send it to Axlsx.
t = ...
excel_time = Time.at(t.to_f + t.utc_offset)
sheet.add_row ["Time:", excel_time]
Long answer
In Ruby (and many other programming languages) timestamps are represented as the number of seconds since Jan 1, 1970 00:00:00 UTC (the epoch). The Ruby Time object also retains a time zone, so that when you print it out it can display the local time. If you change the time zone, the time will be printed out differently, but the underlying number stays the same.
In Excel, timestamps are represented as the number of days since Jan 1, 1900 (or 1904, depending on the workbook settings); time is indicated as a fractional part of a day. If today's date is 41262, then 12am is 41262.0 and noon is 41262.5. There are no time zones in this scheme, time is simply a number you read off your watch.
When Axlsx exports a Ruby Time object to Excel, it calls to_f to get the time value in seconds since the epoch, then does some math to convert that to the serial number that Excel likes. Great! But it threw away the utc_offset, which is why the times are appearing in Excel as UTC.
The solution is to simply add the UTC offset to the times in your code, before you hand them over to Axlsx. For example, if you are on Eastern Standard Time, you must subtract five hours. Technically, the new time object you are creating is incorrect as far as Ruby is concerned, but we're just doing this to please Excel.
Let's say I have a task execute. I want to know according to the system clock/time when that finished executing. Is there something in Java I can use to pull the current system time and display it in Java? I'm not looking to measure time like using Millis or Nano (that would only tell me how many milliseconds, not the actual time or date), but actually print the time finished (like "Finished at 9:42 P.M. 10/14/2012" as per my actual Windows time).
A quick Google search finds this: http://www.mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/
Just make sure you import java.util and java.text, and you should be good to go.
I want to change default epoch time that Ruby is using to 1-1-2001 UTC as it is default OS X epoch time. Is this possible?
I have got some hint from answer to my question but I am still not able to get proper out put and sometimes time goes out of range due to conversion type I use for unpacking I have tried by unpacking with all formats including D,E,G,Q,V,v,q,e,L,l and others as well.
But at some point my values go out of the range. Is there any way to force ruby to start epoch as user defined? Many times I get this error:
fileread.rb:48:in `at': 822752480099018910000000000000000000000000000000000000000000000.000000 out of Time range (RangeError)
For an assignment I need to determine the creation time of a random file. As far as I know, Unix does not store the creation time (in contrast to *-BSD). I think I've read somewhere that you should ask for the modification time instead but I don't know where and asking Google doesn't give me a non-ambigious answser either.
Any ideas?
You cannot get the creation time of files in this context. That makes the assignment easy enough: it reasonably cannot be completed.
If someone is talking about creation time in Unix, they are confused. Modification time is completely different from creation time (obviously).
For the record, there are exactly three timestamps in Unix files: ctime, atime and mtime.
Try stat. It will give you all the times associated with a file.
stat filename
To get just the modification date and time:
stat --format=%y filename
Or in seconds since the Epoch:
stat --format=%Y filename