Exemple #1
0
def test():
    print '--- tests ---'
    import datetime
    from waker import NSTimeZone_tzinfo, datetimeToNSDate, NSDateToDatetime
    # sanity check 
    test_first_jan_2010_8_oclock_datetime = datetime.datetime(2011, 1, 1, 8, 0, tzinfo=NSTimeZone_tzinfo())
    assert(str(test_first_jan_2010_8_oclock_datetime).endswith('08:00:00+00:00'))
    
    # test datetimeToNSDate
    assert(str(datetimeToNSDate(test_first_jan_2010_8_oclock_datetime)).endswith('08:00:00 +0000'))
    
    # test NSDateToDatetime
    nowNSDate = NSDate.date()
    foo = str(nowNSDate)
    bar = str(NSDateToDatetime(nowNSDate))
    #print 'foo', foo
    #print 'bar', bar
    assert(foo[:-6] == bar[:-13])

    #print test_first_jan_2010_8_oclock_datetime
    #print datetimeToNSDate(test_first_jan_2010_8_oclock_datetime)
    print NSDateToDatetime(datetimeToNSDate(test_first_jan_2010_8_oclock_datetime))
    assert(str(NSDateToDatetime(datetimeToNSDate(test_first_jan_2010_8_oclock_datetime))).endswith('08:00:00+00:00'))

    # test NSDateFromString (this uses both the functions above)
    test_first_jan_2010_8_oclock = NSDateFromString('8:00')
    assert(str(test_first_jan_2010_8_oclock).endswith('08:00:00 +0000'))
    
    print '------------'
Exemple #2
0
def NSDateFromString(str):
    if str.startswith('in '):
        str = str.strip()
        m = re.match(r'in (?P<num>\d+)\s*(?P<unit>.*)', str)
        if m:
            num, unit = float(m.groupdict()['num']), m.groupdict()['unit']
            global _NSDateFromString_units
            from datetime import datetime, timedelta
            from waker import datetimeToNSDate
            return datetimeToNSDate(datetime.now()+timedelta(seconds=num*_NSDateFromString_units[unit]))
        raise DidNotUnderstandException()
    date = NSDate.dateWithNaturalLanguageString_(str)
    # now convert it to GMT for the internal processing
    from waker import NSDateToDatetime, datetimeToNSDate
    tmp = NSDateToDatetime(date)
    if tmp is None:
        raise DidNotUnderstandException()
    return datetimeToNSDate(tmp)