Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
File location fix
Browse files Browse the repository at this point in the history
  • Loading branch information
quver committed Apr 7, 2016
1 parent dcf5d1e commit 201bd84
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 81 deletions.
15 changes: 11 additions & 4 deletions EpochConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@

@interface EpochConverter : NSObject

@property (strong, nonatomic, readonly) NSNumber * baseEpoch;
@property (assign, nonatomic, readonly) NSInteger baseEpoch;
@property (assign, nonatomic, readonly) NSInteger weekDay;
@property (assign, nonatomic, readonly) NSInteger firstWeekDay;
@property (assign, nonatomic, readonly) NSInteger lastWeekDay;

- (id)initWithEpoch:(NSInteger)epoch;
- (NSNumber *)firstWeekDay:(NSNumber *)epoch;
- (NSNumber *)lastWeekDay:(NSNumber *)epoch;
- (instancetype)init;
- (instancetype)initWithEpoch:(NSInteger)epoch;
- (void)changeBaseEpoch:(NSInteger)days;

+ (NSInteger)firstWeekDayForEpoch:(NSInteger)epoch;
+ (NSInteger)lastWeekDayForEpoch:(NSInteger)epoch;
+ (NSInteger)weekDayForEpoch:(NSInteger)epoch;

@end
120 changes: 49 additions & 71 deletions EpochConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,71 @@

@implementation EpochConverter

static NSCalendar *calendar;
static NSDateComponents *components;

-(id)init {
self = [super init];
if(self)
{
NSCalendar* myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

[myCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Warsaw"]];

NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:[NSDate date]];
[components setHour: 0];
[components setMinute: 0];
[components setSecond: 0];

NSLog(@"[[myCalendar dateFromComponents:components] timeIntervalSince1970] %f", [[myCalendar dateFromComponents:components] timeIntervalSince1970]);

_baseEpoch = [NSNumber numberWithInteger:[[myCalendar dateFromComponents:components] timeIntervalSince1970]];

return self;
if(self) {
_baseEpoch = [calendar dateFromComponents:components].timeIntervalSince1970;
}
return nil;

return self;
}

-(id)initWithEpoch:(NSInteger)epoch {
self = [super init];
if(self)
{
_baseEpoch = [NSNumber numberWithInteger:epoch];
return self;
if(self) {
_baseEpoch = epoch;
}
return nil;

return self;
}

- (NSInteger)weekDayForEpoch:(NSNumber *)epoch {

if (epoch == nil) {
epoch = _baseEpoch;
}

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setFirstWeekday:2];
return [calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:[NSDate dateWithTimeIntervalSince1970:epoch.doubleValue]];
- (void)changeBaseEpoch:(NSInteger)days {
_baseEpoch = self.baseEpoch + (days * 86400);
}

//- (NSString *)dateStringForEpoch:(NSInteger)epoch {
//
// if (epoch == 0) {
// epoch = (NSInteger)_baseEpoch;
// }
//
// NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Europe/Warsaw"];
// NSDateFormatter *dateFormatter = [NSDateFormatter new];
// NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"pl"];
// [dateFormatter setLocale:locale];
// [dateFormatter setDateFormat:@"EEEE - dd LLLL YYYY"];
// [dateFormatter setTimeZone:timeZone];
//
// NSDate *date = [NSDate dateWithTimeIntervalSince1970:epoch];
//
// return (NSString *)[dateFormatter stringFromDate:date];
//}

- (NSNumber *)firstWeekDay:(NSNumber *)epoch {

if (epoch == nil) {
epoch = _baseEpoch;
}

NSInteger firstWeekDayEpoch = epoch.integerValue - (3600 * 24 * ([self weekDayForEpoch:epoch] - 1));

return [NSNumber numberWithInteger:firstWeekDayEpoch];
- (NSInteger)weekDay {
return [EpochConverter weekDayForEpoch:self.baseEpoch];
}

- (NSNumber *)lastWeekDay:(NSNumber *)epoch {

if (epoch == nil) {
epoch = _baseEpoch;
}

NSInteger lastWeekDayEpoch = epoch.integerValue + (3600 * 24 * (7 - [self weekDayForEpoch:epoch]));

return [NSNumber numberWithInteger:lastWeekDayEpoch];
- (NSInteger)firstWeekDay {
return [EpochConverter firstWeekDayForEpoch:self.baseEpoch];
}

- (void)changeBaseEpoch:(NSInteger)days {
NSInteger newBaseEpoch = (NSInteger)_baseEpoch + (days * 86400);
_baseEpoch = [NSNumber numberWithInteger:newBaseEpoch];
- (NSInteger)lastWeekDay {
return [EpochConverter lastWeekDayForEpoch:self.baseEpoch];
}

+ (void)initialize {
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"Europe/Warsaw"];
calendar.firstWeekday = 2;

components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
fromDate:[NSDate date]];
components.hour = 0;
components.minute = 0;
components.second = 0;
}

+ (NSInteger)weekDayForEpoch:(NSInteger)epoch {
return [calendar ordinalityOfUnit:NSCalendarUnitWeekday
inUnit:NSCalendarUnitWeekOfYear
forDate:[NSDate dateWithTimeIntervalSince1970:epoch]];
}

+ (NSInteger)firstWeekDayForEpoch:(NSInteger)epoch {
NSInteger firstWeekDayEpoch = epoch - (3600 * 24 * ([self weekDayForEpoch:epoch] - 1));

return firstWeekDayEpoch;
}

+ (NSInteger)lastWeekDayForEpoch:(NSInteger)epoch {
NSInteger lastWeekDayEpoch = epoch + (3600 * 24 * (7 - [self weekDayForEpoch:epoch]));

return lastWeekDayEpoch;
}

@end
12 changes: 6 additions & 6 deletions EpochConverter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
D56581F01CB51FA000AB8DDB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D56581EF1CB51FA000AB8DDB /* Assets.xcassets */; };
D56581F31CB51FA000AB8DDB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D56581F11CB51FA000AB8DDB /* LaunchScreen.storyboard */; };
D56581FE1CB51FA000AB8DDB /* EpochConverterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D56581FD1CB51FA000AB8DDB /* EpochConverterTests.m */; };
D5A52F331CB520E00086EB66 /* EpochConverter.m in Sources */ = {isa = PBXBuildFile; fileRef = D5A52F321CB520E00086EB66 /* EpochConverter.m */; };
D5BB77351CB65161000AAFE6 /* EpochConverter.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BB77341CB65161000AAFE6 /* EpochConverter.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -41,8 +41,8 @@
D56581F91CB51FA000AB8DDB /* EpochConverterTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EpochConverterTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D56581FD1CB51FA000AB8DDB /* EpochConverterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EpochConverterTests.m; sourceTree = "<group>"; };
D56581FF1CB51FA000AB8DDB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D5A52F311CB520E00086EB66 /* EpochConverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EpochConverter.h; sourceTree = "<group>"; };
D5A52F321CB520E00086EB66 /* EpochConverter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EpochConverter.m; sourceTree = "<group>"; };
D5BB77331CB65161000AAFE6 /* EpochConverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EpochConverter.h; sourceTree = "<group>"; };
D5BB77341CB65161000AAFE6 /* EpochConverter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EpochConverter.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -84,8 +84,8 @@
D56581E21CB51FA000AB8DDB /* EpochConverter */ = {
isa = PBXGroup;
children = (
D5A52F311CB520E00086EB66 /* EpochConverter.h */,
D5A52F321CB520E00086EB66 /* EpochConverter.m */,
D5BB77331CB65161000AAFE6 /* EpochConverter.h */,
D5BB77341CB65161000AAFE6 /* EpochConverter.m */,
D56581E61CB51FA000AB8DDB /* AppDelegate.h */,
D56581E71CB51FA000AB8DDB /* AppDelegate.m */,
D56581E91CB51FA000AB8DDB /* ViewController.h */,
Expand Down Expand Up @@ -218,7 +218,7 @@
files = (
D56581EB1CB51FA000AB8DDB /* ViewController.m in Sources */,
D56581E81CB51FA000AB8DDB /* AppDelegate.m in Sources */,
D5A52F331CB520E00086EB66 /* EpochConverter.m in Sources */,
D5BB77351CB65161000AAFE6 /* EpochConverter.m in Sources */,
D56581E51CB51FA000AB8DDB /* main.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit 201bd84

Please sign in to comment.