def discardTimeZoneFromDate(the_date): """Input: NSDate object Output: NSDate object with same date and time as the UTC. In Los Angeles (PDT), '2011-06-20T12:00:00Z' becomes '2011-06-20 12:00:00 -0700'. In New York (EDT), it becomes '2011-06-20 12:00:00 -0400'. """ # get local offset offset = NSTimeZone.localTimeZone().secondsFromGMT() try: # return new NSDate minus local_offset return the_date.dateByAddingTimeInterval_(-offset) except: raise BadDateError()
def subtract_tzoffset_from_date(the_date): """Input: NSDate object Output: NSDate object with same date and time as the UTC. In Los Angeles (PDT), '2011-06-20T12:00:00Z' becomes '2011-06-20 12:00:00 -0700'. In New York (EDT), it becomes '2011-06-20 12:00:00 -0400'. This allows a pkginfo item to reference a time in UTC that gets translated to the same relative local time. A force_install_after_date for '2011-06-20T12:00:00Z' will happen after 2011-06-20 12:00:00 local time. """ # find our time zone offset in seconds timezone = NSTimeZone.defaultTimeZone() seconds_offset = timezone.secondsFromGMTForDate_(the_date) # return new NSDate minus local_offset return NSDate.alloc( ).initWithTimeInterval_sinceDate_(-seconds_offset, the_date)
def subtract_tzoffset_from_date(the_date): """Input: NSDate object Output: NSDate object with same date and time as the UTC. In Los Angeles (PDT), '2011-06-20T12:00:00Z' becomes '2011-06-20 12:00:00 -0700'. In New York (EDT), it becomes '2011-06-20 12:00:00 -0400'. This allows a pkginfo item to reference a time in UTC that gets translated to the same relative local time. A force_install_after_date for '2011-06-20T12:00:00Z' will happen after 2011-06-20 12:00:00 local time. """ # find our time zone offset in seconds timezone = NSTimeZone.defaultTimeZone() seconds_offset = timezone.secondsFromGMTForDate_(the_date) # return new NSDate minus local_offset return NSDate.alloc().initWithTimeInterval_sinceDate_( -seconds_offset, the_date)
def add_tzoffset_to_date(the_date): """Input: NSDate object Output: NSDate object with timezone difference added to the date. This allows conditional_item conditions to be written like so: <Key>condition</key> <string>date > CAST("2012-12-17T16:00:00Z", "NSDate")</string> with the intent being that the comparision is against local time. """ # find our time zone offset in seconds timezone = NSTimeZone.defaultTimeZone() seconds_offset = timezone.secondsFromGMTForDate_(the_date) # return new NSDate minus local_offset return NSDate.alloc( ).initWithTimeInterval_sinceDate_(seconds_offset, the_date)
def add_tzoffset_to_date(the_date): """Input: NSDate object Output: NSDate object with timezone difference added to the date. This allows conditional_item conditions to be written like so: <Key>condition</key> <string>date > CAST("2012-12-17T16:00:00Z", "NSDate")</string> with the intent being that the comparision is against local time. """ # find our time zone offset in seconds timezone = NSTimeZone.defaultTimeZone() seconds_offset = timezone.secondsFromGMTForDate_(the_date) # return new NSDate minus local_offset return NSDate.alloc().initWithTimeInterval_sinceDate_( seconds_offset, the_date)
def lookupSystemTimezone(): return NSTimeZone.localTimeZone().name().encode("utf-8")
def main(): # # UNIX command 'date' # if False: pr("UNIX command 'date'") cmd = 'date' r = commands.getoutput(cmd) pr("UNIX date command", r) pr("r.split()[4]", r.split()[4]) # # Python datetime # if False: pr("Python datetime") pr('datetime.datetime.now() ', datetime.datetime.now() ) t = datetime.datetime.now() - datetime.datetime.utcnow() pr("datetime.now() - datetime.utcnow()", t) pr("timedelta(-1, 68399, 999998) == timedelta(-1, 68400, 0)", datetime.timedelta(-1, 68399, 999998) == datetime.timedelta(-1, 68400, 0) ) pr("datetime.timedelta(hours=-5, seconds = -1) < t < datetime.timedelta(hours=-5, seconds = 1)", datetime.timedelta(hours=-5, seconds = -1) < t < datetime.timedelta(hours=-5, seconds = 1)) # print datetime.timedelta(hours=-5, seconds = 0) < t < datetime.timedelta(hours=-5, seconds = 1) #False # print datetime.timedelta(hours=-5, seconds = -1) < t < datetime.timedelta(hours=-5, seconds = 0) #True t2 = datetime.datetime.utcnow() + t pr('strftime("%a %Y.%m.%d %I:%M:%S")', t2.strftime("%a %Y.%m.%d %I:%M:%S") ) d3 = datetime.datetime(2011, 7, 29, 23, 46, 39) pr( 'str(d3)', str(d3) ) prr("_DATETIME_to_python(d3)", _DATETIME_to_python(d3)) # # Cocoa (Foundation) NSDate, NSCalendar, NSDateFormatter, etc. # pr("Cocoa (Foundation) NSDate, etc.") date1 = NSDate.dateWithTimeIntervalSinceReferenceDate_(333675999.713839) date2 = NSDate.dateWithTimeIntervalSinceReferenceDate_(333675999.713839 - 6 * 30 * 24 *60 * 60) date3 = NSDate.distantPast() # dateWithTimeIntervalSinceNow_(0- 6 * 30 * 24 *60 * 60) pr( "date1" , date1) pr( "date2" , date2) pr( "date3" , date3) prr("_DATETIME_to_python(date1)", _DATETIME_to_python(date1)) # pr( "str(date1) ", str(date1) ) currentCalendar = NSCalendar.currentCalendar() # # time zones # def pr_tz(l, tz): s = tz.secondsFromGMT() / (60 * 60) print "%s:\n\n %r (%s) offset %d hours%s\n" % (l,tz.name(), tz.abbreviation(), s , " (**local**)" if "Local Time Zone " in tz.description() else "") # print tz.description() # timeZone_Current = currentCalendar.timeZone() # # pr_tz('timeZone_Local', timeZone_Local) # # # s = timeZone_GMT.secondsFromGMT() / (60 * 60) # # pr("timeZone_GMT", str(timeZone_GMT) + " offset: %d hours" % s ) # pr( "timeZone_Local.isDaylightSavingTime()", timeZone_Local.isDaylightSavingTime() ) # determines whether daylight saving time is currently in effect. # pr( "timeZone_Local.daylightSavingTimeOffset()", timeZone_Local.daylightSavingTimeOffset() ) # determines the current daylight saving time offset. For most time zones this is either zero or one. # pr( "timeZone_Local.nextDaylightSavingTimeTransition()", timeZone_Local.nextDaylightSavingTimeTransition()) # Formatting for Machines: Controlled Environment Needed # # It is a whole other matter if you need to create a date string according to # the specification of a certain file format or API. # In such a case, you usually have to follow a very strict spec to # make sure the other party can read the string you are generating. # By default, NSDateFormatter uses the user’s current calendar and time zone, # which are possibly different from the requirements. # Most file formats and web APIs use the western, Gregorian calendar, # so we need to make sure that our date formatter uses it, too. dateFormatter_Local = NSDateFormatter.alloc().init() dateFormatter_Current = NSDateFormatter.alloc().init() dateFormatter_GMT = NSDateFormatter.alloc().init() dateFormatter_GMT5 = NSDateFormatter.alloc().init() dateFormatter_NY = NSDateFormatter.alloc().init() # dateFormatter_Local.__name__ = 'dateFormatter_Local' formatter_names = [ ] time_zones = [ ('Local' , NSTimeZone.localTimeZone()) , ('Current' , currentCalendar.timeZone()) , ('GMT' , NSTimeZone.timeZoneForSecondsFromGMT_(0)) , ('GMT5' , NSTimeZone.timeZoneForSecondsFromGMT_(-18000)) , ('NY' , NSTimeZone.timeZoneWithName_(u'America/New_York')) , ('System', NSTimeZone. systemTimeZone() ) , ('G' , NSTimeZone.timeZoneWithAbbreviation_(u'GMT')) ] dx = [ {'name' : n , 'tz' : tz, 'df' : NSDateFormatter.alloc().init() } for n, tz in time_zones ] s = [ "%12s: %s" % (x['name'], "%r (%s) %s%s" % tz_pr(x['tz']) ) for x in dx ] print "\n".join(s) print def eq_classes(dx, k): # z = [] d = {} for n, x in enumerate(dx): if x[k] not in d: d[x[k]] = set([ x['name'] ]) for m in range(n): y = dx[m] if x != y and x[k] == y[k]: # z.append((x['name'], y['name'])) if x[k] in d: d[x[k]].add( x['name'] ) # else: # d[x[k]] = set([ x['name'] ]) if y[k] in d: d[y[k]].add( y['name'] ) # else: # d[y[k]] = [ y['name'] ] return d print "eq_classes of dx (under tz):" print eq_classes_dx = eq_classes(dx, 'tz') print "\n".join([ "%20s: %s" % (x.name(), list(eq_classes_dx[x])) for x in eq_classes_dx]) print eq_names = [ list(eq_classes_dx[x])[0] for x in eq_classes_dx ] dx = [ z for z in dx if z['name'] in eq_names ] s = [ "%12s: %s" % (x['name'], "%r (%s) %s%s" % tz_pr(x['tz']) ) for x in dx ] print "\n".join(s) print # print "\n".join([ "%20s: %r" % [k for k in x] for x in dx ]) # format string (formatter) # format_string = "E yyyy'-'MM'-'dd' 'HH':'mm':'ss VVVV" # ==> "AD 2011-07-29 19:46:39 United States (New York)" # format_string = "E yyyy'-'MM'-'dd' 'HH':'mm':'ss VVV" # ==> " 'Fri 2011-07-29 19:46:39 GMT-04:00' format_string = "E yyyy'-'MM'-'dd' 'HH':'mm':'ss z" # ==> 'Fri 2011-07-29 19:46:39 EDT') or 'EST', or 'GMT-04:00' map ( lambda y : NSDateFormatter.setDateFormat_(y, format_string) , [x['df'] for x in dx] ) # locale locale = NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX") pr("NSDate.date()", NSDate.date()) map ( lambda y : NSDateFormatter.setLocale_(y, locale) , [x['df'] for x in dx] ) map ( lambda y : NSDateFormatter.setTimeZone_(y[0], y[1]) , [ (x['df'], x['tz']) for x in dx] ) pr('descriptionWithCalendarFormat:timeZone:locale', date1.descriptionWithCalendarFormat_timeZone_locale_( None, None, locale) ) # format_string,NSTimeZone.timeZoneForSecondsFromGMT_(-18000),locale pr('descriptionWithCalendarFormat:timeZone:locale', date1.descriptionWithCalendarFormat_timeZone_locale_( None, NSTimeZone.timeZoneForSecondsFromGMT_(-18000), locale) ) # format_string,NSTimeZone.timeZoneForSecondsFromGMT_(-18000),locale for a in [date1, date2, date3]: dsd = get_datestrings(dx, a) s = [ "%12s: %r" % (x[0], x[1] ) for x in dsd ] print "\n".join(s) print # # date1_components # fcdc = currentCalendar.components_fromDate_( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit , date1 ) pr( "currentCalendar.components_fromDate", [ fcdc.year(), fcdc.month(), fcdc.day(), fcdc.hour(), fcdc.minute(), fcdc.second(), ] ) dateOfKeynote = currentCalendar.dateFromComponents_(fcdc) pr( "currentCalendar.dateFromComponents", dateOfKeynote )
import commands import datetime # see dates module for list of timezones and formatters from Foundation import NSCalendar, NSDayCalendarUnit, NSWeekdayCalendarUnit,\ NSYearCalendarUnit, NSMonthCalendarUnit, NSHourCalendarUnit, \ NSMinuteCalendarUnit, NSSecondCalendarUnit, NSTimeZone, NSDate, \ NSDateFormatter, NSGregorianCalendar, NSLocale # choose some timezones with which to display some dates, they're fun! time_zones = [ ('Local' , NSTimeZone.localTimeZone()) , ('GMT' , NSTimeZone.timeZoneForSecondsFromGMT_(0)) # ('G' , NSTimeZone.timeZoneWithAbbreviation_(u'GMT')) ] dateFormatters = [ {'name' : n , 'tz' : tz, 'df' : NSDateFormatter.alloc().init() } for n, tz in time_zones ] map ( lambda y : NSDateFormatter.setTimeZone_(y[0], y[1]) , [ (x['df'], x['tz']) for x in dateFormatters] ) format_string = "E yyyy'-'MM'-'dd' 'HH':'mm':'ss z" # ==> 'Fri 2011-07-29 19:46:39 EDT' or 'EST', or 'GMT-04:00' format_string = "E yyyy.MM.dd HH:mm z" # ==> Tue 2012.04.03 00:39 EDT map ( lambda y : NSDateFormatter.setDateFormat_(y, format_string) , [x['df'] for x in dateFormatters] ) def print_timezones(l): print l + ":" # "time_zones:" print
raise RuntimeError(unsupported) HTTP = Decoy() def binaryish(content, format): bin_types = ('pdf','eps','png','jpg','jpeg','gif','tiff','tif','zip','tar','gz') bin_formats = ('raw','bytes','img','image') if any(b in content for b in bin_types): return True if format: return any(b in format for b in bin_types+bin_formats) return False _nsdf = NSDateFormatter.alloc().init() _nsdf.setLocale_(NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX")) _nsdf.setDateFormat_("EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz") _nsdf.setTimeZone_(NSTimeZone.timeZoneForSecondsFromGMT_(0)) def last_modified(resp): """Return the last modified date as a unix time_t""" last_mod = _nsdf.dateFromString_(resp.headers.get('Last-Modified')) if not last_mod: last_mod = NSDate.date() return last_mod.timeIntervalSince1970() ### File/URL Reader ### def read(pth, format=None, encoding=None, cols=None, **kwargs): """Returns the contents of a file into a string or format-dependent data type (with special handling for json and csv files).
__version__ = "1.9" import os import time import errno import httplib import urllib2 import StringIO from hashlib import md5 from threading import RLock from Foundation import NSDateFormatter, NSLocale, NSTimeZone, NSDate _date_parser = NSDateFormatter.alloc().init() _date_parser.setLocale_(NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX")) _date_parser.setDateFormat_("EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz") _date_parser.setTimeZone_(NSTimeZone.timeZoneForSecondsFromGMT_(0)) def GET(url): """Return the contents of a url (from cache if possible)""" opener = urllib2.build_opener(CacheHandler("/tmp/plod")) response = opener.open(url) last_mod = _date_parser.dateFromString_(response.headers.get('Last-Modified')) if not last_mod: last_mod = NSDate.date() return response, last_mod.timeIntervalSince1970() cache_lock = RLock() def locked_function(origfunc):
Returns a string. """ if isinstance(buf, (int,float,long)): # ,Decimal)): return str(buf) elif isinstance(buf, type(None)): return "NULL" else: # Anything else would be a string return "'%s'" % buf # put together a date formatter. used to just use str()! [ :( ] db_df = NSDateFormatter.alloc().init() gmt0_tz = NSTimeZone.timeZoneForSecondsFromGMT_(0) db_df.setTimeZone_( gmt0_tz ) db_df.setTimeStyle_(NSDateFormatterFullStyle) # <=== magic. have to do this(?) db_df.setDateFormat_("yyyy-MM-dd HH:mm:ss") #"yyyy-MM-dd hh:mm:ss") # "2013-03-30 18:11:07" def GetDR(item_dict, required_fields, quote_and_escape=True, verbose_level_threshold=3): """Convert from item_dict (Cocoa) forms to database-ready forms""" GPR.print_dict("GetDR(in)" , item_dict, 36, verbose_level_threshold) result_dict = {} for db_field_name in required_fields: if db_field_name in item_dict: dict_key_name = db_field_name
import os import time import errno import httplib import urllib2 import StringIO from hashlib import md5 from threading import RLock from Foundation import NSDateFormatter, NSLocale, NSTimeZone, NSDate _date_parser = NSDateFormatter.alloc().init() _date_parser.setLocale_( NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX")) _date_parser.setDateFormat_("EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz") _date_parser.setTimeZone_(NSTimeZone.timeZoneForSecondsFromGMT_(0)) def GET(url): """Return the contents of a url (from cache if possible)""" opener = urllib2.build_opener(CacheHandler("/tmp/plod")) response = opener.open(url) last_mod = _date_parser.dateFromString_( response.headers.get('Last-Modified')) if not last_mod: last_mod = NSDate.date() return response, last_mod.timeIntervalSince1970() cache_lock = RLock()