WP7 TimeZoneInfo.ConvertTime not giving correct results - windows-phone-7

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.

Related

How to Get the correct shutdown time of the Windows system Using Win32 or MFC

I am trying to get the last shutdown time of the system. I tried event log EvtQuery() method and got the value Event/System/TimeCreated/#SystemTime but it is not accurate.
I need this Time and date value:
I got value only from here, dates are the same but time is different:
Your timestamps do match, but one is in UTC (17:21:34) the other in local time (10:51:34 PM -> 22:51:34).
So it looks like your local timezone is 5:30 ahead of UTC time. So according to wikipedia that would be parts of India or Sri Lanka.
So what you have to do is convert the local time value to UTC (or the other way around) and you should see that they are the same.
There should be plenty of material for this on StackOverflow (try this search for example).

How does Ruby Time#dst?

Perhaps a daft question that demonstrates my lack of understanding of daylight saving fundamentals, but as per the title, how does Time.dst? know whether the time object is indeed true or false?
Presumably this must be discernible by the combination of the Date and the Timezone, but then that doesn't make sense as lower latitudes in timezones don't use daylight savings? Therefore surely it must need location to discern #dst? ?
What am i missing?
To deal with time zones and daylight savings time, Ruby, like just about everything else, is calling the localtime_r C function. This puts the time in a C structure called tm which includes a field called isdst. Ruby is reading that flag.
localtime_r calculates isdst first by getting your time zone from the global tzname variable. tzname is determined by calling tzset. How tzset does its job is system dependent. It can come from the TZ environment variable, reading a file, or querying an OS service.
For example.
# Time zone from the system.
$ ruby -e 'puts Time.now.zone; puts Time.now.dst?'
PDT
true
# Time zone from the TZ environment variable.
$ TZ='Australia/Brisbane' ruby -e 'puts Time.now.zone; puts Time.now.dst?'
AEST
false
Once it has the time zone, localtime_r can convert from GMT to the desired time zone (using the rules which applied on that date) using the "tz database" aka "tzdata" aka "zoneinfo" aka "the Olson database" after its creator Arthur David Olson. Previously a private effort, this is now maintained by IANA. It is a set of files installed on your system, or shipped with Ruby, which contains Far More Than Everything You Ever Want To Know About Time Zones and Daylight Savings.
The tz database treats daylight savings (and other weird things like War and Peace time) as just another time zone. Time zone records are kept going all the way back as far as we've had time zones. Before we had time zones solar noon for that location is used. Because of these historical complications and shifting time zones, the tz database prefers to work with cities (such as "America/New York") and determine the time zone for you.
The time zone data files contain extensive commentary and background, if you're interested in the history of calendaring they're a fascinating read.
The Ruby 2.2.0 Time.dst? method refers to the time_isdst(...) function in time.c:
// ...
return tobj->vtm.isdst ? Qtrue : Qfalse
Which seems correspond to the UNIX struct tm tm_isdt member, after plenty of mangling:
int tm_isdst daylight savings flag
Search for tm_isdst and isdst in time.c to read about how it is computed.

Get given timezone timestamp

I am playing around with timezone and noticed something wierd.
I am currently in the BST timezone which is an hour ahead of GMT.
now := time.Now()
location, _ := time.LoadLocation("Atlantic/Cape_Verde")
timeAtZone := now.In(location)
fmt.Println(timeAtZone)
timestamp = timeAtZone.Unix()
fmt.Println(timestamp)
fmt.Println(now.Add(-time.Hour).UTC().Unix())
fmt.Println(now.UTC().Unix())
You will notice that the timestamp is that of BST my current timezone.
How do I get the timestamp of GMT???
http://play.golang.org/p/oq0IRYa0h7
Unix time is absolute. There is no "BST Unix time." There is no "Atlantic/Cape_Verde" Unix time." There is only Unix time. It is the number of seconds since a specific moment (00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds).
Time zones are related to the representation of time, not time itself. It is the same moment for you as it is for me, wherever we are in the world (leaving Einstein aside for the moment). We just happen to call that moment different things. Setting the location on a *Time just indicates how you would like to display the time. So if by "timestamp" you mean "string representing the time," you can get the UTC timestamp with time.Now().UTC().String().
Make sure you check your errors, I assume it's telling you there's an issue.
Have you checked: http://golang.org/pkg/time/#LoadLocation
Is your timezone in: $GOROOT/lib/time/zoneinfo.zip?
For me:
time.LoadLocation("CDT") // my time zone
time.LoadLocation("CST")
Both result in an error.
To get my time zone, I must do:
time.LoadLocation("America/Chicago")
Make sure f.Timezone is valid.

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.

How to get modified date in UTC

GetFileAttributesEx returns the date that the filesystem returns, unmodified. NTFS stores dates in UTC, and FAT stores them in the local time zone. Without knowing the time zone, a date is pretty worthless.
What is the best way to reliably get the last modified date of a file in UTC? Is the best way really to just check for NTFS vs. FAT? What if you are using a different filesystem? Is there a different API I can use? Is there some elegant code someone can post here?
Use the Win32 API GetFileTime, it returns all its times in UTC. Yes, FAT file system does store these values in local time, but the Win32 API is doing the UTC conversion for you.
Quote from the 'FileTimes' MSDN documentation:
GetFileTime retrieves cached UTC times from the FAT file system.
from http://msdn.microsoft.com/en-us/library/ms724290%28v=vs.85%29.aspx
The FAT file system records times on disk in local time. GetFileTime retrieves cached UTC times from the FAT file system. When it becomes daylight saving time, the time retrieved by GetFileTime is off an hour, because the cache is not updated. When you restart the computer, the cached time that GetFileTime retrieves is correct. FindFirstFile retrieves the local time from the FAT file system and converts it to UTC by using the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, FindFirstFile takes daylight saving time into account, even if the file time you are converting is in standard time.

Resources