How to change default Epoch time for Ruby? - ruby

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)

Related

Python find what is the epoch time after xxxx milliseconds

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

What's the correct way to get a Unix timestamp in Go

What's the correct way to get the Unix timestamp in Go? I thought it would be time.Now().Unix() but it's clearly not.
http://play.golang.org/p/KoJADUDxOS
So time.Now().Unix() tells me the unix timestamp is 1257894000 whereas my browser tells me it is 1398351437. Also www.unixtimestamp.de tells me it is 1398351704 which is what I would expect. Why is the one coming from Go so far off? I get the same results on my local machine btw.
What am I missing?
UPDATE
Ok, turns out it was really only an issue on play.golang.org. I thought I tested it locally as well but I had another look now and yes on my local machine everything is correct.
Unix() does produce a Unix timestap: The number of seconds since the epoch.
I think the documentation http://golang.org/pkg/time/#Time.Unix is pretty clear here.
Maybe you are not interested in a Unix timestamp but some human readable time? In that case use Format(): http://golang.org/pkg/time/#Time.Format
And: The playground has only synthetic time. Absolute values on the playground do not correspond to UTC.

WP7 TimeZoneInfo.ConvertTime not giving correct results

I'm attempting to convert a time from UTC to the Phone's local time. For this I'm using the following:
if (progress.ActionDateTime.HasValue)
progress.ActionDateTime = TimeZoneInfo.ConvertTime(progress.ActionDateTime.Value, TimeZoneInfo.Local);
However, the time remains exactly the same after the conversion has took place. Is this method working in WP7?
A DateTime does not store information about the time zone. According to the documentation, TimeZoneInfo.ConvertTime will use the DateTime.Kind property to determine how the time should be converted:
DateTimeKind.Local and DateTimeKind.Unspecified: Converts the local time to the time in destinationTimeZone.
DateTimeKind.Utc: Converts Coordinated Universal Time (UTC) to the time in destinationTimeZone.
Since you're using TimeZoneInfo.Local for the second parameter (which specify the destination time zone), I'm assuming that you're DateTimeKind is either Local or Unspecified. Therefore, you're converting a local date to a local date, which obviously won't work.
DateTime.ToLocalTime also uses the DateTimeKind. According to the documentation:
Utc: This instance of DateTime is converted to local time.
Local: No conversion is performed.
Unspecified: This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.
Basically, while TimeZoneInfo.ConvertTime considers that DateTimeKind.Unspecified = Local, DateTime.ToLocalTime considers that DateTimeKind.Unspecified = Utc. It explains why the latter works while the former doesn't.

Exporting to Excel - What to do with timestamps?

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.

What timestamp format?

I am getting the following format for (i guess) timestamps in some unix logfiles, but don't know what format it is and how to convert it:
The timestamp looks like this: 589702.337476
Can anybody help me?
If it's in seconds, it's just under a week.
I suspect that it's the result of calling clock_gettime() with an argument of CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW, which is defined as "monotonic time since some unspecified starting point"; it's typically the number of seconds since the system was last booted.
On my Ubuntu system, I see timestamps like that (along with real-time timestamps) in /var/log/kern.log.

Resources