Entity Framework DateTime and UTC

Here is one approach you might consider: First, define this following attribute: [AttributeUsage(AttributeTargets.Property)] public class DateTimeKindAttribute : Attribute { private readonly DateTimeKind _kind; public DateTimeKindAttribute(DateTimeKind kind) { _kind = kind; } public DateTimeKind Kind { get { return _kind; } } public static void Apply(object entity) { if (entity == null) return; var properties = … Read more

Getting computer’s UTC offset in Python

time.timezone: import time print -time.timezone It prints UTC offset in seconds (to take into account Daylight Saving Time (DST) see time.altzone: is_dst = time.daylight and time.localtime().tm_isdst > 0 utc_offset = – (time.altzone if is_dst else time.timezone) where utc offset is defined via: “To get local time, add utc offset to utc time.” In Python 3.3+ … Read more

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

No need to remove the :’s. To handle the “00:00” style timezone, you just need “ZZZZ”: Swift let dateString = “2014-07-06T07:59:00Z” let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: “en_US_POSIX”) dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZZZZ” dateFormatter.dateFromString(dateString) Objective-C NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_US_POSIX”]; NSString *input = @”2013-05-08T19:03:53+00:00″; [dateFormat setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZ”]; //iso 8601 format NSDate … Read more

How do I get the UTC time of “midnight” for a given timezone?

I think you can shave off a few method calls if you do it like this: >>> from datetime import datetime >>> datetime.now(pytz.timezone(“Australia/Melbourne”)) \ .replace(hour=0, minute=0, second=0, microsecond=0) \ .astimezone(pytz.utc) BUT… there is a bigger problem than aesthetics in your code: it will give the wrong result on the day of the switch to or … Read more

Convert python datetime to epoch with strftime

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 … Read more