小编VMi*_*lls的帖子

具有时区偏移的c中的strptime

我很难找到一种方法来解析时区,如下所示:"星期四,2011年9月1日09:06:03 -0400(美国东部时间)"

我需要在我的程序的更大方案中做的是接受char*并将其转换为time_t.以下是我编写的一个简单的测试程序,试图找出strptime是否完全考虑时区,并且它似乎不是(当这个测试程序执行时,所有打印的数字在它们应该不同时是相同的).建议?

我也尝试使用GNU getdate和getdate_r,因为对于可能灵活的格式来说,这似乎是一个更好的选择,但是我从编译器得到了一个"隐式函数声明"警告,暗示我没有包含正确的库.还有其他我应该#include使用getdate吗?

#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>

#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
#include <time.h>

int main (int argc, char** argv){

char *timestr1, *timestr2, *timestr3;
struct tm time1, time2, time3;
time_t timestamp1, timestamp2, timestamp3;

timestr1 = "Thu, 1 Sep 2011 09:06:03 -0400 (EDT)"; // -4, and with timezone name
timestr2 = "Thu, 1 Sep 2011 09:06:03 -0000"; // -0

strptime(timestr1, "%a, %d %b %Y %H:%M:%S %Z", &time1); //includes UTC offset
strptime(timestr2, "%a, %d %b %Y …
Run Code Online (Sandbox Code Playgroud)

c timezone getdate strptime timezone-offset

5
推荐指数
1
解决办法
8567
查看次数

Python迭代对象列表“不可迭代”

Python 新手,但我已经研究了几个小时。如果我错过了一些明显的事情,请原谅我。

我有一个名为 LineItem 的类,它有一个属性 _lineItems,即属于给定 LineItem 的 LineItem 列表。基本上是一个子列表。

我想打印出 LineItem 及其所有子项(以及子项自己的子项),但我在迭代时遇到问题。

from decimal import * 
class LineItem(object):
    """
    Instance attributes:
      amount: Decimal
      _lineItems: list of child internal lineitems (possibly an empty list)
      isInternal: bool
    """

    def __init__(self, **kw):
        self.amount = Decimal(0)
        self._lineItems = []
        self.isInternal = False
        for k, v in kw.items():
            setattr(self, k, v)
Run Code Online (Sandbox Code Playgroud)

下面给我带来麻烦的一个示例 LineItem 定义为 ext2,具有三个子项。

# External line item with one level of children
int1 = LineItem(amount=Decimal('1886.75'), description='State Dues',
         isInternal=True)
int2 = LineItem(amount=Decimal('232.50'), description='National …
Run Code Online (Sandbox Code Playgroud)

python iteration list object

5
推荐指数
1
解决办法
2万
查看次数

malloc/free,似乎获得了多个自由

我写了一个函数来测试给出的路径是一个有效的Maildir目录(标准的Maildir有三个子文件夹"小人"的"新"和"TMP").函数接受假定的目录,检查这些子文件夹,并适当地返回.

我正在使用当前代码在第二个自由语句中获得段错误,并且我同样得到了"无效的下一个大小"错误,组织略有不同.更令人困惑的,它只出现segfaults一些目录,而成功地完成别人,没有明显原因(虽然它是在哪些它将段错误上一致).随着第二个free()被注释掉,所有准确格式化的目录都成功完成.

显然我是双重释放.我的问题是,为什么以及如何?如果第一个free在条件语句中并且我们在释放后立即返回,我们永远不会获得第二个free.如果我们达到第二个免费,那意味着我们跳过了第一个免费...对吧?

我知道在这种情况下它是完全正常的,因为该系统将在节目结束回收内存,但我更感兴趣的是,这是比只是让代码工作发生的原因.如果我在查看不同的情况,由函数调用的函数调用的函数和内存可能是一个问题呢?我不需要第二次免费回收内存吗?

int is_valid_folder(char* maildir)
{
 struct stat *buf;
 buf = (struct stat *) malloc(sizeof(struct stat));

char* new = strdup(maildir);
char* cur = strdup(maildir);
char* tmp = strdup(maildir);
strcat (cur, "/cur"); strcat (new, "/new"); strcat (tmp, "/tmp");

if(stat(cur, buf) || stat(tmp, buf) || stat(new, buf))
{
printf("Problem stat-ing one of the cur/new/tmp folders\n");
printf("Error number %d\n", errno);
free(buf);
return 1;
}

free(buf);
return 0; //a valid folder path for this function
}
Run Code Online (Sandbox Code Playgroud)

c malloc free segmentation-fault

1
推荐指数
1
解决办法
206
查看次数